Context Engineering
Context window management, long-context processing, context failure modes and context security.
What is context engineering?
Context engineering is the discipline of managing what goes into an LLM's context window for optimal output quality, cost and latency. It includes: selecting the right context (retrieval, history, examples), compressing it (summarisation, pruning), ordering it (recency, relevance), and securing it (PII redaction, injection defence).
As context windows grow (1M+ tokens with Claude, Gemini), the temptation is to stuff everything in. But more context ≠ better results — 'lost in the middle' phenomena, cost scaling, and injection risk all grow with context size. Context engineering is the production discipline of getting the MOST signal into the LEAST context.
Why context engineering matters
Three forces make this critical: (1) Cost — input tokens are priced, and context grows linearly with conversation turns. (2) Quality — 'lost in the middle' means models ignore content in the middle of long contexts. (3) Security — more context = more injection surface area. Engineers who ignore context engineering ship expensive, degrading, insecure systems.
Context engineering pipeline
Available context sources (history, retrieved docs, system prompt, user message, tools) → select (relevance-ranked) → compress (summarise old, prune redundant) → order (system first, most-relevant last) → secure (redact PII, delimit untrusted) → assemble → LLM. Every stage is observable and tunable.
Context window management
def chat(messages: list[dict]) -> str:
# Send the ENTIRE conversation every time
return llm.generate(messages)
# Problems: cost grows linearly, context window overflow, lost-in-the-middle, no compressionExperiment: context strategies
See how context strategy affects quality, cost and 'lost in the middle'.
What to observe
'Stuff everything' degrades badly with conversation length — cost balloons, 'lost in the middle' kills recall. Summarising old turns is the single biggest win for long conversations. Ordering matters: place the most relevant content LAST (highest model attention), system prompt FIRST. Pruning irrelevant context saves cost and improves quality.
Production context engineering
Production context: token budget per request, summarise old history (sliding window), rank+prune retrieved context, order for attention (system first, relevant last), PII redaction on all untrusted content, delimiters around retrieved data, and observability on context composition (what went in, what was pruned).
Challenge
Your long-conversation assistant forgets facts mentioned 30 turns ago but remembers the last 5 turns perfectly. Design a context strategy that preserves long-term facts without exceeding the budget. (Hint: hierarchical summarisation + fact extraction.)
Production checklist
Production checklist
0 of 10 checked
Knowledge check
Your model forgets content in the middle of a long prompt. What's the fix?
Complete
You can now engineer context for quality, cost and security. This is the emerging discipline of 2025 — master it.
Mark this chapter as complete
Track your progress and unlock the next chapter.