Files
triggerdotdev--trigger.dev/docs/run-usage.mdx
T
James Ritchie fade22015f Docs – v4 GA updates (#2298)
* Adds new features table to top of v4 upgrade guide

* Adds wait idempotency to wait-until, wait-for, and wait-for-token pages

* Adds new priority docs page and updates the v4 upgrade guide

* Adds new task lifecycle hooks

* Removes the message about requiring tasks to be exported

* Adds new global lifecycle hooks section

* Moves sections from upgrade guide into the table

* Adds hidden task page

* Improves the global lifecycle hooks section

* Updates middleware and locals section

* Adds new useWaitToken page to the react hooks section

* Adds a new ai.tool section

* Moves Docker (legacy) page into self-hosting section

* Removes known issues from v4 upgrade guide

* Replace “toolTask” with “ai.tool” in the Streams page example

* Renames guide to “Migrating from v3” and adds redirect

* Remove references to v4

* Removes changelog from migration guide

* The installation guide now references `@latest update`

* Changes all references from `/sdk/v3` to `/sdk`

* Updates @v4-beta to @latest

* Fixed broken link

* Fixes broken link

* Adds an upgrade to v4 using AI section

* Fixes 2 broken links

* Adds an entry for targetting preview branches

* Updates the run statuses

* Adds boolean helpers section to the runs and realtime pages

* Updates the concurrency page

* Updates the test page to include the new options

* Adds SDK and curl options for the preview branch targeting

* Updates new bulk actions page

* Remove the releasing concurrency section

* Got rid of some more @v4-beta mentions

* Improved rate limit docs

* Improved migrating docs

* Removed commented sections of the docs

* useWaitToken hook

* Fixed the description

* Fix for missing test image

---------

Co-authored-by: Matt Aitken <matt@mattaitken.com>
Co-authored-by: Dan <8297864+D-K-P@users.noreply.github.com>
2025-08-18 12:34:55 +01:00

110 lines
3.2 KiB
Plaintext

---
title: "Usage"
description: "Get compute duration and cost from inside a run, or for a specific block of code."
---
## Getting the run cost and duration
You can get the cost and duration of the current including retries of the same run.
```ts
export const heavyTask = task({
id: "heavy-task",
machine: {
preset: "medium-2x",
},
run: async (payload, { ctx }) => {
// Do some compute
const result = await convertVideo(payload.videoUrl);
// Get the current cost and duration up until this line of code
// This includes the compute time of the previous lines
let currentUsage = usage.getCurrent();
/* currentUsage = {
compute: {
attempt: {
costInCents: 0.01700,
durationMs: 1000,
},
total: {
costInCents: 0.0255,
durationMs: 1500,
},
},
baseCostInCents: 0.0025,
totalCostInCents: 0.028,
}
*/
// In the cloud product we do not count waits towards the compute cost or duration.
// We also don't include time between attempts or before the run starts executing your code.
// So this line does not affect the cost or duration.
await wait.for({ seconds: 5 });
// This will give the same result as before the wait.
currentUsage = usage.getCurrent();
// Do more compute
const result = await convertVideo(payload.videoUrl);
// This would give a different value
currentUsage = usage.getCurrent();
},
});
```
<Note>
In Trigger.dev cloud we do not include time between attempts, before your code executes, or waits
towards the compute cost or duration.
</Note>
## Getting the run cost and duration from your backend
You can use [runs.retrieve()](/management/runs/retrieve) to get a single run or [runs.list()](/management/runs/list) to get a list of runs. The response will include `costInCents` `baseCostInCents` and `durationMs` fields.
```ts single run
import { runs } from "@trigger.dev/sdk";
const run = await runs.retrieve("run-id");
console.log(run.costInCents, run.baseCostInCents, run.durationMs);
const totalCost = run.costInCents + run.baseCostInCents;
```
```ts multiple runs
import { runs } from "@trigger.dev/sdk";
let totalCost = 0;
for await (const run of runs.list({ tag: "user_123456" })) {
totalCost += run.costInCents + run.baseCostInCents;
console.log(run.costInCents, run.baseCostInCents, run.durationMs);
}
console.log("Total cost", totalCost);
```
## Getting the cost and duration of a block of code
You can also wrap code with `usage.measure` to get the cost and duration of that block of code:
```ts
// Inside a task run function, or inside a function that's called from there.
const { result, compute } = await usage.measure(async () => {
//...Do something for 1 second
return {
foo: "bar",
};
});
logger.info("Result", { result, compute });
/* result = {
foo: "bar"
}
compute = {
costInCents: 0.01700,
durationMs: 1000,
}
*/
```
This will work from inside the `run` function, our lifecycle hooks (like `onStart`, `onFailure`, `onSuccess`, etc.), or any function you're calling from the `run` function. It won't work for code that's not executed using Trigger.dev.