Key takeaways
- ✓Measure before you cut: one message to the assistant was 3,332 tokens, 71% of it tool definitions sent on every call, and 0.3% the question.
- ✓Keeping the last 600 tokens chopped off the rules, tools, memory and documents: 2 of 5 right, and the pricing rule was gone.
- ✓Cut by priority instead: always the rules and the question, then memory, top documents, the newest messages. Summarize what you drop: 5 of 5 at 600 tokens, for one extra call. At 400 even the summary didn't fit.
- ✓Less text doesn't fix arithmetic. 400 raw rows and the 21 failed rows alone were both wrong on all three nights; with exactly 21 rows in front of it, the model counted 22.
- ✓Let code compute (91 tokens, right every night), or offload big results to a file the model can query (about 1,600 tokens over 3 calls, right every night, and the model picks the filter and the math).
Every token in a prompt is paid for, and waited for, on every call. The question is never just "does it fit?" but "what's the smallest prompt that still has what this step needs, in a form the model can use?" This chapter measures where an assistant's tokens go, cuts a prompt to a budget three ways, and hands a huge tool result to the model four ways.
The sources are ai-eng-labs Lab 07 (07_context_engineering), scripts 2 (02_context_budget.py) and 4 (04_tool_results.py). Ledgerly, a payments company, its assistant, chats and payouts are made up; the answers are real recorded runs of gpt-5.4-nano, one run per test. Chapter 6 covers long agent sessions and skills.
What context is
Context is everything the model sees on a call. For Ledgerly's internal engineering assistant, six parts: its rules, tool definitions (one description per action it can take), memory about the user, documents from search, the chat so far, and the question. Context engineering is deciding what goes in, in what form, and what stays out. Chapters 3 and 4 decided which past information comes back; this chapter decides how everything fits.
Where the tokens go
One message to the assistant: 3,332 tokens
Keep the last N tokens and chop off the front: where the rules, tools, memory and documents live.
- How much does the enterprise plan cost? needs: rules✕
“I don’t have any information in this chat about pricing or what the “enterprise plan” costs. If you share the pricing page or the relevant details, I can help you interpret it.”
- Which cloud should my deploy script target? needs: memory✕
“I don’t have enough context from the conversation to know which cloud your deploy script should target.”
- What's the right way to restart ledger-sync? needs: documents✕
“I don’t have enough context to know your exact “ledger-sync” service name/process or the correct operational procedure in your environment. In general, the safe “right way” is: 1. Identify the service (systemd/k8s/etc.). 2. Restart it gracefully (preferred) rather than killing the process. 3. Check logs/health after”
- What's our on-call channel again? needs: old history✓
“Your on-call channel is #ledger-oncall-sev.”
- Which bug am I working on right now? needs: recent history✓
“You’re working on the duplicate-webhook bug in the refunds flow.”
2 of 5 right · the on-call ✓ is luck: the tail starts with that message
| Part | Tokens | Share |
|---|---|---|
| 12 tool definitions | 2,361 | 70.9% |
| Chat history (30 messages) | 624 | 18.7% |
| Documents (runbooks) | 224 | 6.7% |
| Rules | 72 | 2.2% |
| Memory | 41 | 1.2% |
| The question | 10 | 0.3% |
| One message | 3,332 |
Every token is paid for, and waited for, on every call, and the tools are sent even when no tool is needed. Measure first: the biggest part may not be the one you'd guess.
Cutting to a budget
Say the prompt must fit in 600 tokens. Five test questions each need a different part:
| Needs | Question |
|---|---|
| Rules | How much does the enterprise plan cost? (The rule: reply exactly "Please contact sales.") |
| Memory | Which cloud should my deploy script target? |
| Documents | What's the right way to restart ledger-sync? |
| Old history | What's our on-call channel again? (said 30 messages ago) |
| Recent history | Which bug am I working on right now? |
| Cut | 1,200 tokens | 600 | 400 |
|---|---|---|---|
| Keep the end (chop the front) | 4/5 | 2/5 | 1/5 |
| By priority | 5/5 | 4/5 | 4/5 |
| By priority + summary | 5/5 | 5/5 | 4/5 |
- Keep the end cuts the front, where the rules, tools, memory and documents live. Asked about pricing with the rules gone, it said it had no pricing information and asked for the pricing page instead of "Please contact sales." It never quoted a price, but it broke the rule. At 600, the on-call ✓ is luck: the kept tail starts with that message.
- By priority always keeps the rules and the question, then fills the rest in order: memory, the top documents, the newest messages, older ones if there's room. The 12 tools are dropped first: none of these questions needs one. At 600 it lost the on-call channel, in the 18 oldest messages it dropped.
- + summary summarizes the dropped messages in a few lines first (one extra model call): 5 of 5 at 600. At 400 there was no room even for the summary. Some budgets are just too small.
Huge tool output
"How many payouts failed on the night of 2026-03-06, and what was their total amount in dollars?" The payouts tool returns 400 rows of JSON. Four ways to hand it to the model, on three nights:
| Method | What the model gets | Prompt tokens | Nights right |
|---|---|---|---|
| Raw | All 400 rows | 38,031 | 0/3 |
| Trimmed in code | Only the failed rows | 594 | 0/3 |
| Computed in code | Two numbers: the count and the total | 91 | 3/3 |
| Offloaded | A summary of a saved file, and a query tool | about 1,600, over 3 calls | 3/3 |
A night counts as right only with the right count and a total within $1. On night 1 the truth is 21 failed, $40,913.16. Raw said 10 ($26,980.62). Trimmed, with exactly 21 rows in front of it, said 22 ($44,624.52). On night 3 trimmed got the count right and the total wrong.
Less text doesn't fix arithmetic. This small model is unreliable at counting and adding up by reading. Computing in code is right every night, but only if you knew the question in advance.
Offloading keeps the full table outside the prompt:
get_payouts(date="2026-03-06")saves the rows topayouts_2026-03-06.jsonand returns only a summary: 400 rows, the 10 column names, and two example rows.- The model calls
query_saved_file(file, where_field="status", where_value="failed", aggregate="count_and_sum_amount_usd"), and code returns{"count": 21, "sum_amount_usd": 40913.16}. - The model answers: 21 payouts, $40,913.16.
About 1,600 tokens in total over three small calls (308 + 601 + 678 on night 1), right every night. The model picked the filter and the math itself, so the tool didn't have to be built for this one question. Its limit: the query tool only does "equals" filters and the aggregates written in advance.
What else the lab measures
Script 1 of the same lab measures context rot: models getting worse as the prompt grows, even when the answer is in it. The classic needle test is easy because the hidden sentence shares the question's words; reworded facts, look-alike distractors and facts that must be combined break sooner. On gpt-5.4-nano, the harder versions failed even at 1K tokens of filler, yet passed with the filler removed; gpt-5.4-mini showed the classic curve. It isn't in the video, and it's one more reason to keep prompts short.
The checklist
- Measure where the tokens go before you cut.
- Never chop blindly: keep the rules and the question, then fill by priority, adding list items one at a time.
- Summarize what you drop, and know the budget below which nothing works.
- Send only the tools a request needs (Chapter 6 does this for instructions, as skills).
- Never make a model count or add up what code can compute.
- Keep big results in a file, with a summary and a way to query them.
Practice: cut a prompt, offload a result
Profile a prompt, implement "keep the end" and see it lose the rules, cut by priority (the tests reproduce the lab's 18 dropped messages at 600 tokens), then write the offload summary and a query tool. The tests use the lab's real part sizes and the real 400 payouts from 2026-03-06.
Quiz
1.In one 3,332-token message to the assistant, which part was the biggest?
2.You keep only the last 600 tokens of the prompt. What gets cut?
3.Cutting by priority at 600 tokens got 4 of 5. Which one was lost, and what fixed it?
4.The tool output was trimmed in code to only the 21 failed payouts, 64 times fewer tokens. Did the model get the count right?
5.What does offloading return to the model instead of the 400 rows?
6.Computing the answer in code used 91 tokens and was right every night. Why still offload?
Check your understanding
Try each question before opening its answer.
What is context engineering?
Deciding what goes into the model's context on each call (rules, tool definitions, memory, documents, history, tool output, the question), in what form, and what stays out, within a token budget.
Why profile the prompt first?
The biggest part is often not what you'd guess: here the 12 tool definitions were 71% of every message, sent even when no tool was needed, while the question was 0.3%.
What goes wrong with "keep the last N tokens"?
It cuts the front of the prompt, where the rules, tools, memory and documents live. At 600 tokens only the history and question survived: 2 of 5 right, and the pricing rule was broken.
How do you cut by priority, and what does the summary add?
Always keep the rules and the question, then memory, top documents, newest messages, older messages; drop unneeded tools first. A few-line summary of the dropped messages saved the on-call channel: 5 of 5 at 600, for one extra call. At 400 even the summary didn't fit.
Why didn't trimming the tool output fix the answer?
The problem was arithmetic, not length. With exactly the 21 failed rows in front of it, the model counted 22, and it was wrong on all three nights. Code should count and add up.
When would you offload instead of computing in code?
When you don't know the question in advance. The model gets a short summary of the saved file and a query tool, chooses the filter and the aggregate, and code does the math: right every night at about 1,600 tokens over 3 calls.
Next up: Chapter 6 follows an agent that keeps adding to its own prompt: compaction, what it forgets, and skills that load only when needed.