Why agent runs get expensive: tokens, context growth and prompt caching
An agent resends its whole history on every step. Follow a twelve-step tool-calling run, see what each step costs and how long it takes, project a longer run, and find out how much prompt caching and shorter context save.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
The table is a trace of a coding agent: twelve LLM calls, each with the input tokens it sent, the output tokens it wrote and the latency of the tool it then called. The orange columns price each call and time it. The chart plots the input tokens per step as white dots with a dashed trend line, and the bars underneath show what each step cost.
The results panel totals the run and projects what a longer run of the same agent would cost. The prices default to $3 per million input tokens and $15 per million output tokens; set them to your model's prices.
The algorithm
An LLM keeps no memory between calls. An agent built on it therefore sends, on every step, the system prompt, the task, the tool definitions and everything that happened so far: each earlier answer and each tool result. The loop of a ReAct-style agent is simple: call the model, run the tool it asked for, append the result, call the model again.
That append is the cost driver. If each step adds about the same amount of text, input grows linearly with the step number, so the total input over N steps grows with N²: doubling the length of a run roughly quadruples its input tokens. Output tokens grow only linearly, because each step writes a short decision or tool call.
The simulation fits a line through the input tokens per step, a starting size a and a growth b per step, and uses it to project the cost of a run of N steps: N × a + b × N(N+1)/2 input tokens, plus the average output per step times N.
Prompt caching changes the input price. Providers can reuse the prefix of a prompt they have just processed and bill those cached tokens at a fraction of the normal rate; the simulation uses 10%, and real discounts vary by provider, so check yours. In an agent loop most of each prompt is the unchanged history, so the cacheable share can be high.
With the defaults the twelve steps send 95,310 input tokens and write 2,660 output tokens. The run costs $0.33, and 87.8% of that is input. Context grows by about 1,020 tokens a step. A 40-step run of the same agent would cost about $2.80, almost nine times as much for a little over three times the steps. With 80% of input read from cache, the twelve-step run costs $0.12 and the 40-step projection $0.88.
Things to try
- Raise the cache share to 0.9 and see input stop dominating the bill.
- Set the projection to 100 steps: about $16 for one run without caching. This is why long-running agents summarise or trim old steps.
- Halve the input price and compare with halving the output price: for agents, input price matters far more.
- Lower the output speed to 30 tokens a second to see generation, not tools, dominate the run time.
- Paste the token counts from your own LangSmith or Langfuse trace and download the per-step costs.
How it works in real systems
Tracing tools record exactly these numbers: LangSmith and Langfuse attach token counts, cost and latency to every span of a run, so you can find the steps that blow up the context. The standard fixes are prompt caching with a stable prefix (system prompt and tool definitions first), trimming or summarising old tool outputs, storing large results outside the context and passing references, splitting work across sub-agents with their own short contexts, and setting a hard limit on steps and spend per run.