Lesson 59.1 · 1 of 3 · 4 min read · Reviewed 24 Nov 2026
Where tokens go
By the end, you can
- Break down where tokens go in a request.
- Measure token use before optimising.
Before trimming, find out what’s big.
The usual parts
| Part | Typical size | Notes |
|---|---|---|
| System prompt | Hundreds to thousands | Same every turn; cacheable |
| Tool definitions | Hundreds per tool | Same every turn; cacheable |
| Conversation history | Grows each turn | Often the biggest in long chats |
| Documents | Can be huge | Retrieve just in time (58.2) |
| Tool results | Often huge in agents | Trim and clear (58.3) |
| Output | Your answer | Priced higher than input |
Measure
- Every response has a usage field with input and output tokensOutput tokens: The tokens the model generates in its reply, including any tool calls and thinking., plus cache readsCache hit: A request whose prompt prefix was found in the cache, so it is read cheaply instead of processed again. and writes.
- The token countingToken counting: Measuring how many tokens a request will use before sending it, for example with a token-counting endpoint. endpoint tells you a request’s size before you send it.
Log these per turn. A chart of input tokensInput tokens: The tokens you send in a request: system prompt, tool definitions, conversation history and attached content. over a run shows exactly when and why context grows.
Leave room for output
Input plus max_tokens must fit the window. If the input fills nearly all of it, the answer gets cut off with stop_reason: "max_tokens".
Key points
- A request's tokens come from the system prompt, tool definitions, history, documents and tool results.
- Count before cutting: the token counting endpoint and the usage field show what each part costs.
- Output tokens cost more than input, and max_tokens must leave room for the answer.
Break a request down by token cost
Estimate the tokens in each part of an agent request, find the biggest part, and check there is room left for the answer.
You will have: A budget table by part, the biggest part named, and a fit check against the window with max_tokens reserved.
Steps
- Read the REQUEST parts in the starter (text is generated to realistic sizes).
- Estimate tokens per part at about 4 characters per token.
- Print a table sorted by size with each part's share.
- Check that input plus max_tokens fits in the 200,000-token window and print how much room is left.
- Halve the biggest part and print the new total.
Done when
- The table lists all six parts with shares adding to 100%.
- The biggest part is named as tool results.
- The fit check prints the room left after reserving max_tokens.
Open in the Playground No API key needed.
Starter code (Python)
REQUEST = {
"system prompt": "You are a careful research assistant. " * 120,
"tool definitions": '{"name": "search", "description": "Search the web"} ' * 260,
"documents": "Clause text from the contract. " * 4000,
"history": "user and assistant turns ... " * 5000,
"tool results": "search hit with snippet and url ... " * 12000,
"new message": "Summarise the termination clauses.",
}
WINDOW, MAX_TOKENS = 200000, 8000
def tokens(text):
return max(1, len(text) // 4)
# TODO: build a list of (part, tokens) sorted by size, print each with its share,
# then check input + MAX_TOKENS against WINDOW
Show a solution
REQUEST = {
"system prompt": "You are a careful research assistant. " * 120,
"tool definitions": '{"name": "search", "description": "Search the web"} ' * 260,
"documents": "Clause text from the contract. " * 4000,
"history": "user and assistant turns ... " * 5000,
"tool results": "search hit with snippet and url ... " * 12000,
"new message": "Summarise the termination clauses.",
}
WINDOW, MAX_TOKENS = 200000, 8000
def tokens(text):
return max(1, len(text) // 4)
parts = sorted(((name, tokens(t)) for name, t in REQUEST.items()), key=lambda x: -x[1])
total = sum(t for _, t in parts)
for name, t in parts:
print("%-17s %7d %5.1f%%" % (name, t, 100 * t / total))
print("input total:", total)
room = WINDOW - total - MAX_TOKENS
print("fits with max_tokens reserved:", room >= 0, "| room left:", room)
print("biggest part:", parts[0][0])
print("total if it were halved:", total - parts[0][1] // 2)