AI for beginnersGuide
Why AI agents use so many tokens (a 20-step example)
An AI agent resends its history, tool list and tool output on every step. See where the tokens go, with a 20-step worked example.

On this page
You ask an agent one question, it works for a few minutes, and the usage page shows hundreds of thousands of tokens. Nothing is broken. An agent is a loop, and each turn of the loop resends everything said so far (the tool list, the history, whatever the tools returned) as input tokens. The answer it writes is tiny. What it reads again and again is not.
What an agent is, in one picture#
A chat model answers once. An agent is the same model inside a loop with tools: read a file, search the web, run a command. And the model remembers nothing between calls. Picture an assistant who forgets everything each time they hang up: every time they call back, you read the notes from the top, then add one line. That is how the API works.
Anthropic's tool-use docs (checked 26 October 2026) describe the loop like this:
- Your app sends the user message and the list of tools.
- The model replies with
stop_and areason: "tool_ use" tool_useblock: which tool, with which arguments. - Your app runs the tool and sends a second request. It contains the earlier messages, the model's tool call, a
tool_resultblock and the same tools list. - The model answers, or asks for another tool, and the loop goes round again.
The same page says tool-use requests are billed on the total input sent, the tools parameter included, plus the output. So step 3 is never "just the new part": it is the whole conversation again.
Request 1 tools + system + task
Request 2 tools + system + task + call 1 + result 1
Request 3 tools + system + task + call 1 + result 1 + call 2 + result 2
...It is the same mechanism as in why a long chat costs more, only faster. A chat grows by one message when you type. An agent grows by two blocks on every step, and nobody types anything.
Three things ride along on every step#
The tool list. Names, descriptions and parameter schemas of every tool go into every request, even when the model uses none of them this turn. Docs add a small extra: with tools on, the API adds its own tool-use prompt, 286 tokens on Claude Sonnet 5.5, Opus 5.5 and Haiku 5.5, and other models have other counts.
The history. Your task, the model's earlier tool calls, its notes.
Tool output. This is the sneaky one. A file the agent read, a page of search results, a test log: once it lands in the history, it is paid for again on every later step.
A token is a small piece of text, roughly a short word or part of one, and each of these three is counted in them.
A 20-step run, counted#
Say you run a small coding agent with three tools (read a file, search, run tests) and give it a task that takes 20 steps. Take these assumptions:
- tool definitions: 1,500 tokens, system prompt: 500, your task: 200, so every request starts at 2,200 tokens;
- on each step the model writes a tool call of 150 tokens, and the tool returns 800 tokens;
- both join the history, so each step adds 950 tokens to the next request.

To get the run total, add up all 20 requests. The base of 2,200 is sent 20 times, and the rounds are carried over by later steps: step 1 carries none, step 2 one, and so on up to 19, which is 190 rounds in all.
20 × 2,200 = 44,000
950 × 190 = 180,500
44,000 + 180,500 = 224,500 input tokens, and 20 × 150 = 3,000 output tokens.
The agent wrote 3,000 tokens and read 224,500. Output tokens cost more each than input tokens (see input vs output tokens), but there are so few of them that the input side still decides the bill. Here is that run on current prices:
| Model | Input $/1M | Output $/1M | Cost | OpenRouter |
|---|---|---|---|---|
| GLM-5.3-Flash | $0.02 | $0.04 | $0.0046 | $0.0095 |
| DeepSeek-V4-Flash | $0.015 | $0.10 | $0.0037 | $0.0111 |
| MiniMax-M2.7 | $0.05 | $0.15 | $0.0117 | $0.071 |
224,500 input and 3,000 output tokens in total. Prices per 1M tokens are current and change together with our tariffs; the OpenRouter column shows reference prices for the same workload.
On DeepSeek-V4-Flash one such run comes to $0.0037, on GLM-5.3-Flash to $0.0046. A hundred runs is 22,450,000 input and 300,000 output tokens: $0.367 on DeepSeek-V4-Flash. The table above counts input and output together, and in total the models there come out cheaper than OpenRouter.
The step count matters more than it looks. A 5-step run with the same assumptions:
5 × 2,200 = 11,000
950 × 10 = 9,500
11,000 + 9,500 = 20,500 input tokens. Four times the steps gave about eleven times the input, because every new step also re-reads all the old ones.
Where to cut: four habits#
One tool result is the easiest place to save. Say a test run on step 3 returns a 4,000-token log. It is then resent by the 17 requests that follow:
- before: 4,000 × 17 = 68,000 input tokens carried by that one log;
- after, if the tool returns only the 40 relevant lines (around 400 tokens): 400 × 17 = 6,800.
Same task, same answer, a tenth of the input from that one result. That gives four habits:
- Return less from tools. Lines around the error, not the whole log; the function, not the whole file. There is a longer take in keeping tool output out of context.
- Give the agent fewer tools. Every definition rides every request.
- Cap the steps and start a fresh task when the old history no longer matters.
- Add up the usage of every step, and stop the run at a budget you chose in advance. For the estimating side, see how to estimate the cost of an AI agent.
Count your own run#
Every response reports how many tokens it took in and put out, in its usage field. Sum them over all steps and you have the real numbers for your agent instead of my assumptions. A sketch with the OpenAI Python SDK pointed at CheapTokens (messages and tools are your agent's own):
from openai import OpenAI
client = OpenAI(base_url="https://api.cheaptokens.dev/v1", api_key="YOUR_API_KEY")
total_in = total_out = 0
for step in range(20): # your agent loop
resp = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4-Flash-0731",
messages=messages,
tools=tools,
)
total_in += resp.usage.prompt_tokens
total_out += resp.usage.completion_tokens
# ...run the tool, append the result to messages
print(total_in, total_out)The two totals plug straight into the cost table above. For long histories like this, DeepSeek-V4-Flash gives you a 380K context window and up to 32,768 output tokens per answer, at one price at any hour; GLM-5.3-Flash has a 390K context and up to 8,192 output tokens. Setup is in the docs, the current price of every model is on the pricing page, and the model has its own page, cheap DeepSeek.



