Skip to content

Context Engineering

📝 Context

Everyone talks about prompt engineering — the wording of the instruction. The bigger lever is usually context engineering: deciding what information goes into the model's window, and in what order, for each request. RAG, chunking, memory, and tool results are all context engineering. This lesson reframes "the prompt" as "the briefing," because that's where most real quality lives.

Recommendation: spend your effort on what the model sees, not just how you phrase the ask. The window is finite and its contents are the model's entire world for that call.

🎯 The Levers

LeverThe decisionWhy it matters
What to retrieveWhich passages/tools/results to includeWrong context → confident wrong answer
How to chunkPassage size and boundariesSets the ceiling on what retrieval can even find
OrderingWhere in the window each piece sitsModels weight position; lead with the most relevant
CompactionSummarize/trim old turnsThe window is finite; overflow drops context silently
InstructionSystem prompt: grounding, refusal, formatTurns raw capability into reliable behavior

🧭 The Window Is Finite

Context window assembly — system instruction, question, retrieved passages, and conversation history feeding an assemble-context step; if the briefing fits the window, the model answers, and if it does not, the app compacts and retries

The token-count numbers you hear in the field are always model- and workload-specific. The durable rule is the loop above: assemble the briefing, check whether it fits, and compact or re-retrieve when it does not.

What an SE says about this

"'It forgot what I told it' or 'it ignored the doc' is usually a context problem, not a model problem — the relevant thing wasn't in the window, or was buried. We fix it by engineering what goes in, not by swapping models."

📊 Rules of Thumb (illustrative)

  • Chunk sizes commonly start around 300–800 tokens with a small overlap — then tune by inspecting what retrieval returns. No universal best size.
  • Lead with the most relevant passages; models weight earlier context more, and very long contexts dilute attention ("lost in the middle").

Accuracy note: these ranges are illustrative starting points, not standards. The durable rule: measure retrieval quality on your own data and adjust.

🧩 Worked Scenario: The Long Conversation That Drifts

A support chat works well for five turns, then starts contradicting itself. The window filled up; the earliest (and a key) turn scrolled out of context.

  • Diagnosis — history grew past the window; old context was silently dropped.
  • Fix — compaction: summarize older turns into a running brief, keep the summary + recent turns in the window.
  • Also — re-retrieve relevant docs each turn rather than assuming they're still in context.
  • Result — coherent long conversations without a bigger model.

🚨 Failure Path

Treating the context window as infinite — pasting entire documents or unbounded history into every call. It's slow, expensive, dilutes attention, and silently drops whatever doesn't fit.

  • Symptom — degrading quality as conversations or documents grow; rising cost per call.
  • Root cause — no strategy for what to include and what to compact.
  • Fix — retrieve the relevant slice, order it deliberately, compact the rest.

👁️ Audience Lens — Who Hears What

Engineer hearsExec hearsCustomer hears
Context engineeringretrieval, chunking, ordering, compactionquality/cost lever without a model upgrade"it stays on track and uses our docs"

🗣️ Talk Track

Say it like this

"The model only knows what we put in front of it for each question — think of it as the briefing, not the brain. Most of the quality work is choosing the right briefing: pulling the relevant passages, ordering them well, and summarizing the rest. That's cheaper and more reliable than reaching for a bigger model."

⚠️ Gotchas

  • Assuming a bigger context window removes the need to curate — long contexts still dilute attention.
  • Chunking as an afterthought — it caps what retrieval can find (see RAG Patterns).
  • Never compacting history — long chats silently lose early, important context.
  • Optimizing prompt wording while ignoring what's actually in the window.