Skip to main content
The maxDuration parameter sets a maximum compute time limit for tasks. When a task exceeds this duration, it will be automatically stopped. This helps prevent runaway tasks and manage compute resources effectively. You must set a default maxDuration in your trigger.config.ts file, which will apply to all tasks unless overridden:
/config/trigger.config.ts
import { defineConfig } from "@trigger.dev/sdk";

export default defineConfig({
  project: "proj_gtcwttqhhtlasxgfuhxs",
  maxDuration: 60, // 60 seconds or 1 minute
});
The minimum maxDuration is 5 seconds. If you want to avoid timeouts, set this value to a very large number of seconds.
You can set the maxDuration for a run in the following ways:

How it works

The maxDuration is set in seconds, and is compared to the CPU time elapsed since the start of a single execution (which we call attempts) of the task. The CPU time is the time that the task has been actively running on the CPU, and does not include time spent waiting during the following:
  • wait.for calls
  • triggerAndWait calls
  • batchTriggerAndWait calls
You can inspect the CPU time of a task inside the run function with our usage utility:
/trigger/max-duration.ts
import { task, usage } from "@trigger.dev/sdk";

export const maxDurationTask = task({
  id: "max-duration-task",
  maxDuration: 300, // 300 seconds or 5 minutes
  run: async (payload: any, { ctx }) => {
    let currentUsage = usage.getCurrent();

    currentUsage.attempt.durationMs; // The CPU time in milliseconds since the start of the run
  },
});
The above value will be compared to the maxDuration you set. If the task exceeds the maxDuration, it will be stopped with the following error: