Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/runs/max-duration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Runs have no maximum duration. You set a default `maxDuration` for your project

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.
The `maxDuration` parameter limits how long a task can execute. 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:

Expand All @@ -33,14 +33,14 @@ 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](/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:
The `maxDuration` is set in seconds, and is compared to the execution time of a single attempt (see [attempts](/runs#attempts)) of the task. Execution time is the wall-clock time since the attempt started, minus any time the run was paused. It includes time spent awaiting network calls such as LLM responses. It does not include time spent paused during the following:

- `wait.for` calls
- `wait.for` and `wait.until` calls longer than 5 seconds (shorter waits run in-process and count towards it)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

State the five-second boundary explicitly.

The SDK counts a wait of exactly five seconds, but “shorter waits” does not include that boundary. Say “waits of 5 seconds or less” so readers can determine how the limit applies.

- `triggerAndWait` calls
- `batchTriggerAndWait` calls
- `wait.forToken` calls

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

```ts /trigger/max-duration.ts
import { task, usage } from "@trigger.dev/sdk";
Expand All @@ -51,7 +51,7 @@ export const maxDurationTask = task({
run: async (payload: any, { ctx }) => {
let currentUsage = usage.getCurrent();

currentUsage.compute.attempt.durationMs; // The CPU time in milliseconds since the start of this attempt
currentUsage.compute.attempt.durationMs; // Execution time in milliseconds since the start of this attempt, excluding paused waits
},
});
```
Expand Down
2 changes: 1 addition & 1 deletion docs/snippets/paused-execution-free.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
In the Trigger.dev Cloud we automatically pause execution of tasks when they are waiting for
longer than a few seconds.

When triggering and waiting for subtasks, the parent is checkpointed and while waiting does not count towards compute usage. When waiting for a time period (`wait.for` or `wait.until`), a wait longer than 5 seconds does not count towards compute usage.
When triggering and waiting for subtasks (`triggerAndWait`, `batchTriggerAndWait`) or waiting on a [waitpoint token](/wait-for-token) (`wait.forToken`), the run is checkpointed and the wait does not count towards compute usage, however long it lasts. When waiting for a time period (`wait.for` or `wait.until`), a wait longer than 5 seconds does not count towards compute usage at all, and a wait of 5 seconds or less is billed in full.

Free compute isn't the same as freed concurrency: the concurrency slot is only released once we've snapshotted the machine and shut it down. For `wait.for` and `wait.until` that happens 60 seconds into the wait, so anything shorter stays `EXECUTING` and holds its slot for the whole wait.
Loading