Skip to content

Commit 082afa9

Browse files
jackbridgerTrigger.dev RepoOps
authored andcommitted
docs(ai-chat): add agent compute costs page
Mono-RevId: 025882c751590dbb14220e5c66d2385355970a74
1 parent c33d437 commit 082afa9

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: "Agents are only billed while they're working"
3+
sidebarTitle: "Compute costs"
4+
description: "You don't pay for compute while your AI agent waits for a user reply, an approval or a webhook. See what stops billing, what keeps an agent billed, and when an idle agent's run ends."
5+
---
6+
7+
**On Trigger.dev Cloud, you pay for an agent's compute while its code is running, not while it waits for a user, an approval or a webhook.** By default an agent stays on for 30 seconds after each reply before billing stops, and every run also has a small invocation cost.
8+
9+
## When an agent is billed
10+
11+
- **Working** means its code is running on a machine. That includes waiting for the model to reply, because your code is still running.
12+
- **Waiting** means it's waiting on something outside itself, like the user's next message. Billing stops once the agent is in the wait, after the wait itself is set up (for example, `triggerAndWait()` first triggers the child task). For the user's next message, it stops once the short idle window below ends. Soon after, we save the agent and switch the machine off, which we call suspending. When there's something to do, we switch it back on and your code carries on from the same line, with its variables intact. Nothing is replayed.
13+
14+
With the settings in the example below, an agent that works for 10 seconds and then waits a day for a person to reply is billed for the 10 seconds, not the day, plus the run's invocation cost.
15+
16+
## An agent stays on for 30 seconds after each reply
17+
18+
After each reply, the agent stays on in case the user answers quickly. That time is billed, and the agent keeps its concurrency slot. Set [`idleTimeoutInSeconds`](/ai-chat/reference#chatagentoptions) to `0` to skip that window, so billing stops right after each reply, once any `onChatSuspend` hook has run, unless a message is already waiting. The agent suspends soon after if it's still waiting.
19+
20+
## The run ends after an hour without a message
21+
22+
If no message arrives within an hour after that idle window, the run ends. The next message starts a new run that picks the conversation up from [transcript storage](/ai-chat/transcript-storage). That's slower than waking a suspended agent, the new run has its own [invocation cost](https://trigger.dev/pricing), and it runs on your latest deployed version, unless the session is [pinned to a version](/ai-chat/patterns/version-upgrades). Change the hour with [`turnTimeout`](/ai-chat/reference#chatagentoptions).
23+
24+
## A refund agent stops billing while it waits for approval
25+
26+
This agent asks before it issues a refund, and billing stops while it waits for the user to approve.
27+
28+
```ts trigger/support-agent.ts
29+
import { chat } from "@trigger.dev/sdk/ai";
30+
import { tool, stepCountIs } from "ai";
31+
import { anthropic } from "@ai-sdk/anthropic";
32+
import { z } from "zod";
33+
import { refund } from "@/lib/refunds"; // Your own refund code
34+
35+
const issueRefund = tool({
36+
description: "Refund an order after the user approves.",
37+
inputSchema: z.object({ orderId: z.string() }),
38+
needsApproval: true,
39+
execute: async ({ orderId }) => refund(orderId),
40+
});
41+
42+
export const supportAgent = chat.agent({
43+
id: "support-agent",
44+
tools: { issueRefund },
45+
idleTimeoutInSeconds: 0, // Stop billing as soon as each reply ends
46+
turnTimeout: "24h", // Keep the run open for a day between messages
47+
run: async ({ messages, tools, signal, streamText }) => {
48+
return streamText({
49+
model: anthropic("claude-sonnet-5-5"),
50+
messages,
51+
tools,
52+
abortSignal: signal,
53+
stopWhen: stepCountIs(15),
54+
});
55+
},
56+
});
57+
```
58+
59+
See [human-in-the-loop](/ai-chat/patterns/human-in-the-loop) for the frontend side.
60+
61+
## What stops billing
62+
63+
Billing stops while an agent waits on something outside itself:
64+
65+
- The user's next message ([chat agents](/ai-chat/how-it-works))
66+
- Someone approving a tool or answering a question ([human-in-the-loop](/ai-chat/patterns/human-in-the-loop)), or completing a [wait token](/wait-for-token)
67+
- A subtask started with [`triggerAndWait()`](/triggering#yourtask-triggerandwait)
68+
- A [webhook](/webhooks/waiters) or data on an [input stream](/tasks/streams#input-streams) (`.wait()`)
69+
- A [`wait.for()`](/wait-for) or [`wait.until()`](/wait-until) longer than 5 seconds
70+
71+
## What keeps an agent billed
72+
73+
Everything else, including:
74+
75+
- The idle window after each reply (30 seconds by default)
76+
- Waiting for a model or API to reply (`streamText()`, `generateText()`, `fetch()`)
77+
- Running tools
78+
- Polling, `setTimeout` and sleep helpers
79+
- Streaming a [sub-agent](/ai-chat/patterns/sub-agents)'s reply with `chat.forwardSubAgent()`
80+
- Listening on an input stream with `.once()` or `.on()`
81+
- A `wait.for()` or `wait.until()` of 5 seconds or less, which runs in your process
82+
83+
If your agent polls another system, have that system complete a [wait token](/wait-for-token) or send a [webhook](/webhooks/waiters) instead. Your agent then stops billing while it waits.
84+
85+
## Other details that affect what you pay
86+
87+
- **Suspended agents don't use your concurrency.** An agent keeps its slot during the idle window and until it's suspended. When it wakes up, it keeps its original place in the queue, so it usually runs before newer runs. A newer run with a higher priority can still go first.
88+
- **Subtasks are billed separately.** A subtask started with `triggerAndWait()` is billed for its own working time and has its own invocation cost.
89+
- **Waiting time doesn't count toward [`maxDuration`](/runs/max-duration).** The idle window after each reply does.
90+
- **Database connections can be closed while an agent is suspended.** Use a pool that reconnects ([details](/database-connections#how-suspend-affects-connections)).
91+
- **An agent that wakes up runs on the version it started on**, even if you deploy while it's waiting ([version locking](/versioning#version-locking)).
92+
93+
## Check what an agent cost
94+
95+
Once a run finishes, its details panel in the dashboard shows its usage duration, invocation cost and compute cost. See [pricing](https://trigger.dev/pricing) for rates and [how to reduce your spend](/how-to-reduce-your-spend) for other savings.

‎docs/ai-chat/patterns/human-in-the-loop.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ After the turn finishes, the run stays active for `idleTimeoutInSeconds` (30 sec
3737

3838
[`turnTimeout`](/ai-chat/backend#chat-agent) controls how long a run waits for the next message (default `"1h"`). When it expires, the run ends. A later answer starts a continuation run that restores the conversation from [transcript storage](/ai-chat/transcript-storage). Raising `maxDuration` isn't necessary for time spent suspended.
3939

40+
See [agent compute costs](/ai-chat/agent-compute-costs) for what you're billed for while an agent waits.
41+
4042
## Backend: define the tool
4143

4244
Define an `inputSchema` for the question and omit `execute` so the frontend can supply the answer. When the model calls `askUser`, `streamText` returns control to your agent.

‎docs/docs.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"ai-chat/chat-local",
4444
"ai-chat/types",
4545
"ai-chat/custom-agents",
46-
"ai-chat/how-it-works"
46+
"ai-chat/how-it-works",
47+
"ai-chat/agent-compute-costs"
4748
]
4849
},
4950
{

0 commit comments

Comments
 (0)