Level 8Lesson 71โฑ๏ธ 45 min

Assembling Context

The four pillars don't just sit there - they're assembled, ranked, and pruned into one tidy desk on every single turn. This is the pipeline

Context Is Built Fresh Every Turn

Here's the part people miss: the context window isn't set up once and left alone. It's rebuilt on every turn of the agent's loop. Each step, a little pipeline decides what goes on the desk this time - what to add, what to keep, what to clear off. Context engineering is really the design of that pipeline.

The whole pillar set, in motion: instructions, retrieval, memory, and tools all feed into one assembly step that produces the final desk for this turn. Get the assembly wrong and even great pillars fail.

The Four Questions

Every assembly decision boils down to four questions. Memorize these - they're the entire discipline in four lines:

1. WHAT do we fetch?      (which docs / facts / files)
2. WHEN do we fetch it?   (up front, or just in time)
3. HOW do we compress it? (summarize, truncate, re-rank)
4. WHEN do we throw away? (evict stale or finished context)

Questions 1-2 are the retrieval pillar. Questions 3-4 are budget management - the part beginners skip, and the part that separates a demo from a production agent.

The Assembly Pipeline

Put concretely, here's what runs each turn:

USER INPUT
   โ”‚
   โ”œโ”€โ–ถ run several retrievals in parallel
   โ”‚     (keyword search, semantic search, file reads, db lookups)
   โ”‚
   โ”œโ”€โ–ถ MERGE all the candidates into one big pile
   โ”‚     (this pile is almost always TOO BIG for the budget)
   โ”‚
   โ”œโ”€โ–ถ RE-RANK and keep only the top few (more on this below)
   โ”‚
   โ”œโ”€โ–ถ layer in: system instructions + memory + tool defs
   โ”‚
   โ–ผ
FINAL CONTEXT  โ†’  send to the model  โ†’  it acts  โ†’  repeat next turn

Re-Ranking: The Make-or-Break Step

The merge step produces way more candidate chunks than the budget allows. Re-rankingis the step that scores every candidate against the actual question and keeps only the best handful. This is where context pipelines quietly succeed or fail.

WITHOUT re-ranking          WITH re-ranking
  retrieve 50 chunks          retrieve 50 chunks (high recall)
  dump all 50 on the desk     score each vs. the question
  hope the answer's in there  keep the top 5 (high precision)
  โ†’ bloated, rot, lost-       โ†’ tight, high-signal desk
    in-the-middle
The mental model: cast a wide net (grab 50 so you don't miss the answer), then ruthlessly filter (keep 5 so the desk stays sharp). Wide recall, then tight precision. A pipeline that grabs 50 and keeps the best 5 beats one that grabs 50 and dumps all of them, every time.

Token-Budget Management

This is the "compress" and "throw away" questions made practical. The key insight: cut low-signal content before it hits the desk, not after. Common moves:

โ€ข Truncate tool outputs        (keep the useful head/tail, drop the rest)
โ€ข Compact old conversation     (summarize, as in Lesson 69)
โ€ข Drop low-relevance chunks    (below a score threshold โ†’ don't include)
โ€ข Cap each retrieval           (no single search can flood the desk)
โ€ข Clear finished tool results  (once read, the raw output can go)
Order matters too (Lesson 66 callback): because of "lost in the middle," put the highest-signal material at the top or bottom of the assembled context - never buried in the center of a long block.

Track the Budget Like a Bill

Every retrieval, every tool call, every re-rank shows up in cost and latency. Production teams track tokens-used and tool-calls per task, set budgets, and get alerted when an agent blows past them. It's the same discipline as watching a cloud bill - an agent quietly using 5ร— the context it needs is a quietly 5ร— expensive agent.

This connects to Level 7: remember "Cost per Accepted Change"? Token budget per task is the context-engineering version. A leaner desk is a cheaper, faster, and more accurate agent - all three improve together.

Hands-On: Be the Re-Ranker

Hands-on (15 min): Pick a question about a topic you know. Do a web or doc search and copy the titles/snippets of the first ~15 results (your "candidates"). Now play re-ranker: score each from 0-5 on how directly it answers your specific question, and keep only the top 4. Notice how many high-ranking-by-search results are actually off-target, and how much sharper your kept-4 set is. That gap - between "what search returned" and "what actually helps" - is precisely the value re-ranking adds to a context pipeline.
Lesson 71 Quick Reference
Context is rebuilt each turn

A pipeline assembles the desk fresh every step of the agent loop - that pipeline is what you design

The four questions

What to fetch, when to fetch, how to compress, when to throw away

Assembly pipeline

Retrieve in parallel โ†’ merge candidates โ†’ re-rank to top-k โ†’ layer in instructions/memory/tools โ†’ send

Re-ranking

Score all candidates against the question and keep only the best few - wide recall, then tight precision

Token-budget management

Cut low-signal content before it enters: truncate, compact, drop below threshold, cap, clear finished results

Order for attention

Put highest-signal material at the top or bottom, never the lost middle

Track tokens per task

Monitor cost/latency per task and alert on overruns - leaner context is cheaper, faster, and more accurate

โ† Pillar 4 - Tools
Unlocks in ~12 min of reading