Back
intermediate
Application Reliability

Context Engineering

Design the information window around a model: instructions, memory, tools, retrieval, and conversation state

22 min read· context engineering· memory· RAG· prompting

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

LayerExamplesRisk
Instructionssystem/developer messagesconflicting rules
User requestcurrent taskambiguity
Conversation stateprior turns and summariesstale assumptions
Retrieved knowledgedocuments, search results, database rowsprompt injection
Tool resultsAPI outputs, code execution, calculationspermission mistakes
Output contractschema, style, citationsbrittle 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

text
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

MemoryWhat it storesExample
Working memoryactive task state"We are drafting section 3."
Episodic memoryprior interactions"User prefers concise answers."
Semantic memorydurable facts"Refund window is 30 days."
Procedural memoryhow 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.