Where LLM Agent Tokens Actually Go
Stateless LLM calls force agents to resend history every turn, so most tokens pay for state, not new answers. This article shows the quadratic input-token growth of multi-step tool loops, classifies output vs. repeated vs. new input tokens, and covers prefix trimming, result offloading, rolling compression, subtasks, prefix cache, and external memory.
1. Why tokens disappear: the model has no memory
Every large language model call is stateless. The model does not keep the previous inference; it only sees the text in the current request. If an agent must know what it already did and what it got back, the only option is to put that entire history into the next request.
Most token spend is therefore rereading, not new generation. Each step’s input includes every earlier step, so input size grows as the task proceeds. A single Q&A has one question and one answer and does not accumulate. As soon as the job needs several actions that depend on previous results, history is sent again and again.
2. A token bill for a multi-step task
Suppose a task loops: the model writes text, calls a tool, receives the result, then starts the next round. Each request looks roughly like this.
| Round | What the request contains | What this round adds |
|---|---|---|
| 1 | System prompt, tool descriptions, user instruction | First reply, reasoning, tool call |
| 2 | Everything from round 1 plus round-1 tool result | Second reply, reasoning, tool call, tool result |
| 3 | Everything from rounds 1–2 plus tool results | Third reply, reasoning, tool call, tool result |
| n | All previous rounds | This round’s reply, reasoning, tool call, and tool result |
Let the fixed prefix (system prompt plus tool descriptions) have length P, and let each round add about d tokens. Round k’s input length is about P + (k−1)d. After n rounds, total input tokens are:
Cumulative input = nP + d × n(n−1)/2
Cumulative input grows quadratically with the number of rounds. With hypothetical numbers P = 5000 and d = 800 per round (about 150 model output and 650 tool result):
- 50 rounds: about 250,000 + 980,000 ≈ 1.23 million input tokens
- 200 rounds: about 1,000,000 + 15,920,000 ≈ 16.9 million input tokens
In the same task, 200 rounds of model output total about 30,000 tokens, while cumulative input exceeds 16 million. Doubling rounds roughly quadruples input. Controlling round count and tokens added per round are the two main cost levers.
Sketch of the accumulation:
total_input = 0history_len = FIXED_PREFIX_LENfor round in range(1, n + 1):total_input += history_lennew_tokens = model_output_tokens + tool_result_tokenshistory_len += new_tokens
3. What those tokens actually produce
Input and output tokens fall into three groups with different value.
- Output tokens: replies, reasoning, and tool-call arguments. This is new information that drives the next action.
- Repeated input tokens: history sent again—system prompt, tool descriptions, early rounds. Their job is to keep state so the model knows where the task stands.
- New input tokens: tool returns such as file contents, command output, and search hits. They bring the outside world in and often grow fastest.
Most tokens therefore maintain state rather than produce a new answer. The model must see what is done, which results still matter, and what to do next, and all of that rides on the request. Not all repetition is waste: context the next decision depends on must stay. The optimization target is content that does not affect the next decision.
4. Engineering ways to cut cost
1. Slim the fixed prefix
Keep the system prompt to role, rules, and necessary constraints. Load tool descriptions on demand; omit tools the current step will not use. Split long specs into documents the model can read when needed so every round does not carry the full text.
2. Truncate and externalize tool results
Tool results usually dominate new tokens. Command output is often noisy logs; the useful parts are errors and a few key lines. Write the full output to a file or database and return a summary plus a location.
def run_tool(call):raw = execute(call)if len(raw) > RESULT_LIMIT:location = save_to_store(raw)return head_and_tail(raw) + "\nFull output saved at: " + locationreturn raw
Search tools can cap hits and keep titles, key fields, and short snippets.
3. Rolling compression of context
When history exceeds a threshold, compress older rounds into a state summary and keep the last few rounds raw. That turns quadratic growth toward linear.
if estimate_tokens(history) > THRESHOLD:summary = summarize(history[:-KEEP_RECENT])history = [summary] + history[-KEEP_RECENT:]
Compression drops detail. Keep key decisions, open items, changed file paths, and confirmed errors in structured storage instead of relying only on a natural-language summary.
4. Split subtasks and isolate context
The main task can hand a self-contained subgoal to a subtask with its own context. The subtask returns a short conclusion; intermediate steps never re-enter the main history, so main context does not grow with subtask steps. Give the subtask a clear input scope and deliverable so it does not re-explore what the parent already knows.
5. Use prefix cache
Many inference APIs cache identical prefixes and reuse prior compute. Keep the system prompt and tool definitions stable, put them first, and put changing content later. A timestamp or random id at the start of the system prompt changes the prefix every time and busts the cache. Whether cache is available and how it is billed depends on the provider’s documentation.
6. Move memory out of the context window
Plans, progress, and key decisions can live in files or a database. Each step reads only what it needs instead of sending the full history. Memory moves from tokens to storage and the cost shape changes.
7. Drop historical reasoning when it is safe
Chain-of-thought in past turns often helps later actions little. If the task still works, keep conclusions and actions and drop intermediate reasoning. Whether you may delete historical thinking depends on the model API; confirm before you do it.
5. How to tell if token use is reasonable
Track at least:
- Total input vs. output tokens per task
- Average new tokens per round
- Rounds vs. success rate
- Cache hit rate (if the service reports it)
- Share of input tokens from tool results
Use the table below to locate issues.
| Symptom | Likely cause | First move |
|---|---|---|
| Tool results dominate input | Output not truncated | Truncate and externalize tool results |
| Many rounds, input grows fast | Task too long, not split | Subtasks and rolling compression |
| Large fixed cost per round | Huge tool list and specs | Load tools on demand, slim the prompt |
| Low cache hit rate | Prefix changes often | Freeze the prefix, reorder content |
6. Conclusion
Agent token cost comes mainly from resending history to keep state and from streaming in tool results. Cost control means less repetition, dropping content that does not affect the next decision, and putting long-term memory in external storage. Treat each request as a full state snapshot and ask which pieces actually change the next action and which are leftover history. Reviewing requests that way surfaces most avoidable spend.
