Most agent bills get diagnosed as an output problem. In a tool-heavy agent, tool calling token cost is usually an input problem: the same tool schemas and the same tool results get re-sent on every turn, and input tokens are billed every time you send them.
This post works through the arithmetic for one 30-turn session, shows how to measure your own session instead of guessing, and lists the changes that actually move the number. The figures are illustrative and every assumption is stated — this is not a benchmark. It is an AtmoRouter-authored post, and the rates come from the live catalogue at writing time; rates change, so the model page stays authoritative.
Why tool definitions dominate a tool-heavy agent
Tool calling on AtmoRouter is passed through to the model. The gateway does not execute your functions — your application parses the arguments, runs the function, and appends the result as a message. That round trip is where the cost model gets awkward.
There is no partial send: each request carries the system prompt, the full tools array, and the entire message history. A tool you called once is not paid once, because its schema is billed again on every later turn, and the result you appended is billed on every turn after that. Some providers also add their own tool-use preamble on top of your JSON, which is more input you did not write and cannot trim.
That is why an agent with forty tools feels expensive even when its answers are short. The expensive part is the scaffolding, and the scaffolding is re-billed at full input rate.
The arithmetic of a 30-turn session
State the assumptions before you state the bill, or the bill is meaningless. A deliberately plain agent:
- System prompt: 900 tokens.
- 42 tools at roughly 130 tokens of JSON schema each: about 5,500 tokens.
- Transcript grows by about 900 tokens per turn (one tool result, one short assistant message).
- 30 turns, 250 output tokens each.
Turn n therefore sends 6,400 + 900n input tokens, and the session totals 610,500 input tokens and 7,500 output tokens. Against current catalogue rates:
| Model (from input / output per Mtok) | Input cost | Output cost | Session total | |---|---|---|---| | zai/glm-5.3 — $0.0035 / $0.011 | $0.0021 | $0.0001 | ~$0.002 | | ali/deepseek-v4.1-flash — $0.038625 / $0.1545 | $0.0236 | $0.0012 | ~$0.025 | | cc/claude-sonnet-5-5 — $0.295 / $1.475 | $0.1801 | $0.0111 | ~$0.19 | | oai/gpt-6-sol — $1.505 / $7.525 | $0.9188 | $0.0564 | ~$0.98 |
Two things fall out of that table. Input is 94–96% of the session cost on every row, so the output column is almost decoration. And the tool definitions alone account for 165,000 tokens, 27% of all input, before a single tool result is counted.
The compounding is the part people underestimate. A 4,000-token file dump at turn three sits in the transcript for 27 more turns, so it is billed 28 times. Truncating it to 400 tokens does not save 3,600 tokens; it saves roughly 100,000.
Measuring your own turns
Do not model this from a tokenizer guess. Read usage from the response you already pay for. This computes a per-turn cost from the reported counts, treating cached input at 10% of the input rate where the model supports it:
pythonimport os
from openai import OpenAI
client = OpenAI(api_key=os.environ['ATMOROUTER_API_KEY'],
base_url='https://atmorouter.dev/v1')
# Your real registry goes here; one tool keeps the example runnable.
TOOLS = [{
'type': 'function',
'function': {
'name': 'list_files',
'description': 'List files in a directory',
'parameters': {
'type': 'object',
'properties': {'path': {'type': 'string'}},
'required': ['path'],
},
},
}]
def turn_cost(response, in_rate, out_rate, cache_ratio=0.10):
# in_rate / out_rate are USD per single token, e.g. 0.295 / 1e6.
usage = response.usage
details = getattr(usage, 'prompt_tokens_details', None)
cached = (getattr(details, 'cached_tokens', 0) or 0) if details else 0
fresh = usage.prompt_tokens - cached
cost = (fresh * in_rate
+ cached * in_rate * cache_ratio
+ usage.completion_tokens * out_rate)
return {'input': usage.prompt_tokens, 'cached': cached,
'cached_share': round(cached / max(usage.prompt_tokens, 1), 3),
'usd': round(cost, 6)}
resp = client.chat.completions.create(
model='cc/claude-sonnet-5-5',
messages=[{'role': 'user', 'content': 'List the files in ./src'}],
tools=TOOLS,
)
print(turn_cost(resp, 0.295 / 1e6, 1.475 / 1e6))Log input, cached_share and usd per turn and plot them. If cached_share stays at 0.0 across turns you have a caching bug, not a pricing problem, and no model swap will fix it. Non-streaming responses also return an X-Atmorouter-Cost header with the exact deduction, which is a cheap cross-check on your own arithmetic. For streamed turns that header cannot exist, since it cannot be added after an SSE response has begun, so consume the reported stream usage and reconcile against the settled usage log.
Five changes that move the number
- 1.Prune the registry per task. Going from 42 tools to 12 drops the schema block from about 5,500 to about 1,600 tokens: 117,000 tokens across the session, roughly 19% of input, with no behaviour change for tasks that only ever used twelve.
- 2.Collapse near-duplicates into one tool with an `action` enum.
read_file,read_range,read_headandread_tailare one tool with four enum values, not four schemas billed thirty times. - 3.Keep the `tools` array byte-stable and identically ordered. Same definitions, same order, same serialization every turn. Renaming a description or reordering entries breaks the prefix and forfeits the discount for the whole session.
- 4.Return handles instead of payloads. Send the path, the line range and a short summary; keep the 40 KB file on disk. Tool results grow fastest and compress hardest.
- 5.Compact the middle of the history. Drop or summarize tool results older than the last few turns, and cap the window explicitly. A transcript that grows forever is a subscription to every earlier mistake.
Caching only pays when the prefix is byte-identical
Cached input is billed at 10% of the input rate where the model supports it. Applied conservatively to just the tool block, and counting turn one as a cache miss, the session above drops from 610,500 billable-equivalent input tokens to about 467,000, which is roughly $0.70 instead of $0.92 of input on oai/gpt-6-sol: about 24% off, before any pruning.
Three things break it. Anything volatile in the prefix (a timestamp, a request ID, a user name in the system prompt) invalidates every turn. Tool definitions must match exactly, including ordering, because caching works on the prefix and the tools sit inside it. And some providers document a minimum cacheable length, often 1,024 tokens, below which nothing is cached at all, silently; parallel requests fired together also all miss, since the entry only exists once the first response starts.
FAQ
Are tool results billed as input tokens?
Yes. A tool result is a message in the prompt, so it is billed on the turn that carries it and on every turn after it while it stays in the history.
Does JSON mode change the cost?
response_format and strict schemas are forwarded to the model, and support is model-dependent, so a forwarded parameter is not a gateway guarantee. Where a model honours a schema it may add instruction tokens of its own, so measure both variants on the model you actually route to.
What if the model answers instead of calling a tool?
Handle both branches, because some models do exactly that. Retrying with a sharper instruction costs another full input pass over the same prefix, which is why a cheap validation step usually pays for the first avoided retry.
The fastest win in production is measurement: log cached_share for a day, then prune the registry. The caching deep dive is in prompt caching for AI agents, and current per-model rates are on the models page, so check it before you budget, since these rates refresh.