Sub topic 4
Checking How Much Context You Have Used
This chapter assumes you have used a coding agent in a terminal or editor, can run basic shell and git commands, and know that a model reads a limited number of tokens at once. It covers how to see your context usage, how to read the numbers, and when to act on them.
Why measure at all
The context window is a fixed budget. Every instruction, file read, command result, and message you exchange spends part of it. If you don't look at the balance, you find out it is low only when the agent starts forgetting things or the tool compacts the session on its own schedule.
Measuring turns "the agent seems off" into a number you can act on. A session at 30% needs no attention. A session at 75% needs a decision.
What the usage number includes
Most agents report usage as tokens used out of the model's window, often also as a percentage. A typical breakdown has these parts:
| Part | What it contains | Changes during a session? |
|---|---|---|
| System prompt | The agent's built-in rules and behavior | No |
| Tool definitions | Descriptions of each tool the agent may call | Only if you add or remove tools |
| Project instructions | Files such as a rules or memory file loaded at start | Rarely |
| Conversation | Your messages and the agent's replies | Grows every turn |
| Tool results | File contents, search hits, command output | Grows fastest |
The first three are a fixed cost you pay before typing anything. The last two are the variable cost, and tool results are usually the largest share. A single unbounded test log or a whole-file read can cost more than twenty chat messages.
Ways to check usage
The exact command differs by tool, so look in your agent's documentation for the equivalent of each of these:
- A slash command that shows usage. Many agents have a command, often named something like
/contextor/usage, that prints a breakdown by category. This is the most useful form because it shows where the tokens went, not only the total. - A status line or footer. Some agents show a running percentage at all times. Check it when you finish a subtask, not only when something feels wrong.
- Compaction warnings. Some tools warn you when usage passes a threshold. Treat this as a late signal. By then you have less room to choose how to compact.
- Logs or API usage fields. If you build your own agent loop, the model API returns token counts with each response. You can log them yourself.
If your tool has none of these, you can estimate. A rough rule for English text and code is about four characters per token, so a 40,000-character file costs on the order of 10,000 tokens. Treat this as a guide, since code with long identifiers or unusual symbols often costs more.
Reading the breakdown
When you have a category breakdown, ask three questions.
- How large is the fixed cost? If system prompt, tool definitions, and instructions already take a large share of the window, you have less room for work than the window size suggests. Many connected tool servers each add definitions, so disconnecting ones you don't need can recover space.
- What is the largest variable item? If tool results dominate, the fix is in how the agent reads and runs things (see the chapters on targeted reading and output control). If conversation dominates, the fix is shorter exchanges or compaction.
- How fast is it growing? Note the percentage at the start of a subtask and again at the end. The difference is the cost of that kind of work, and it lets you predict whether the next subtask fits.
Measuring growth yourself
If your tool does not report per-step cost, you can build a rough measure. This script estimates tokens for a list of files using the four-characters-per-token rule, so you can compare the cost of reading candidates before you ask the agent to read them.
import sys
from pathlib import Path
CHARS_PER_TOKEN = 4 # rough average; real tokenizers vary
def estimate_tokens(path):
text = Path(path).read_text(encoding="utf-8", errors="replace")
return len(text) // CHARS_PER_TOKEN
def main(paths, window=200_000):
total = 0
for p in paths:
tokens = estimate_tokens(p)
total += tokens
print(f"{tokens:>8} {p}")
share = 100 * total / window
print(f"{total:>8} total (~{share:.1f}% of a {window:,}-token window)")
if __name__ == "__main__":
main(sys.argv[1:])Run it as python estimate.py src/config.py src/loader.py. The window size is a parameter because it depends on your model. Set it to the value your agent reports.
Thresholds and what to do at each
There is no universal correct number, but a working set of thresholds makes the decision routine instead of ad hoc. The percentages below are starting points to adjust after you see how your own tool behaves.
| Usage | State | Action |
|---|---|---|
| Under 40% | Comfortable | Keep working. Check at subtask boundaries. |
| 40% to 60% | Watch | Prefer targeted reads. Avoid pasting large output. |
| 60% to 80% | Act | Finish the current subtask, then compact or write notes. |
| Over 80% | Late | Stop starting new work. Save notes and compact or start fresh. |
Quality can fall before the window is full, so the thresholds are set well below 100%. Degradation in long contexts is gradual, and the room needed for the agent's own reply and for the compaction summary also has to fit.
Common errors
- Checking only when something breaks. By the time the agent contradicts an earlier decision, the context has been crowded for a while. Check at subtask boundaries instead.
- Reading the total and ignoring the breakdown. A total of 70% means different things if 40% is tool results or if 40% is fixed overhead. Only the first is fixable by changing how you work.
- Trusting an estimate as exact. Character-based estimates can be off by a wide margin for code, non-English text, or minified files. Use them to rank options, not to plan to the last token.
- Forgetting the reply budget. The agent needs room to write its answer and to call tools. A session at 95% may fail on a large edit even though it technically fits.
- Leaving unused tool servers connected. Their definitions count on every turn, whether or not the agent calls them.
- Assuming a percentage is comparable across models. A window of 200,000 tokens and one of 1,000,000 tokens put 60% at very different absolute sizes. Degradation depends on how much content there is, so the same percentage can be safer on a larger window. Learn the behavior of the model you use.
A short check routine
- Record the usage figure when you start a task.
- After each subtask, check usage and note the change.
- If the largest category is tool results, tighten how output is read before continuing.
- Compare the figure with your thresholds and take the listed action.
- If you cross into the Act band, write down the decisions so far before you compact, so you can confirm the summary kept them.
Exercise
Your agent reports 130,000 of 200,000 tokens used. The breakdown shows 25,000 in fixed overhead, 15,000 in conversation, and 90,000 in tool results. What is the usage percentage, which band does it fall in using the table above, and which category should you change first?
Show answer
130,000 / 200,000 is 65%, which is in the 60% to 80% "Act" band. Tool results are the largest category at 90,000 tokens, so change how the agent reads and runs things first, for example with capped searches, line-range reads, and truncated test output. Then finish the current subtask and compact or write notes.
Exercise
A teammate says their session is safe because the agent shows only 45% usage, but the agent has started repeating a question you already answered. What should you do, and why is the percentage alone not enough?
Show answer
Inspect the contents with the breakdown view. Usage measures size, not quality, so a session can degrade at moderate usage if it holds a lot of irrelevant or stale material, or if the early decision has been buried. Write the decision into a notes file, compact or start a fresh session with that file, and remove noisy content such as large pasted logs.