Lesson 9.4 · 23 min · video lesson
Context Engineering: Curating the Model's Working Memory
When an AI assistant gives a wrong answer, is the model usually the problem, or is it what we showed the model?
In short: Context engineering is the work of deciding what information goes into an LLM's context window for each call: instructions, memory, history, retrieved documents, tool definitions and tool results. The model only knows what is in front of it, so selecting, ordering, compressing and isolating that information often matters more than clever wording. Good context is relevant, sufficient, compact and well structured.
What you will learn
- What is Context Engineering?
- The Big Picture
- Why Context Engineering matters
- Prompt Engineering vs Context Engineering
- The components of the context
- Common patterns in Context Engineering
- Common mistakes to avoid
- Best practices
- Quick Summary
In this lesson
- What is context engineering?
- The big picture: the context is assembled, not written
- Why context engineering matters
- Prompt engineering vs context engineering
- The components of the context
- Code you can run: assembling context under a budget
- Common patterns in context engineering
- Common mistakes and best practices
- Going one level deeper
- Practice: try it yourself
- Quick summary
Key terms in this lesson
- Context window
- All the tokens an LLM can see in one call, up to its maximum context length.
- Context engineering
- Designing what information goes into the context window for each model call, and in what form and order.
- Retrieval (RAG)
- Searching a knowledge source and inserting the most relevant results into the context.
- Long-term memory
- Information saved outside the context across sessions and pulled back in when relevant.
- Lost in the middle
- The tendency of models to use information in the middle of a long context less reliably than at its edges.
- Sub-agent isolation
- Running a sub-task in a separate context window and returning only a compact result.
This lesson is part of the Generative AI Engineering and Complete AI Engineer tracks. See plans and pricing to open the full lesson, its video, labs and quiz, or try the free lesson first (free account needed).
← 9.3 Prompt Caching: Reusing Computation Across API Calls · 9.5 Context Compaction: Fitting More Into a Finite Window →