Sub topic 2
The context window as a shared budget
This chapter assumes you can read basic Python and know that a model reads and writes tokens. It also assumes you have used a coding agent. It builds on the earlier chapter on what a token is.
What the window limits
The context window is the maximum number of tokens a model can handle in one request. Many people think of it as an input limit only. It is a limit on input and output together. The prompt you send and the reply the model writes must fit inside the same window.
Everything the model sees on a turn competes for that space:
- The system prompt and tool definitions.
- The conversation history, including earlier tool calls and their results.
- New tool output, such as a file read or a test log.
- The model's reply, including any reasoning tokens it produces before answering.
Reasoning tokens are covered in a later chapter. For now, note that they usually count against the window too.
Checking the budget in code
Providers let you cap the reply with a maximum output setting. Some check that the input plus this cap fits in the window, and some reject the request if it does not. The exact behavior differs by provider, so check the docs for the one you use. A safe habit is to reserve room for the reply before adding anything else.
def remaining_budget(window, system, history, tool_output, max_output):
"""Tokens left in the window after reserving space for the reply.
A negative result means the request does not fit.
"""
used = system + history + tool_output
return window - used - max_output
# Placeholder sizes in tokens.
print(remaining_budget(200_000, 5_000, 100_000, 30_000, 16_000)) # 49000
print(remaining_budget(200_000, 5_000, 120_000, 60_000, 16_000)) # -1000The second call shows how one large tool result can push a session over the limit even though the history alone fit comfortably.
Common errors
Treating the window as free space. A 200,000-token window does not mean 200,000 tokens are cheap to use. Every token you place in the context is billed as input on each turn that includes it. Window size sets a ceiling. Your bill depends on how much of the window you actually fill, and how many times.
Forgetting the reply. If you fill the window with input, the model has little room to answer. A long edit or a long reasoning chain can be cut off. Some providers return a truncated reply with a stop reason such as a length limit. Always check that stop reason before you treat a reply as complete.
Trusting your own token estimates. Different models split text into tokens differently, so the same file can have different counts on different models. Use the token count the provider reports, or the provider's counting tool, when you are close to the limit.
Assuming quality stays flat as the window fills. Models can handle large contexts, but long, cluttered context can make them less accurate at picking out the detail that matters. A full window is a cost problem and often a quality problem as well.
Why this matters for agents
A single request rarely hits the limit. An agent loop does, because each turn appends a tool call and its result to the history. The window acts as a hard cap on how long a session can run before something has to be dropped or summarized. The cost of the loop is a separate concern, covered in the next chapters.
Treat the window like a budget with fixed categories. Decide how much the system prompt may take, how large a single tool result may be, and how much to reserve for output. Then enforce those limits in the agent's code instead of discovering them when a request fails.
Exercise
A model has a 128,000-token window. Your system prompt and tools take 6,000 tokens, the history is 90,000 tokens, and a test run just returned a 25,000-token log. You set the maximum output to 8,000 tokens. Does the next request fit? By how much?
Show answer
No. Input is 6,000 + 90,000 + 25,000 = 121,000 tokens. Adding the 8,000-token output reserve gives 129,000, which is 1,000 over the 128,000 window. Truncating the log or lowering the output cap would fix it.