An AI agent uses the information in its current input to answer questions, search documents, and use tools. Context engineering means choosing and maintaining the information the agent receives, including instructions, conversation history, retrieved documents, memory, and tool descriptions. Give the model what it needs for the current step, without burying useful details in irrelevant information.
Prompt engineering focuses on wording instructions. Context engineering covers what the model receives and when it receives it, as Sourcegraph’s explanation describes. This distinction matters for agents that take multiple steps. Each tool call adds information, and each step needs accurate, relevant context that is easy to use.
Choose what enters the context
An agent’s context is its working information for a particular step. It may include the user’s request, operating instructions, available tools, tool results, relevant conversation history, and information retrieved from an external store. The model’s context window sets a limit on how much of this information it can process at once. The model does not automatically retain information outside that window. To use information across sessions, an agent needs an external memory store and a way to retrieve relevant records, as Weaviate’s discussion of memory and retrieval explains.
Start with the current task. Include instructions for the agent, the facts it needs, and the tools for its next step. Write system instructions clearly enough to guide the agent, while leaving room for situations they do not cover. Anthropic’s guidance on context engineering recommends clear instructions and a small set of high-signal information.
Tool descriptions tell the agent what each tool does and what information it returns. Give each tool a clear purpose, and avoid presenting irrelevant tools when they could distract the agent from the task. For example, a document-searching agent should receive search tools that match its current work, along with the relevant search results, rather than a long catalog of unrelated capabilities.
Manage context as the agent works
An agent’s context changes as it reads, reasons, and calls tools. Plan for these changes. Keep useful decisions and unresolved questions, summarize long exchanges when details no longer matter, and retrieve specific facts again when the next step needs them. A summary should keep information that affects the agent’s next action. A specific preference tied to the task is more useful than a vague note such as “the user prefers concise answers.”
A growing context does not automatically improve an answer. Anthropic uses context rot to describe declining recall as context grows. In its testing, models became less reliable at retrieving details from longer inputs. The same discussion uses “attention budget” to describe how additional tokens compete for the model’s focus. Adding an entire conversation or a large document collection can make the relevant instruction harder to use.
Poorly managed context causes several problems. Context poisoning occurs when incorrect information enters the agent’s working context and influences later steps. Context distraction occurs when accumulated history draws attention away from the current task. Context confusion arises when irrelevant documents or tools crowd the input, while context clash occurs when the context contains contradictory instructions or facts. If an agent repeats a mistake, check the records and tool results it received before adding another instruction.
Test context changes before keeping them
Static context files, such as repository instructions that an agent reads for every coding task, can help or add overhead. Evidence on their effects is mixed. A review of context-engineering research reports that one study found human-written files improved task success, while auto-generated files reduced it. Both increased inference costs. Another reported lower runtime and fewer output tokens, with task completion unchanged. Because the results come from different evaluations, they do not show that context files always help or always hurt.
Test additions against representative tasks before making them permanent. Check whether the agent completes the task, chooses the right tools, and uses resources efficiently. If the agent rarely needs information in a context file, provide it through retrieval instead of including it every time. If it consistently clarifies a recurring task, keep it and continue checking its effects.
Some approaches update context as agents work. The Agentic Context Engineering framework describes generating, reflecting on, and curating context as an evolving playbook. Its reported results come from specific benchmarks. They support the approach in those settings, but do not guarantee results for every agent. Revise context based on task outcomes instead of treating an initial prompt or file as final.
When an agent fails at a task, first find the cause. If the instructions are unclear, revise them. If the agent lacked a needed fact, improve retrieval or memory. If irrelevant tools or old history distracted the agent, remove or update them. If latency or cost is the problem, changing the model may work better than rewriting prompts, as Anthropic’s prompt-engineering overview notes.