|
| 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. |
0 commit comments