Track 01 · Models & contextLesson 07 of 10
Context on a budget: what to keep, compress and cut
Fit the evidence, the history and the instructions inside a token and cost ceiling.
- Lesson library · reference
- 35 min read
- 60 min hands-on
- Halden Freight
- Pro
What you'll be able to do
01- Explain why a context window is a budget with diminishing returns, not a capacity to fill.
- Design a per-turn allocation across system prompt, tools, evidence, history and scratch, with explicit priorities and caps.
- Implement pruning (stale tool results, duplicate evidence) and threshold-triggered compaction that keeps the facts that must survive verbatim.
- Design a prompt layout that maximises cache reuse, and estimate the saving from provider pricing principles.
- Diagnose a cost or quality regression from per-turn token accounting.
Before you start
- Tokens, context windows and why answers go wrong: tokenisation, window limits, and position effects such as "lost in the middle".
- System prompts that survive a regulated rollout: what belongs in the stable prefix.
- Typed output: schemas, validation and tool calls: tool definitions and results are tokens too.
The short version
Every request re-sends everything the model needs: instructions, tool definitions, evidence and the conversation so far. That is your context, and it is a budget. Spend too little and the model lacks the facts; spend too much and you pay for tokens that make answers worse, slower and more expensive. Context engineering is deciding, every turn, what goes in, in what order, and what comes out. You allocate by priority, prune what has gone stale (old tool output, duplicate chunks), compact long history into a summary that keeps IDs, numbers and commitments verbatim, fetch detail just in time instead of stuffing it up front, and put everything static first so the provider can cache it. Then you measure it per turn, because context bloat is how agents that were fine in week one become slow, expensive and wrong in month three.
- The window is a ceiling, not a target. Usable budget is the window minus output reserve and safety margin.
- Agent loops make input cost grow roughly quadratically with turns, because history is re-sent.
- Pruning is cheap and safe; compaction is lossy. Prune first, compact second, and pin what must never be summarised.
- Static first, dynamic last. Stable prefixes are what prompt caching rewards.
- If you do not log tokens per category per turn, you cannot explain a regression.
Pro lesson
The rest of this lesson is in Pro.
The mental model, worked example, field note, failure table, exercise and self-check, with the voice tutor.
- Every stage and specialisation, in full
- Every lab and every role project
- Halden weeks 2–10, on your record
- Every drill, live in the playground
- All solutions, plus the server judge
- Lab Assistant and every Teach with AI