Context engineering involves determining the precise information available to an LLM or agent during reasoning, such as instructions, memory, retrieved facts, and tools, to ensure a fixed, finite window results in a dependable answer rather than a mere estimation.
Context engineering Curating and assembling all the information a model sees at inference time, including system instructions, user requests, memory, knowledge retrieval, and tools, allows an agent to reason with precision and efficiency, ensuring only the necessary information is utilized. This process differs from prompt engineeringContext engineering oversees the entire information lifecycle for a multi-step, sometimes multi-session task by crafting a single instruction. The widely recognized operating framework, popularized by LangChain, divides the discipline into four key steps: Write (save context for later), Select (pull in only what's relevant now), Compress (fit more meaning into fewer tokens), and Isolate (keep unrelated context from contaminating a task).
Prompt engineering It involves carefully wording a single directive - the phrasing, examples, and formatting - a skill applied to individual messages.
Context engineering The architectural approach to information management encompasses the entire lifecycle of information, including selection, retrieval, filtering, construction, compression, evaluation, and refresh, ensuring seamless functionality throughout an agent's entire task rather than just its initial message, preventing issues such as missing critical documents or being overwhelmed by unnecessary information.
Consider a context window as a checklist for a task: guidelines, the current issue, recent observations, reliable information, and available resources all come from the same limited pool of tokens.
A knowledge-intensive agent task often fills up almost half of the window with retrieved documents, highlighting the importance of careful selection and compression over sheer window size.
Four strategies, made famous by LangChain's research on agent engineering, that encompass nearly all the necessary tasks for a context-engineered system to maintain reliability as tasks become more lengthy and intricate.
Every pillar addresses a unique failure mode: losing information, being overwhelmed by it, running out of space, or allowing unrelated tasks to overlap.
Store interim discoveries in a separate scratchpad to ensure they can be accessed later, preventing loss of important information when the window refreshes.
Only bring in information that is pertinent to the current task. Not every piece of stored data needs to be included, so retrieval should be both thorough and selective.
Condense or rearrange lengthy information to maximize efficiency without sacrificing necessary details for future use.
Prevent any unrelated information, such as another task, user, or document set, from influencing the current thought process.
Agents cannot just copy and paste an entire external knowledge base into their window. Vector databases address this issue by representing documents, conversation history, and structured records as embeddings, allowing the system to retrieve only the pertinent passages for the current task.
When combined with a retrieval-augmented generation (RAG) pipeline, this enables 'select' to expand the agent's knowledge base beyond the limitations of a context window by loading only the necessary information at a time.
Vector search can identify passages that share similarities, but it may not recognize the connections between a customer's ticket, their subscription tier, and the relevant policy document. In contrast, knowledge graphs explicitly model these relationships, allowing agents to navigate from a customer to their tickets to the applicable SLA in a structured manner, rather than relying on chance for all three to be surfaced through a similarity search.
GraphRAG Integrating graph-aware traversal with retrieval helps pinpoint relevant facts and extract supporting text, minimizing errors and providing a clear path for answering complex, multi-source questions, especially important in environments with access restrictions or compliance regulations.
The enduring concept present in all frameworks like LangChain's four pillars, RAG, knowledge graphs, and tiered memory, is also found in the 12-Factor Agents design principles: an agent architecture should be robust. own its context window Instead of viewing it as an endless notepad, the model will autonomously organize itself.
This implies that each design choice regarding instructions, memory, and retrieval is done purposefully, with a defined responsibility and allocated resources, rather than relying on happenstance.
| Context type | What it holds | Primary pillar it feeds |
|---|---|---|
| System instructions | Role, goals, constraints, safety rules | Write |
| User prompt | The specific request and its stated constraints | Select |
| Short-term memory | Recent conversation turns, current task state | Compress |
| Long-term memory | Durable facts, preferences, past sessions | Select |
| Retrieved knowledge | Documents, records, and graph facts pulled via RAG | Select / Compress |
| Tools & state | Available actions and the live state of the environment | Isolate |
Prompt engineering focuses on crafting the wording of individual instructions, while context engineering oversees the entire information lifecycle that an agent utilizes for a multi-step task, including instructions, memory, retrieved knowledge, and tools. Even a perfectly worded prompt can be ineffective if the surrounding context is inaccurate.
The framework, popularized by LangChain's agent research, includes: Store (save context for future use), Retrieve (select only relevant information), Condense (pack more meaning into fewer words), and Shield (prevent unrelated context from interfering with the task at hand).
Knowledge engineering involves organizing and saving data for easy retrieval by constructing a vector database or knowledge graph. Context engineering is the next level, determining during inference which information from the stored knowledge is relevant to the model's current context window.
GraphRAG integrates the explicit relationships of a knowledge graph with text lookup from retrieval-augmented generation, providing added complexity suitable for multi-source or time-sensitive inquiries, data with restricted access, or instances requiring explainable reasoning trails over single similarity-matched passages.
Short-term memory maintains the flow of conversation, while long-term memory stores important information to prevent repetitive learning. Selective retrieval of long-term memory prevents it from overwhelming the current task.
In the 12-Factor Agents methodology, the engineering team deliberately controls the contents of the model's context, treating it as a managed, finite resource instead of leaving it to the framework to decide.
Begin by identifying the six necessary types of context for your agent, then use Write, Select, Compress, and Isolate techniques to maintain a focused and relevant window as tasks become more complex.