Lesson 3.1 · 1 of 4 · 4 min read · Reviewed 24 Nov 2026
What counts against the context window
By the end, you can
- List what counts against the context window.
- See why agents fill it fast.
The context windowContext window: The maximum number of tokens a model can consider at once, counting the input and the output it generates. is the most text, measured in tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id., that one request can hold: your input plus the model’s output.
+------------------------------------------------------------+
| system prompt | tool definitions | messages ... | output |
| 1,200 | 2,400 | 18,000 | <= max |
+------------------------------------------------------------+
total must fit the window
What counts
| Item | Often forgotten? |
|---|---|
| System prompt | No |
| Tool definitions (names, descriptions, schemas) | Yes: many tools add up |
| Conversation history | No, but it grows every turn |
| Tool results | Yes: one file read can be thousands of tokens |
| Documents and images | Sometimes |
| Thinking (if enabled) | Yes |
| The reply | Yes: output shares the window |
Each call starts fresh
The API doesn’t remember earlier calls. To continue a conversation you send the history again, so a 30-turn chat sends all 30 turns on turn 31. That’s why long sessions cost more per turn and eventually need trimming (Part 11).
Agents fill it fastest
An agent that reads files, runs searches and calls tools can add tens of thousands of tokens in a few steps. Context engineeringContext engineering: Deciding what goes into the model’s context on each call: instructions, retrieved facts, tool results, memory and history. (chapter 58) is mostly about controlling this.
Key points
- The context window holds everything in one request plus the reply: system prompt, tool definitions, messages, tool results, images, documents and the output.
- Each turn resends the whole conversation, so a long chat re-reads its history every time.
- Tool definitions and big tool results are easy to forget and often the largest items.
Budget a request against the window
Add up every part of a request, including reserved output, and trim it until it fits the context window.
You will have: A budget table for one request and a trimming step that brings it under the window.
Steps
- Estimate tokens for each part of the request from its character count.
- Write total() so it includes every part and the reserved output.
- Check whether the request fits the 200,000-token window.
- Drop the largest documents one at a time until it fits.
Done when
- The first check reports fits: False.
- total() includes the 8,000 reserved output tokens.
- After trimming, the request fits with the smallest number of documents dropped.
Open in the Playground No API key needed.
Starter code (Python)
WINDOW = 200_000
def estimate(chars):
return chars // 4 # rough rule for English prose; use a counting endpoint for real numbers
request = {
"system prompt": estimate(4_800),
"tool definitions": estimate(20_000),
"history": estimate(120_000),
"documents": [estimate(c) for c in (300_000, 240_000, 180_000, 60_000)],
}
MAX_TOKENS = 8_000
def total(req):
# TODO: add up every part (documents is a list) AND the reserved output MAX_TOKENS
return 0
print(f"total {total(request):,} of {WINDOW:,} -> fits: {total(request) <= WINDOW}")
# TODO: drop the largest documents one at a time until the request fits, then print the new total
Show a solution
WINDOW = 200_000
def estimate(chars):
return chars // 4 # rough rule for English prose; use a counting endpoint for real numbers
request = {
"system prompt": estimate(4_800),
"tool definitions": estimate(20_000),
"history": estimate(120_000),
"documents": [estimate(c) for c in (300_000, 240_000, 180_000, 60_000)],
}
MAX_TOKENS = 8_000
def total(req):
return sum(sum(v) if isinstance(v, list) else v for v in req.values()) + MAX_TOKENS
for part, tokens in request.items():
print(f"{part:17} {sum(tokens) if isinstance(tokens, list) else tokens:>8,} tokens")
print(f"{'output (reserved)':17} {MAX_TOKENS:>8,} tokens")
print(f"total {total(request):,} of {WINDOW:,} -> fits: {total(request) <= WINDOW}")
# Trim: drop the largest documents first until it fits.
docs = sorted(request["documents"])
while total(request) > WINDOW and docs:
dropped = docs.pop()
request["documents"] = docs
print(f"dropped a {dropped:,}-token document")
print(f"after trimming: total {total(request):,} -> fits: {total(request) <= WINDOW}")