Agents · Module 08
Context Engineering
Deciding what goes into the context window and what stays out: its anatomy, a token budget, writing and reading context, and assembling it per request.
Học xong bạn làm được
- Explain how context engineering extends prompt engineering
- Name the parts of a context window
- Set and enforce a context budget
- Assemble context dynamically for each request
- Structure context for prompt caching and read its cost
- Ground answers and avoid context anti-patterns
Bài học
- From prompt engineering to context engineering
Context engineering designs, structures and manages everything an LLM is given - system prompt, retrieved documents, tool outputs, history and metadata - treating the whole context window as an engineered artifact with a budget, an architecture and a lifecycle. Prompt engineering tunes the instruction; production quality depends on all the rest too.
- Anatomy of a context window
Every LLM call assembles its context window from six components - system prompt, few-shot examples, retrieved context, tool outputs, conversation history and the user instruction - each with its own role and typical size, from 50-500 tokens for the instruction to 1,000-50,000 for the history.
- Context budgeting
A context budget allocates the token window across components like a financial budget - exceeding it degrades quality, wasting it leaves the model under-informed. Fixed budgets go to the system prompt, few-shot examples, retrieval and tool outputs, the remainder to history, after reserving room for the answer. Over-budget parts are cut by recency, relevance, summarisation or a sliding window.
- Write context vs read context
Andrej Karpathy's framing splits context into write context - authored and controlled, static, optimised offline (system prompts, few-shot examples, output schemas) - and read context - assembled at runtime, dynamic (retrieved documents, tool outputs, history, user messages). Write context is A/B tested, curated and versioned; read context is ranked, summarised and compressed per request.
- Dynamic context assembly
In production the context is assembled from several sources at request time - retrieved documentation, a customer profile from a tool, recent tickets - and filled into a system template with rules, then the user query. Every retrieved chunk is tagged with its source so the model can cite it.
- Context caching and optimisation
Providers cache a reused context prefix to cut cost and latency - Anthropic prompt caching with cache_control, OpenAI's automatic prefix caching above 1,024 tokens, Google context caching with a TTL. To benefit, put the stable prefix (system prompt, few-shot examples, reference docs) first and the dynamic content (retrieved context, user query) last.
- Grounding, patterns and the full pipeline
Context is the main defence against hallucination - explicit sourcing instructions, source tags, relevance filtering and fact-check prompting keep the model faithful, and placing the best documents first and last counters lost in the middle. Good patterns are budget-first design, layered context, progressive disclosure and context validation; the anti-patterns are context stuffing, blind retrieval, history hoarding and prompt-only thinking.
- Prompt caching: the highest-ROI optimisation
Prompt caching serves a repeated prompt prefix at a steep discount - Anthropic writes at 1.25x and reads at 0.1x (90% off), OpenAI reads at 0.5x automatically, Google Gemini at 0.25x. Put static content first, keep the prefix stable, batch similar requests and watch cache_creation_input_tokens against cache_read_input_tokens; well-configured caching typically cuts cost by 45-80%.
- Assignment: Context Engineering
Context Engineering for Production RAG