# 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.

<div data-sim="specx" data-spec="{&quot;level&quot;:&quot;intermediate&quot;,&quot;head&quot;:&quot;Why agent runs get expensive: context growth in the agent loop&quot;,&quot;sub&quot;:&quot;A twelve-step tool-calling run. Each step resends the whole history, so input tokens grow every step. See the cost and time per step, and what prompt caching saves.&quot;,&quot;params&quot;:[{&quot;id&quot;:&quot;pin&quot;,&quot;label&quot;:&quot;Input price&quot;,&quot;min&quot;:0.1,&quot;max&quot;:15,&quot;step&quot;:0.1,&quot;value&quot;:3,&quot;unit&quot;:&quot;$ / M tokens&quot;},{&quot;id&quot;:&quot;pout&quot;,&quot;label&quot;:&quot;Output price&quot;,&quot;min&quot;:0.4,&quot;max&quot;:75,&quot;step&quot;:0.1,&quot;value&quot;:15,&quot;unit&quot;:&quot;$ / M tokens&quot;},{&quot;id&quot;:&quot;cache&quot;,&quot;label&quot;:&quot;Share of input read from the prompt cache&quot;,&quot;min&quot;:0,&quot;max&quot;:0.95,&quot;step&quot;:0.05,&quot;value&quot;:0,&quot;note&quot;:&quot;Cached input is billed at 10% of the input price here&quot;},{&quot;id&quot;:&quot;tps&quot;,&quot;label&quot;:&quot;Output speed&quot;,&quot;min&quot;:20,&quot;max&quot;:300,&quot;step&quot;:10,&quot;value&quot;:80,&quot;unit&quot;:&quot;tokens/s&quot;},{&quot;id&quot;:&quot;nsteps&quot;,&quot;label&quot;:&quot;Steps to project&quot;,&quot;min&quot;:12,&quot;max&quot;:100,&quot;step&quot;:1,&quot;value&quot;:40}],&quot;data&quot;:{&quot;columns&quot;:[&quot;step&quot;,&quot;in_tok&quot;,&quot;out_tok&quot;,&quot;tool_ms&quot;],&quot;rows&quot;:[[1,2400,180,850],[2,3350,210,1200],[3,4420,160,640],[4,5300,240,2100],[5,6390,190,900],[6,7420,220,1500],[7,8360,170,700],[8,9520,260,3100],[9,10480,200,950],[10,11510,230,1300],[11,12460,180,800],[12,13700,420,0]],&quot;note&quot;:&quot;An illustrative trace of a coding agent: input and output tokens of each LLM call and the latency of the tool it called. Export the same numbers from a LangSmith or Langfuse trace of your agent.&quot;},&quot;pre&quot;:[{&quot;id&quot;:&quot;slope&quot;,&quot;label&quot;:&quot;Context growth per step (tokens)&quot;,&quot;expr&quot;:&quot;cov(step, in_tok)/vari(step)&quot;,&quot;fmt&quot;:&quot;i&quot;},{&quot;id&quot;:&quot;base&quot;,&quot;label&quot;:&quot;First-step input (fitted)&quot;,&quot;expr&quot;:&quot;mean(in_tok) - slope*mean(step)&quot;,&quot;hide&quot;:true}],&quot;row&quot;:[{&quot;id&quot;:&quot;cost&quot;,&quot;label&quot;:&quot;cost ($)&quot;,&quot;expr&quot;:&quot;(in_tok*(1 - cache)*pin + in_tok*cache*pin*0.1 + out_tok*pout)/1000000&quot;,&quot;tex&quot;:&quot;c_i = \\frac{x_i\\,(1-h)\\,p_{in} + 0.1\\,x_i\\,h\\,p_{in} + y_i\\,p_{out}}{10^6}&quot;},{&quot;id&quot;:&quot;secs&quot;,&quot;label&quot;:&quot;seconds&quot;,&quot;expr&quot;:&quot;out_tok/tps + tool_ms/1000&quot;}],&quot;derived&quot;:[{&quot;id&quot;:&quot;tot_in&quot;,&quot;label&quot;:&quot;Input tokens in the run&quot;,&quot;expr&quot;:&quot;sum(in_tok)&quot;,&quot;fmt&quot;:&quot;i&quot;},{&quot;id&quot;:&quot;tot_out&quot;,&quot;label&quot;:&quot;Output tokens in the run&quot;,&quot;expr&quot;:&quot;sum(out_tok)&quot;,&quot;fmt&quot;:&quot;i&quot;},{&quot;id&quot;:&quot;tot_cost&quot;,&quot;label&quot;:&quot;Cost of the run ($)&quot;,&quot;expr&quot;:&quot;sum(cost)&quot;},{&quot;id&quot;:&quot;in_share&quot;,&quot;label&quot;:&quot;Share of cost from input&quot;,&quot;expr&quot;:&quot;sum(in_tok)*(1 - cache*0.9)*pin/1000000/tot_cost&quot;,&quot;fmt&quot;:&quot;p&quot;},{&quot;id&quot;:&quot;tot_secs&quot;,&quot;label&quot;:&quot;Run time (s)&quot;,&quot;expr&quot;:&quot;sum(secs)&quot;},{&quot;id&quot;:&quot;proj&quot;,&quot;label&quot;:&quot;Projected cost of a longer run ($)&quot;,&quot;expr&quot;:&quot;((nsteps*base + slope*nsteps*(nsteps + 1)/2)*(1 - cache*0.9)*pin + nsteps*mean(out_tok)*pout)/1000000&quot;,&quot;tex&quot;:&quot;C(N) \\approx \\frac{\\big(N a + b\\,\\tfrac{N(N+1)}{2}\\big)(1-0.9h)\\,p_{in} + N\\,\\bar y\\,p_{out}}{10^6}&quot;}],&quot;chart&quot;:{&quot;title&quot;:&quot;Input tokens per step: the context keeps growing&quot;,&quot;x&quot;:{&quot;var&quot;:&quot;t&quot;,&quot;label&quot;:&quot;Step of the agent loop&quot;,&quot;from&quot;:1,&quot;to&quot;:12},&quot;series&quot;:[{&quot;label&quot;:&quot;Linear trend of input tokens&quot;,&quot;expr&quot;:&quot;base + slope*t&quot;,&quot;dash&quot;:true}],&quot;points&quot;:{&quot;x&quot;:&quot;step&quot;,&quot;y&quot;:&quot;in_tok&quot;,&quot;label&quot;:&quot;Input tokens sent at each step&quot;}},&quot;bars&quot;:{&quot;title&quot;:&quot;Cost of each step ($)&quot;,&quot;y&quot;:&quot;cost&quot;,&quot;label&quot;:&quot;step&quot;},&quot;steps&quot;:[{&quot;title&quot;:&quot;Every step resends the history&quot;,&quot;text&quot;:&quot;The model is stateless, so each call carries the system prompt, the task and every earlier tool result: input grows by about {slope} tokens a step.&quot;,&quot;show&quot;:[&quot;slope&quot;]},{&quot;title&quot;:&quot;Price each call&quot;,&quot;text&quot;:&quot;Over twelve steps the agent sends {tot_in} input tokens and writes {tot_out} output tokens.&quot;,&quot;show&quot;:[&quot;tot_in&quot;,&quot;tot_out&quot;]},{&quot;title&quot;:&quot;See where the money goes&quot;,&quot;text&quot;:&quot;The run costs ${tot_cost}, and {in_share:p} of it is input, mostly the same history sent again.&quot;,&quot;show&quot;:[&quot;tot_cost&quot;,&quot;in_share&quot;]},{&quot;title&quot;:&quot;Add the waiting&quot;,&quot;text&quot;:&quot;Generation and tools take {tot_secs} seconds in all.&quot;,&quot;show&quot;:[&quot;tot_secs&quot;]},{&quot;title&quot;:&quot;Project a longer run&quot;,&quot;text&quot;:&quot;Because input grows linearly, total input grows with the square of the steps: {nsteps} steps would cost about ${proj}. Prompt caching and summarising old steps bend this curve.&quot;,&quot;show&quot;:[&quot;proj&quot;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## 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.

## Sources

- [Anthropic documentation: Prompt caching](https://docs.claude.com/en/docs/build-with-claude/prompt-caching)
- [OpenAI documentation: Prompt caching](https://platform.openai.com/docs/guides/prompt-caching)
- [Yao et al.: ReAct: Synergizing Reasoning and Acting in Language Models (arXiv)](https://arxiv.org/abs/2210.03629)
