Sub topic 4
Checking how much context you have used
This chapter assumes you have used a coding agent in a terminal, know basic shell and git, and know that a model has a limited input size measured in tokens. Here you will learn how to see how full the context window is and when to act on that number.
Why measure at all
A context window has a fixed token budget. The agent does not warn you gently as it fills. Quality drops first, and then the session hits the limit. Checking usage turns a vague feeling ("the agent seems off") into a number you can act on.
Where to read the number
Most agents expose usage in one of three places:
- A slash command, such as
/contextor/usage, that prints a breakdown of tokens used. - A status line or footer that shows a percentage of the window.
- The API response, which reports input and output token counts for each call.
Command names differ between tools. Check your agent's help output for the exact name. A useful breakdown separates the parts you learned about earlier:
| Part | What it contains | Typical behavior |
|---|---|---|
| System prompt and tool definitions | Fixed instructions and tool schemas | Constant size |
| Project instructions | Files loaded at start, such as a rules file | Constant size |
| Conversation history | Your messages and the agent's replies | Grows every turn |
| Tool results | File reads, search output, command logs | Grows fastest |
The constant parts are your fixed cost. If they already take 20 percent of the window, you have 80 percent left for work, not 100.
Estimate when you have no counter
If your tool shows no usage figure, estimate. A rough rule for English text and code is about 4 characters per token. You can approximate the size of a file before you ask the agent to read it:
import sys
from pathlib import Path
def estimate_tokens(path):
text = Path(path).read_text(encoding="utf-8", errors="replace")
return len(text) // 4
if __name__ == "__main__":
for name in sys.argv[1:]:
print(f"{name}: about {estimate_tokens(name)} tokens")Run it as python estimate.py src/config.py src/server.py. The result is approximate. Real tokenizers vary by language and content, and code with long identifiers or minified text can differ a lot. Use the figure to compare files, not as an exact count.
Decide using thresholds
A number only helps if you attach a decision to it. These thresholds are a starting point. Adjust them to your model and task.
- Under 50 percent: keep working. Check again at natural breaks.
- 50 to 75 percent: finish the current subtask, then compact or write notes.
- Over 75 percent: do not start new work. Save progress to a file and begin a new session.
Act early. Compaction itself needs room to run, and degradation often starts before the window is full.
Common errors
- Checking only when something breaks. By then the context is already crowded. Check at the end of each subtask.
- Ignoring the fixed cost. Many tool definitions or a long rules file can consume a large share before you type anything.
- Trusting one reading. One large file read can add thousands of tokens in a single turn. Check again after big reads or long test runs.
- Confusing output limit with window size. The window covers input and output together. A reply budget is separate from the total.
Exercise
Your agent reports 62 percent usage, and you are halfway through a subtask. Using the thresholds in this chapter, what should you do?
Show answer
Finish the current subtask first, since the usage is in the 50 to 75 percent range. Then compact the session or write progress notes to a file. Do not start a new subtask in the same crowded context.
Exercise
A file is 40,000 characters long. Roughly how many tokens will it add when the agent reads it in full, and why is the figure only an estimate?
Show answer
About 10,000 tokens, using 4 characters per token. It is an estimate because tokenizers differ, and code or non-English text can produce more or fewer tokens per character.