Getting the run cost and duration
You can get the cost and duration of the current including retries of the same run.
export const heavyTask = task({
id: "heavy-task",
machine: {
preset: "medium-2x",
},
run: async (payload, { ctx }) => {
const result = await convertVideo(payload.videoUrl);
let currentUsage = usage.getCurrent();
compute: {
attempt: {
costInCents: 0.01700,
durationMs: 1000,
},
total: {
costInCents: 0.0255,
durationMs: 1500,
},
},
baseCostInCents: 0.0025,
totalCostInCents: 0.028,
}
*/
await wait.for({ seconds: 5 });
currentUsage = usage.getCurrent();
const result = await convertVideo(payload.videoUrl);
currentUsage = usage.getCurrent();
},
});
In Trigger.dev cloud we do not include time between attempts, before your code executes, or waits
towards the compute cost or duration.
Getting the run cost and duration from your backend
You can use runs.retrieve() to get a single run or runs.list() to get a list of runs. The response will include costInCents
baseCostInCents
and durationMs
fields.
import { runs } from "@trigger.dev/sdk/v3";
const run = await runs.retrieve("run-id");
console.log(run.costInCents, run.baseCostInCents, run.durationMs);
const totalCost = run.costInCents + run.baseCostInCents;
import { runs } from "@trigger.dev/sdk/v3";
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:
const { result, compute } = await usage.measure(async () => {
return {
foo: "bar",
};
});
logger.info("Result", { result, compute });
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.