Sub topic 2
The context window as working space
This chapter assumes you can write basic Python, have called a chat-style LLM API, and know that an agent is an LLM running in a loop with tools. The previous chapter showed that each LLM call is stateless. Here you will see where the model "sees" information on each call: the context window.
What the context window is
The context window is the maximum amount of text, measured in tokens, that a model can read and write in one call. A token is a chunk of text, often a word fragment. English text averages roughly three-quarters of a word per token, but the exact count depends on the tokenizer.
Everything the model can use for a single call must fit in this window. Nothing outside it exists for the model. This is why the window works like a desk: it is the only surface where the model can lay out material, and it has a fixed size.
What competes for the space
The window is shared. Input and output draw from the same budget, so a long prompt leaves less room for the answer.
| Part of the window | Typical content | Who controls it |
|---|---|---|
| System message | Role, rules, tool descriptions | You |
| Conversation history | Earlier user and assistant turns | Your buffer logic |
| Retrieved memories | Facts pulled from long-term storage | Your retrieval code |
| Tool results | Search output, file contents, API responses | Tools, often unpredictably |
| Model output | The reply being generated | Reserved by you |
Tool results are a common source of surprise. One large web page or log file can use more tokens than the entire conversation so far.
The token budget
A token budget is the number of tokens you allow each part of the prompt to use. You set it below the model's hard limit so there is always room to answer.
MODEL_LIMIT = 8000 # the model's context window
RESERVED_OUTPUT = 1000 # room kept for the reply
PROMPT_BUDGET = MODEL_LIMIT - RESERVED_OUTPUT
system_tokens = 400
memory_tokens = 300
history_budget = PROMPT_BUDGET - system_tokens - memory_tokens
print(history_budget) # 6300 tokens left for conversation and tool resultsIf you ignore the budget, the API either rejects the request with a context-length error or, in some setups, silently cuts text. Silent cuts are the worse case, because the agent then behaves as if it forgot something with no visible error.
A bigger window is not a full answer
Larger windows help, but they do not remove the need for memory design:
- Cost and latency grow with prompt size, and you pay for the full prompt on every call.
- Models can use information less reliably when it is buried in a very long prompt.
- The window still empties when the session ends. It holds nothing across sessions.
Where this leads
The window is the working space, and your code decides what goes into it. The next chapters build on this in two ways. Short-term memory keeps the recent conversation in the window. Long-term memory keeps facts outside the window and brings back only the relevant ones.
Exercise
A model has an 8,000-token window. Your system message uses 500 tokens, retrieved memories use 400, and you reserve 1,000 tokens for the reply. How many tokens remain for conversation history and tool results? What happens if a tool returns 7,000 tokens?
Show answer
8,000 - 500 - 400 - 1,000 = 6,100 tokens remain. A 7,000-token tool result exceeds that budget, so the call fails with a context-length error or text gets cut. You should truncate or summarize the tool output before adding it to the prompt.
Exercise
Why does a larger context window not remove the need for long-term memory?
Show answer
The window is rebuilt on each call and is empty at the start of a new session, so nothing persists in it. A larger window also raises cost and latency, and the model may use details in a very long prompt less reliably.