---
title: "Max compute time"
sidebarTitle: "Max compute time"
description: "Set a maximum compute time limit for a task to run."
---
The `maxComputeSeconds` 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.
`maxDuration` replaces the deprecated `maxComputeSeconds` parameter. Both work the same way, but we recommend using `maxDuration` for clarity.
You must set a default maxComputeSeconds in your `trigger.config.ts` file, which will apply to all tasks unless overridden:
```ts /config/trigger.config.ts
import { defineConfig } from "@trigger.dev/sdk";
export default defineConfig({
project: "proj_gtcwttqhhtlasxgfuhxs",
maxComputeSeconds: 60, // 60 seconds or 1 minute
});
```
The minimum maxComputeSeconds is 5 seconds. If you want to avoid timeouts, set this value to a very large number of seconds.
You can set the `maxComputeSeconds` for a run in the following ways:
- Across all your tasks in the [config](/config/config-file#max-compute-time)
- On a specific task
- On a specific run when you [trigger a task](/triggering#maxcomputeseconds)
## How it works
The `maxComputeSeconds` is set in seconds, and is compared to the CPU time elapsed since the start of a single execution (which we call [attempts](/runs#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:
```ts /trigger/max-duration.ts
import { task, usage } from "@trigger.dev/sdk";
export const maxComputeSecondsTask = task({
id: "max-duration-task",
maxComputeSeconds: 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 `maxComputeSeconds` you set. If the task exceeds the `maxComputeSeconds`, it will be stopped with the following error:

## Configuring for a task
You can set a `maxComputeSeconds` on a specific task:
```ts /trigger/max-duration-task.ts
import { task } from "@trigger.dev/sdk";
export const maxComputeSecondsTask = task({
id: "max-duration-task",
maxComputeSeconds: 300, // 300 seconds or 5 minutes
run: async (payload: any, { ctx }) => {
//...
},
});
```
This will override the default `maxComputeSeconds` set in the config file. If you have a config file with a default `maxComputeSeconds` of 60 seconds, and you set a `maxComputeSeconds` 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:
```ts /trigger/max-duration-task.ts
import { task, timeout } from "@trigger.dev/sdk";
export const maxComputeSecondsTask = task({
id: "max-duration-task",
maxComputeSeconds: timeout.None, // No max duration
run: async (payload: any, { ctx }) => {
//...
},
});
```
## Configuring for a run
You can set a `maxComputeSeconds` on a specific run when you trigger a task:
```ts /trigger/max-duration.ts
import { maxComputeSecondsTask } from "./trigger/max-duration-task";
// Trigger the task with a maxComputeSeconds of 300 seconds
const run = await maxComputeSecondsTask.trigger(
{ foo: "bar" },
{
maxComputeSeconds: 300, // 300 seconds or 5 minutes
}
);
```
You can also set the `maxComputeSeconds` to `timeout.None` to turn off the max duration for a specific run:
```ts /trigger/max-duration.ts
import { maxComputeSecondsTask } from "./trigger/max-duration-task";
import { timeout } from "@trigger.dev/sdk";
// Trigger the task with no maxComputeSeconds
const run = await maxComputeSecondsTask.trigger(
{ foo: "bar" },
{
maxComputeSeconds: timeout.None, // No max duration
}
);
```
## maxComputeSeconds in run context
You can access the `maxComputeSeconds` set for a run in the run context:
```ts /trigger/max-duration-task.ts
import { task } from "@trigger.dev/sdk";
export const maxComputeSecondsTask = task({
id: "max-duration-task",
maxComputeSeconds: 300, // 300 seconds or 5 minutes
run: async (payload: any, { ctx }) => {
console.log(ctx.run.maxComputeSeconds); // 300
},
});
```
## maxComputeSeconds and lifecycle functions
When a task run exceeds the `maxComputeSeconds`, the lifecycle functions `cleanup`, `onSuccess`, and `onFailure` will not be called.