Context Engineering
Prompt engineering asks, "What should I say to the model?" Context engineering asks, "What information should the model see, in what order, and with what authority?"
Many production failures are not model failures. They are context failures: missing evidence, stale memory, too much irrelevant text, or untrusted content treated as instructions.
The context stack
| Layer | Examples | Risk |
|---|---|---|
| Instructions | system/developer messages | conflicting rules |
| User request | current task | ambiguity |
| Conversation state | prior turns and summaries | stale assumptions |
| Retrieved knowledge | documents, search results, database rows | prompt injection |
| Tool results | API outputs, code execution, calculations | permission mistakes |
| Output contract | schema, style, citations | brittle downstream parsing |
Context is a budget
Long context windows are useful, but they are not free:
- more tokens cost more
- latency increases
- irrelevant context distracts the model
- important details can get lost in the middle
- private data exposure risk increases
The goal is not "stuff everything in." The goal is "include the smallest sufficient context."
A good context pipeline
1. Classify the task
2. Select the needed sources
3. Retrieve or call tools
4. Compress or rank evidence
5. Assemble the prompt in trusted order
6. Ask for a structured answer
7. Evaluate grounding and format
Memory types
| Memory | What it stores | Example |
|---|---|---|
| Working memory | active task state | "We are drafting section 3." |
| Episodic memory | prior interactions | "User prefers concise answers." |
| Semantic memory | durable facts | "Refund window is 30 days." |
| Procedural memory | how to do tasks | "Run tests before deployment." |
Do not put everything in long-term memory. Store only stable, useful, non-sensitive facts.
Context safety rules
- Mark retrieved content as evidence, not instructions.
- Separate trusted tool output from user-provided text.
- Strip or quote untrusted instructions in documents.
- Prefer citations for factual answers.
- Keep secrets out of prompts.
- Use evals to catch regressions when context assembly changes.
Knowledge check
Q1: What is the goal of context engineering?
To give the model the smallest trusted context that is sufficient for the task.
Q2: Why can more context make answers worse?
It can add distraction, cost, latency, and lost-in-the-middle effects.