
Tokens, cost, and the context window
How an agent’s requests grow each turn, what a run costs, and why the context window misleads.
On the agent compass Stage 1 of 4 · Build the requestModel APIs measure everything in tokens. Your request is counted in them, the reply is counted in them, the size limits are stated in them, and the price list is written per token. For an agent, one consequence matters more than any other. The model remembers nothing between calls, so each request has to resend the conversation from the beginning, and an agent’s conversation never shrinks on its own.
Below, that growth is traced through recorded runs, followed by what it does to cost and to the context window.
Tokens and prices
A token is a piece of text the model treats as one unit. Short, common words are usually one token each; longer words get split into several, and punctuation marks are often tokens of their own. For English text, figure about four characters to a token. The request you send on a call is called the prompt, and the text the model sends back is the completion.
Providers set two prices per million tokens, one for input and a higher one for output, since producing a token is much more work for the model than reading one.
In October 2026 the default model in the companion code, anthropic/claude-haiku-5.5, cost $0.10 per million input tokens and $0.50 per million output tokens.
Each reply comes with a usage report, and the agent should hold on to it:
// Reply is the model's answer to one Request.
type Reply struct {
Message Message // the assistant's text, tool calls, or both
FinishReason string // why the model stopped: "stop", "length", ...
Usage Usage // tokens counted for this call, and its cost
}
// Usage is what one model call consumed.
type Usage struct {
PromptTokens int // everything sent, counted in tokens
CompletionTokens int // everything the model wrote
CachedTokens int // prompt tokens served from the cache
ReasoningTokens int // completion tokens spent on reasoning
Cost float64 // in dollars, as the provider reported it
}
Two fields in that report deserve explanation. Cached tokens are input that matches the opening of a request the provider handled shortly before, and the provider bills them at a discount. Reasoning tokens are text the model writes to work through the problem before giving its answer; they are billed as output even when you never see them. In one recorded chat, a four-line answer used 700 output tokens, and 642 of those were reasoning.
The same question, four prices
A low price per token doesn’t guarantee a low price per answer.
Four inexpensive models were asked the same short question about nil and empty slices, four times on each.
The question was 19 words, but the input counts ran from 29 to 53 tokens, mainly because every provider adds its own formatting around a message.
z-ai/glm-5.3-flash produced three times the output tokens of the default model, mostly reasoning, and the amount of reasoning swung between 193 and 807 tokens across runs of the identical question.
deepseek/deepseek-v4.1-flash had the cheapest input on the price list, yet its median answer cost more than the default model’s, because it wrote longer replies and its output price was higher.
Before picking a model from a price table, run it on your own task and look at the bill.
Every request carries everything
In a chat, each new request is the full history plus one more question. Here are the three requests from a recorded chat:
| Turn | Tokens in | Tokens out | What was sent |
|---|---|---|---|
| 1 | 43 | 122 | System prompt and question 1 |
| 2 | 175 | 253 | Turn 1’s input, answer 1, and question 2 |
| 3 | 439 | 700 | Turn 2’s input, answer 2, and question 3 |
Look at answer 1: you buy it as 122 output tokens in turn 1, then buy it again as input in turn 2, and again in turn 3, and in every turn after. A turn’s cost therefore rises as the conversation lengthens, and the total for a conversation of n turns rises about as fast as n squared.
Three turns hide this; twenty don’t. A 20-turn chat on the default model was recorded as well. By the last turn, a question of roughly twelve words went out inside a 13,785-token request. Over all twenty turns the requests added up to 124,673 tokens, nine times the size of the finished conversation, and turn 20 cost ten times what turn 1 did.
An agent grows faster
People add to a chat a sentence at a time. An agent adds whole tool outputs: each file it opens and each test run it does becomes part of the history, and it may take thirty turns with no person involved. Here are the four requests from a recorded bug fix, in which a model repairs a failing Go test using four tools:
| Call | Tokens in | Tokens out |
|---|---|---|
| 1 | 583 | 94 |
| 2 | 834 | 99 |
| 3 | 1,846 | 245 |
| 4 | 2,164 | 131 |
Call 3 jumps by about a thousand tokens because the reply to call 2 asked to read two source files, and from then on both files ride along in every request.
The more surprising number is call 1.
The task itself is a single sentence of 26 tokens, but the request is 583.
The four tool definitions account for roughly 270 of those.
About 290 more are added by the provider whenever tools are present: it rewrites your definitions into its own format and wraps them in its own usage instructions, which you never see and are billed for on every call.
The overhead depends on the provider.
Measured on the identical first request, deepseek/deepseek-v4.1-flash came to 441 tokens and z-ai/glm-5.3-flash to 317, while openai/gpt-5.6-luna needed only 126, since its provider stores tool definitions in a format more compact than the JSON you send.
On a task this small the overhead hardly shows; the whole run cost $0.0008. Crash investigation is different, because the agent reads much more. (This is the crash-triage agent the companion code builds: it reads crash reports, finds the bug, fixes it, and files an issue.) A batch of 99 crash reports cost $1.33 in a run with no context policy; with a policy that turns on prompt caching (explained below) and clears old tool results once a request passes 60,000 tokens, two runs cost $0.06 and $0.42; the second took 62 turns to the first’s 30, because the same agent took a longer path through the same batch.
Caching changes the bill, not the size
With prompt caching, the provider saves the work it did on the beginning of one request and reuses it, for less money, when a later request begins with the exact same tokens. Agents benefit naturally, since each request is the one before it with a little added at the end.
In the 20-turn chat, openai/gpt-5.6-luna and deepseek/deepseek-v4.1-flash cached without being asked and charged a tenth of the normal rate for most of each request; for deepseek/deepseek-v4.1-flash, the final turn ended up a little cheaper than the first.
The default model supports caching as well, but through OpenRouter you have to request it by putting markers in the request.
One approach adds two cache markers: one at the end of the fixed start every request shares (the system prompt and the task), and one at the end of the whole request, so the next request can reuse this one’s history. On the largest task in the companion code, the difference was:
Measured result
$0.23 for the batch, down from $1.33
With the markers, 95 percent of the input tokens were read from the cache, the cost fell about sixfold, and the agent passed the same 86 of the grader’s 94 checks. The two runs took 35 and 33 turns, so most of the gap comes from what each token cost rather than from different choices by the model. Part of it is the long-context price tier described below: priced at the lower rate throughout, the markers would still have made the batch about four and a half times cheaper.
- Task
- chapter 8’s batch of 99 crash reports from 23 causes, triaged by the crash-triage agent with its whole history in every request, once without cache markers and once with them
- Models
anthropic/claude-haiku-5.5- Runs
- one per condition
- Date
- October 2026
Measured with the companion code
Either way, a cached history costs less while staying exactly as long, and length has a cost of its own, covered next.
The context window, and why it’s the wrong number
The context window is the most tokens a single call can involve, counting the prompt and the reply together. The default model allows 1,000,000; other models go from a few thousand to a few million. Send more than fits and the provider will usually reject the request with an error.
Quality drops long before that limit. A 2023 paper by Liu and others, “Lost in the Middle”, found that models reliably found a fact placed near the beginning or end of a long input and often missed the same fact placed in the middle. In 2025, Chroma’s “Context Rot” report ran 18 models through simple tasks and saw every one of them get less accurate as the input lengthened, well short of its window. Length can raise the price per token too: on the default model, once a request passes 100,000 tokens, the whole request is billed at five times the usual rate, and about half the requests in the batch run with no context policy crossed that line.
So plan around a budget, not the window. A file the agent opened many turns back and doesn’t need any more is still billed on each call, and it still takes some of the model’s attention away from what matters now.
What to do about it
- Count from the first call. Record the usage that comes back with each reply and total it for the run; the ch04 example does it by wrapping the model: a small
metertype that holds the real model, passes each call through, and adds the reply’s usage to running totals. - Bound the spend. A turn limit catches a loop that won’t stop, and a dollar budget checked before every call limits the cost itself.
- Keep tool output small. Whatever a tool returns is resent on every call after it. The
go testtool in the bug fix reduces 1,767 bytes of JSON events to a 158-byte report. - Keep the start of the request stable. The cache discount only applies to an identical prefix, so put instructions and tool definitions first and leave them alone during a run.
- Decide what the model sees. Eventually the history needs rules: drop old turns, clear old tool results, or summarize. That is context management, part of building each request.
Run it
From the code directory of a clone of the companion repository:
$ go run ./ch02
$ go run ./ch04
./ch02 plays back the three-turn chat and prints the tokens and cost of every call.
./ch04 plays back the bug fix and ends with the run’s totals: 4 model calls, 5,427 tokens in, 569 out.
type Reply struct {
Message Message // the assistant's text, tool calls, or both
FinishReason string // why the model stopped: "stop", "length", ...
Usage Usage // tokens counted for this call, and its cost
}
Reply is the model's answer to one Request.
Reply is the model's answer to one Request. type Reply struct { Message Message // the assistant's text, tool calls, or both FinishReason string // why the model stopped: "stop", "length", ... Usage Usage // tokens counted for this call, and its cost }. In ch02/agent/types.go, line 64.
field Message Message
the assistant's text, tool calls, or both
the assistant's text, tool calls, or both field Message Message. In ch02/agent/types.go, line 65.
type Message struct {
Role string // "system", "user", "assistant", or "tool"
Content string // empty when the message has tool calls
ToolCalls []ToolCall // set on assistant messages that call tools
ToolCallID string // on tool messages: the call this answers
IsError bool // set on tool messages that report a failure
}
Message is one entry in the conversation.
Message is one entry in the conversation. type Message struct { Role string // "system", "user", "assistant", or "tool" Content string // empty when the message has tool calls ToolCalls []ToolCall // set on assistant messages that call tools ToolCallID string // on tool messages: the call this answers IsError bool // set on tool messages that report a failure }. In ch02/agent/types.go, line 33.
field FinishReason string
why the model stopped: "stop", "length", ...
why the model stopped: "stop", "length", ... field FinishReason string. In ch02/agent/types.go, line 66.
type string string
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text.
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text. type string string. Documented at pkg.go.dev, builtin.string.
field Usage Usage
tokens counted for this call, and its cost
tokens counted for this call, and its cost field Usage Usage. In ch02/agent/types.go, line 67.
type Usage struct {
PromptTokens int // everything sent, counted in tokens
CompletionTokens int // everything the model wrote
CachedTokens int // prompt tokens served from the cache
ReasoningTokens int // completion tokens spent on reasoning
Cost float64 // in dollars, as the provider reported it
}
Usage is what one model call consumed.
Usage is what one model call consumed. type Usage struct { PromptTokens int // everything sent, counted in tokens CompletionTokens int // everything the model wrote CachedTokens int // prompt tokens served from the cache ReasoningTokens int // completion tokens spent on reasoning Cost float64 // in dollars, as the provider reported it }. In ch02/agent/types.go, line 71.
field PromptTokens int
everything sent, counted in tokens
everything sent, counted in tokens field PromptTokens int. In ch02/agent/types.go, line 72.
type int int
int is a signed integer type that is at least 32 bits in size.
int is a signed integer type that is at least 32 bits in size. type int int. Documented at pkg.go.dev, builtin.int.
field CompletionTokens int
everything the model wrote
everything the model wrote field CompletionTokens int. In ch02/agent/types.go, line 73.
field CachedTokens int
prompt tokens served from the cache
prompt tokens served from the cache field CachedTokens int. In ch02/agent/types.go, line 74.
field ReasoningTokens int
completion tokens spent on reasoning
completion tokens spent on reasoning field ReasoningTokens int. In ch02/agent/types.go, line 75.
field Cost float64
in dollars, as the provider reported it
in dollars, as the provider reported it field Cost float64. In ch02/agent/types.go, line 76.
type float64 float64
float64 is the set of all IEEE 754 64-bit floating-point numbers.
float64 is the set of all IEEE 754 64-bit floating-point numbers. type float64 float64. Documented at pkg.go.dev, builtin.float64.

