Sub topic 1
The Context Window and Its Token Budget
Prerequisites
You should have basic experience using a coding agent in a terminal or editor. You should know common command-line tools, such as grep, sed, and pytest, and understand that a token is a small piece of text.
The Context Window and Its Token Budget
A context window is the working input that a language model can consider for one response. It has a fixed token budget. The budget includes both the conversation sent to the model and space reserved for the model's next response.
Tokens include parts of words, punctuation, code, and whitespace. A source file with many comments or repeated logs can consume more tokens than its line count suggests. When the context becomes full, the agent cannot keep every earlier detail available.
The context window is different from long-term project storage. It is temporary working memory for the current session.
flowchart TD
A[Context window] --> B[Instructions]
A --> C[Conversation history]
A --> D[File contents]
A --> E[Tool output]
A --> F[Response budget]caption: The main parts of an agent's context window alt: A flowchart showing instructions, conversation history, file contents, tool output, and response budget entering the context window
What Actually Fills the Context in an Agent Session
Every turn can add more material:
- System and project instructions.
- Your requests and the agent's replies.
- File contents returned by reads or searches.
- Shell output, test logs, and error messages.
- Plans, decisions, and repeated explanations.
- Tool results that are no longer relevant but remain in history.
A large tool result is often the fastest way to waste context. Reading one relevant function is usually better than reading its entire file. Running tests with a full log is usually worse than stopping at the first failure and keeping the final summary.
How Long Sessions Go Wrong
Crowded contexts cause context degradation. The agent may:
- Forget an earlier constraint.
- Repeat a question that was already answered.
- Use stale code instead of the latest version.
- Produce less precise edits.
- Spend time discussing irrelevant tool output.
- Miss the actual failure in a long log.
These are warning signs, not proof that the model has failed. Restate the current goal, remove irrelevant material, or start a fresh session before making a risky change.
> callout warning > Treat context as a limited working budget. Do not wait until the window is completely full to recover clarity.
Checking How Much Context You Have Used
Use a usage indicator when your agent provides one. Otherwise, estimate from the amount of history, file content, and tool output already included. Check after large reads, broad searches, or long test runs.
A practical rule is to act when responses become repetitive or lose constraints. Compact the session, record the important decisions, and continue with a smaller working set.
Exercise
What should you do after a test command produces hundreds of irrelevant log lines?
Show answer
Keep only the failure summary or the relevant tail, then continue with the smaller result.