What Is Context Engineering, Anyway?
What Is Context Engineering, Anyway?
You're talking to an AI assistant about planning a product launch. You've explained your target audience, your budget constraints, your deadline pressure, and the specific regulatory requirements in your industry. The AI responds thoughtfully. Then you switch to a different conversation thread for a moment—just to check on a technical question—and when you return, your product launch assistant has completely forgotten the regulatory requirements you mentioned. Not just forgotten them, but actively recommended something that would violate them.
This is a context failure. And if you've spent any meaningful time working with large language models, you've probably experienced something similar. Perhaps not as dramatically, but the pattern is familiar: the system that "forgets" important details you've provided, the chatbot that contradicts itself three messages later, the assistant that helps you write code for the wrong programming language despite you mentioning the correct one seven times.
These failures feel like bugs. They're often described as "memory problems" or dismissed as inherent limitations of the technology. But they're neither bugs nor inherent limitations—they're *engineering problems*. And like most engineering problems, they're solvable with the right understanding and the right tools.
That's what this book is about.
Context engineering is the discipline of designing, building, and optimizing the information systems that surround large language models. It's about understanding what "context" actually means in these systems, why it matters so much, where it tends to break down, and—most importantly—how to fix it.
But before we can engineer something, we need to understand what we're building.
The Fuzzy Thing We Call Context
When developers talk about "context" in AI systems, they often mean the prompt—the user input, the system instructions, the few-shot examples. When researchers talk about context, they might mean the attention window, the context length, the 128,000 tokens or however many the model can process at once. When end users talk about context, they're usually referring to the conversational thread, the things they've said, the information they've shared.
All of these are *aspects* of context, but none of them is context itself.
Here's a more useful way to think about it: Context is the totality of information that influences how an AI model produces its output at any given moment. This includes everything from the explicit instructions you've written in a system prompt to the implicit patterns the model has learned during training, from the specific conversation history to the broader task you're trying to accomplish, from the format you want your response in to the domain knowledge required to answer correctly.
The confusion arises because context isn't a single thing you can point to—it's an *emergent property* of multiple information streams converging at the moment of inference.
Consider what actually happens when you send a message to a state-of-the-art language model:
1. Your message arrives at the server. 2. The system retrieves your conversation history (if any exists). 3. It may retrieve additional context from external systems—retrieval databases, knowledge graphs, user profiles. 4. It applies any active system instructions or prompts. 5. It runs inference on the entire assembled context. 6. The output is generated and returned to you.
Each step in this chain involves decisions, architectures, and potential failure points. The model itself is just one component in a larger context system—and often not the weakest link.
Context Is Not Just the Prompt
Here's a concrete example of how this confusion manifests in practice. A development team building a customer service chatbot was experiencing complaints about the bot "forgetting" customer information mentioned earlier in the conversation. Their initial diagnosis was that the model had a poor memory.
The real problem was different. The team had implemented a "context compression" feature that aggressively trimmed conversation history after five exchanges to reduce token costs. But they hadn't accounted for the fact that customers often mention critical information (account numbers, order IDs, specific error messages) casually in the first few messages—sometimes even in greetings—without repeating it later.
The model wasn't forgetting. The compression algorithm was systematically discarding the most important information. This wasn't a model problem at all; it was a context engineering problem.
This distinction matters enormously. If you think "the model is forgetting," you might try switching to a model with a longer context window. If you understand that context engineering is the issue, you can identify that the compression logic is the bottleneck and fix it directly—which is faster, cheaper, and more effective.
The Information Flow Perspective
Another way to understand context is as a system of information flow. Information enters the system through various channels:
- **User input**: The explicit text the user types or speaks
- **System prompts**: Instructions baked into the application layer
- **Retrieval augmented data**: Information pulled from external sources at query time
- **Persistent user memory**: Long-term information about the user stored elsewhere
- **Session state**: Information specific to the current interaction session
- **Training-derived priors**: Knowledge encoded in the model weights
Once inside the system, this information flows through several stages before affecting the output:
- It must be *selected*—deciding which information is relevant
- It must be *formatted*—presenting it in a way the model can effectively use
- It must be *integrated*—blending multiple information sources coherently
- It must be *weighted*—determining how much influence each piece has
All of these stages are context engineering concerns. And all of them can fail in ways that degrade system performance without producing obvious error messages.
The Context Lifecycle
If context is the information flowing through an AI system, then the context lifecycle describes the stages that information passes through from initial creation to eventual expiration. Understanding this lifecycle is the foundation for diagnosing problems and designing solutions.
```mermaid graph TD A[Creation] --> B[Maintenance] B --> C[Retrieval] C --> D[Compression] D --> E[Expiration] E -->|Feedback Loop| A C -->|Influences| A B -->|Updates| A
A1[User Input] --> A A2[System Prompt] --> A A3[Retrieved Data] --> A
D1[Token Limit Hit] --> D D2[Relevance Score] --> D D3[Age Threshold] --> D
E1[Forced Context Drop] --> E E2[Session End] --> E E3[Manual Clear] --> E ```
Let me walk through each stage.
Creation
Context creation is the process of assembling the initial information payload that will be passed to the model. This happens every time a new request is made—or, in continuous systems, continuously throughout a session.
Creation involves several sub-processes:
Input gathering: Collecting the user's raw input (text, images, files, etc.)
Prompt construction: Assembling system instructions, role definitions, and any templated content
Context assembly: Combining all information sources into a coherent whole
Metadata attachment: Adding relevant metadata (timestamps, user identifiers, session IDs)
The critical decisions during creation are about *what* to include and *how* to structure it. A common mistake is including too much information—developers often assume that "more context is better" and dump entire conversation histories, large documents, or irrelevant background into every request. This creates noise that degrades model performance, increases latency, and inflates costs.
Maintenance
Once created, cont