Software factory / Agent workflows
Agent workflow budgets and limits
An agent in a loop can spend a lot of money before you see it. You want each job of your agent workflow to stop at a cost, a number of tokens or a time that you choose.
3 min read
Set budget on the job, on an agent step, or on both. A budget has no default, so a job without one stops only at the time limit of the job and the monthly spend cap.
Start with a working workflow: see Write your first agent workflow.
1. Set a budget
import { agent, defineAgentWorkflow, manual } from '@tanstack-source/ci'
const model = { provider: 'anthropic', model: 'claude-opus-5-5' } as const
export default defineAgentWorkflow('careful-fix', { on: [manual()] }).job('fix', {
budget: { maxCostUsd: 5, maxTokens: 2_000_000, maxMinutes: 60 },
steps: [
agent({ name: 'Plan', model, prompt: 'Plan a fix for the failing tests.', tools: ['read_file', 'search'], budget: { maxCostUsd: 1 } }),
agent({ name: 'Fix', model, prompt: 'Fix the failing tests.', tools: ['read_file', 'run_command', 'write_file'] }),
],
})
Each limit is optional:
maxCostUsd: the model cost in dollars.maxTokens: the model tokens.maxMinutes: the minutes, at most 1440.
2. See how they add up
- The job's budget counts every agent step of the job. When it is used up, the next agent step does not start.
- A step's budget is its own limit, within what the job has left. Above,
Planstops at 1 dollar, andFixgets the rest of the 5 dollars. maxMinutescounts from when the job, or the step, first started.
3. Read the end
A job that reaches a budget fails, with a reason such as Step "Fix" stopped: The agent used its budget. Raise the budget in the workflow file, merge it, and start a new run. Retry from failed step runs the steps of the run's own workflow code, with its old budget.
Done
Each job stops at the cost, the tokens or the minutes that you set, and the run page says which limit it reached.
The time limit of a job
Every agent workflow job stops after 2 hours. timeoutMinutes accepts up to 1440, but the forge holds each job to 2 hours. A smaller timeoutMinutes stops the job sooner.
The 2 hours count for each job. When a lost container or a retry starts a new job for a cell, its 2 hours, and its budget, count again from zero. See Keep long jobs going.
What a run costs you
- A model on your own key is billed by its provider.
- A model that the forge pays for counts toward the monthly spend cap of the owner, or toward its plan while billing is on. At the limit, an agent step does not start.
- The runs list shows the tokens and the cost of each run: the sum of all its jobs. A cell that runs a new job after a lost container or a Retry from failed step adds the cost of each job one time.
- The Spend section lists each workflow as
<name> (agent workflow). It counts each run one time. When a retry ends the run again, the spend adds what the retry used, on the day it ended. - The
mediatools (image,videoandspeech) work only with an OpenAI model on the repository's own OpenAI key. Their cost is not in the budget or the spend cap: OpenAI bills you.
Other limits
- A job has at most 256 cells.
- An output has at most 1 MiB of JSON.
- A
schedule()runs at most every 5 minutes. - A job with a container waits for a container in the CI queue of the account. The CI limits count it with the CI jobs.
- The jobs of an untrusted run count toward the daily CI container time of the person who caused it.