Skip to Content

Usage

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.

import { , ,  } from "@trigger.dev/sdk";

export const heavyTask = ({
  : "heavy-task",
  : {
    preset: "medium-2x",
  },
  : async (payload, {  }) => {
    // 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 = .();
    /* 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 longer than 5 seconds 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 .for({ seconds: 10 });

    // This will give the same result as before the wait.
    currentUsage = .();

    // Do more compute
    const result2 = await convertVideo(payload.videoUrl);

    // This would give a different value
    currentUsage = .();
  },
});

In Trigger.dev cloud we do not include time between attempts, before your code executes, or waits longer than 5 seconds towards the compute cost or duration. Waits of 5 seconds or less still count.

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.

single run
import {  } from "@trigger.dev/sdk";

const run = await .retrieve("run-id");
.(run.costInCents, run.baseCostInCents, run.durationMs);
const totalCost = run.costInCents + run.baseCostInCents;
multiple runs
import {  } from "@trigger.dev/sdk";

let totalCost = 0;
for await (const run of .list({ tag: "user_123456" })) {
  totalCost += run.costInCents + run.baseCostInCents;
  .(run.costInCents, run.baseCostInCents, run.durationMs);
}

.("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:

import { ,  } from "@trigger.dev/sdk";

// Inside a task run function, or inside a function that's called from there.
const { result, compute } = await .(async () => {
  //...Do something for 1 second
  return {
    foo: "bar",
  };
});

.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.

Was this page helpful?

Last updated

Edit on GitHub

On this page