Skip to Content

Max duration

Set a maximum duration for a task to run.

Runs have no maximum duration. You set a default maxDuration for your project and can override it per task or per run, or set it to timeout.None to let a run execute indefinitely.

Trigger is built for long running AI agents and workflows and we have seen Trigger.dev users with runs lasting more than six months.

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 {  } from "@trigger.dev/sdk";

export default ({
  : "proj_gtcwttqhhtlasxgfuhxs",
  : 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
  • wait.forToken calls

You can inspect the CPU time of a task inside the run function with our usage utility:

/trigger/max-duration.ts
import { ,  } from "@trigger.dev/sdk";

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

    currentUsage.compute.attempt.durationMs; // The CPU time in milliseconds since the start of this attempt
  },
});

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:

Configuring for a task

You can set a maxDuration on a specific task:

/trigger/max-duration-task.ts
import {  } from "@trigger.dev/sdk";

export const maxDurationTask = ({
  : "max-duration-task",
  : 300, // 300 seconds or 5 minutes
  : async (payload: any, {  }) => {
    //...
  },
});

This will override the default maxDuration set in the config file. If you have a config file with a default maxDuration of 60 seconds, and you set a maxDuration of 300 seconds on a task, the task will run for 300 seconds.

You can "turn off" the Max duration set in your config file for a specific task like so:

/trigger/max-duration-task.ts
import { ,  } from "@trigger.dev/sdk";

export const maxDurationTask = ({
  : "max-duration-task",
  : .None, // No max duration
  : async (payload: any, {  }) => {
    //...
  },
});

Configuring for a run

You can set a maxDuration on a specific run when you trigger a task:

/trigger/max-duration.ts
import { maxDurationTask } from "./trigger/max-duration-task";

// Trigger the task with a maxDuration of 300 seconds
const run = await maxDurationTask.trigger(
  { foo: "bar" },
  {
    maxDuration: 300, // 300 seconds or 5 minutes
  }
);

You can also set the maxDuration to timeout.None to turn off the max duration for a specific run:

/trigger/max-duration.ts
import {  } from "./trigger/max-duration-task";
import {  } from "@trigger.dev/sdk";

// Trigger the task with no maxDuration
const run = await .trigger(
  { foo: "bar" },
  {
    maxDuration: .None, // No max duration
  }
);

maxDuration in run context

You can access the maxDuration set for a run in the run context:

/trigger/max-duration-task.ts
import {  } from "@trigger.dev/sdk";

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

maxDuration and lifecycle functions

When a task run exceeds the maxDuration, the lifecycle functions cleanup, onSuccess, and onFailure will not be called.

Was this page helpful?

Last updated

Edit on GitHub

On this page