← 返回文章列表
2025-09-29

AI 智能体的高效上下文工程

Effective context engineering for AI agents

Effective context engineering for AI agents

After a few years of prompt engineering being the focus of attention in applied AI, a new term has come to prominence:context engineering. Building with language models is becoming less about finding the right words and phrases for your prompts, and more about answering the broader question of “what configuration of context is most likely to generate our model’s desired behavior?" Contextrefers to the set of tokens included when sampling from a large-language model (LLM). Theengineeringproblem at hand is optimizing the utility of those tokens against the inherent constraints of LLMs in order to consistently achieve a desired outcome. Effectively wrangling LLMs often requiresthinking in context— in other words: considering the holistic state available to the LLM at any given time and what potential behaviors that state might yield. In this post, we’ll explore the emerging art of context engineering and offer a refined mental model for building steerable, effective agents.

在提示工程(Prompt Engineering)作为应用人工智能领域焦点数年之后,一个新术语逐渐崭露头角:上下文工程(Context Engineering)。基于语言模型的开发正在从寻找合适的提示词和短语,转向回答一个更根本的问题:"怎样的上下文配置最有可能让模型产生我们期望的行为?"

Context engineering vs. prompt engineering

上下文(Context)指的是从大语言模型(LLM)采样时所包含的一组 Token。而工程(Engineering)问题的核心在于:如何在 LLM 固有约束下优化这些 Token 的效用,以持续达成预期结果。有效地驾驭 LLM 往往需要以上下文的方式进行思考——换言之,需要综合考量 LLM 在任意时刻所能获取的整体状态,以及该状态可能引发的潜在行为。

At Anthropic, we view context engineering as the natural progression of prompt engineering. Prompt engineering refers to methods for writing and organizing LLM instructions for optimal outcomes (seeour docsfor an overview and useful prompt engineering strategies).Context engineeringrefers to the set of strategies for curating and maintaining the optimal set of tokens (information) during LLM inference, including all the other information that may land there outside of the prompts. In the early days of engineering with LLMs, prompting was the biggest component of AI engineering work, as the majority of use cases outside of everyday chat interactions required prompts optimized for one-shot classification or text generation tasks. As the term implies, the primary focus of prompt engineering is how to write effective prompts, particularly system prompts. However, as we move towards engineering more capable agents that operate over multiple turns of inference and longer time horizons, we need strategies for managing the entire context state (system instructions, tools,Model Context Protocol(MCP), external data, message history, etc). An agent running in a loop generates more and more data thatcouldbe relevant for the next turn of inference, and this information must be cyclically refined. Context engineering is theart and scienceof curating what will go into the limited context window from that constantly evolving universe of possible information.

在本文中,我们将探索上下文工程这一新兴实践,并提供一个经过优化的心智模型,用于构建可控且高效的智能体(Agent)。

Why context engineering is important to building capable agents

上下文工程与提示工程

Despite their speed and ability to manage larger and larger volumes of data, we’ve observed that LLMs, like humans, lose focus or experience confusion at a certain point. Studies on needle-in-a-haystackstyle benchmarking have uncovered the concept ofcontext rot: as the number of tokens in the context window increases, the model’s ability to accurately recall information from that context decreases. While some models exhibit more gentle degradation than others, this characteristic emerges across all models. Context, therefore, must be treated as a finite resource with diminishing marginal returns. Like humans, who havelimited working memory capacity, LLMs have an “attention budget” that they draw on when parsing large volumes of context. Every new token introduced depletes this budget by some amount, increasing the need to carefully curate the tokens available to the LLM. This attention scarcity stems from architectural constraints of LLMs. LLMs are based on thetransformer architecture, which enables every token toattend to every other tokenacross the entire context. This results in n² pairwise relationships for n tokens. As its context length increases, a model's ability to capture these pairwise relationships gets stretched thin, creating a natural tension between context size and attention focus. Additionally, models develop their attention patterns from training data distributions where shorter sequences are typically more common than longer ones. This means models have less experience with, and fewer specialized parameters for, context-wide dependencies. Techniques likeposition encoding interpolationallow models to handle longer sequences by adapting them to the originally trained smaller context, though with some degradation in token position understanding. These factors create a performance gradient rather than a hard cliff: models remain highly capable at longer contexts but may show reduced precision for information retrieval and long-range reasoning compared to their performance on shorter contexts. These realities mean that thoughtful context engineering is essential for building capable agents.

在 Anthropic,我们将上下文工程视为提示工程的自然演进。提示工程指的是编写和组织 LLM 指令以获得最优结果的方法(参见我们的文档了解概览及实用的提示工程策略)。上下文工程则指在 LLM 推理过程中,策展和维护最优 Token(信息)集合的一整套策略,涵盖提示之外可能进入上下文的所有其他信息。

The anatomy of effective context

在 LLM 工程化的早期阶段,提示是 AI 工程工作的最大组成部分,因为除日常对话交互外,大多数用例都需要针对一次性分类或文本生成任务优化的提示。顾名思义,提示工程的核心关注点是如何编写有效的提示,尤其是系统提示。然而,随着我们转向构建更强大的、能够在多轮推理和更长时间跨度上运行的智能体,我们需要管理整个上下文状态的策略(系统指令、工具、模型上下文协议(Model Context Protocol, MCP)、外部数据、消息历史等)。

Given that LLMs are constrained by a finite attention budget,goodcontext engineering means finding thesmallestpossibleset of high-signal tokens that maximize the likelihood of some desired outcome. Implementing this practice is much easier said than done, but in the following section, we outline what this guiding principle means in practice across the different components of context. System promptsshould be extremely clear and use simple, direct language that presents ideas at theright altitudefor the agent. The right altitude is the Goldilocks zone between two common failure modes. At one extreme, we see engineers hardcoding complex, brittle logic in their prompts to elicit exact agentic behavior. This approach creates fragility and increases maintenance complexity over time. At the other extreme, engineers sometimes provide vague, high-level guidance that fails to give the LLM concrete signals for desired outputs or falsely assumes shared context. The optimal altitude strikes a balance: specific enough to guide behavior effectively, yet flexible enough to provide the model with strong heuristics to guide behavior. We recommend organizing prompts into distinct sections (like<background_information>,<instructions>,## Tool guidance,## Output description, etc) and using techniques like XML tagging or Markdown headers to delineate these sections, although the exact formatting of prompts is likely becoming less important as models become more capable. <background_information> <instructions>

一个在循环中运行的智能体会产生越来越多可能与下一轮推理相关的数据,这些信息必须经过循环筛选和提炼。上下文工程就是从不断演化的海量可能信息中,策展出最适合进入有限上下文窗口的内容——这既是一门艺术,也是一门科学。

Tool guidance

为什么上下文工程对构建强大智能体至关重要

Output description

尽管 LLM 处理速度极快且能管理越来越大的数据量,我们观察到 LLM 与人类一样,在达到某个临界点后会丧失注意力或产生困惑。"大海捞针"(Needle-in-a-Haystack)类基准测试的研究揭示了上下文衰退(Context Rot)的概念:随着上下文窗口中 Token 数量的增加,模型从该上下文中准确回忆信息的能力会下降。

Regardless of how you decide to structure your system prompt, you should be striving for the minimal set of information that fully outlines your expected behavior. (Note that minimal does not necessarily mean short; you still need to give the agent sufficient information up front to ensure it adheres to the desired behavior.) It’s best to start by testing a minimal prompt with the best model available to see how it performs on your task, and then add clear instructions and examples to improve performance based on failure modes found during initial testing. Toolsallow agents to operate with their environment and pull in new, additional context as they work. Because tools define the contract between agents and their information/action space, it’s extremely important that tools promote efficiency, both by returning information that is token efficient and by encouraging efficient agent behaviors. InWriting tools for AI agents – with AI agents, we discussed building tools that are well understood by LLMs and have minimal overlap in functionality. Similar to the functions of a well-designed codebase, tools should be self-contained, robust to error, and extremely clear with respect to their intended use. Input parameters should similarly be descriptive, unambiguous, and play to the inherent strengths of the model. One of the most common failure modes we see is bloated tool sets that cover too much functionality or lead to ambiguous decision points about which tool to use. If a human engineer can’t definitively say which tool should be used in a given situation, an AI agent can’t be expected to do better. As we’ll discuss later, curating a minimal viable set of tools for the agent can also lead to more reliable maintenance and pruning of context over long interactions. Providing examples, otherwise known as few-shot prompting, is a well known best practice that we continue to strongly advise. However, teams will often stuff a laundry list of edge cases into a prompt in an attempt to articulate every possible rule the LLM should follow for a particular task. We do not recommend this. Instead, we recommend working to curate a set of diverse, canonical examples that effectively portray the expected behavior of the agent. For an LLM, examples are the “pictures” worth a thousand words. Our overall guidance across the different components of context (system prompts,tools,examples,message history, etc) is to be thoughtful and keep your context informative, yet tight. Now let's dive into dynamically retrieving context at runtime.

虽然某些模型的衰退比其他模型更为平缓,但这一特征在所有模型中都会出现。因此,上下文必须被视为一种边际收益递减的有限资源。与工作记忆容量有限的人类类似,LLM 在解析大量上下文时也有一个"注意力预算"(Attention Budget)。每引入一个新 Token 都会消耗一部分预算,从而增加了精心策展可用 Token 的必要性。

Context retrieval and agentic search

这种注意力稀缺源于 LLM 的架构约束。LLM 基于 Transformer 架构,该架构使每个 Token 都能关注整个上下文中的其他所有 Token。对于 n 个 Token,这会产生 n² 的成对关系。

InBuilding effective AI agents, we highlighted the differences between LLM-based workflows and agents. Since we wrote that post, we’ve gravitated towards asimple definitionfor agents: LLMs autonomously using tools in a loop. Working alongside our customers, we’ve seen the field converging on this simple paradigm. As the underlying models become more capable, the level of autonomy of agents can scale: smarter models allow agents to independently navigate nuanced problem spaces and recover from errors. We’re now seeing a shift in how engineers think about designing context for agents. Today, many AI-native applications employ some form of embedding-based pre-inference time retrieval to surface important context for the agent to reason over. As the field transitions to more agentic approaches, we increasingly see teams augmenting these retrieval systems with “just in time” context strategies. Rather than pre-processing all relevant data up front, agents built with the “just in time” approach maintain lightweight identifiers (file paths, stored queries, web links, etc.) and use these references to dynamically load data into context at runtime using tools. Anthropic’s agentic coding solutionClaude Codeuses this approach to perform complex data analysis over large databases. The model can write targeted queries, store results, and leverage Bash commands like head and tail to analyze large volumes of data without ever loading the full data objects into context. This approach mirrors human cognition: we generally don’t memorize entire corpuses of information, but rather introduce external organization and indexing systems like file systems, inboxes, and bookmarks to retrieve relevant information on demand. Beyond storage efficiency, the metadata of these references provides a mechanism to efficiently refine behavior, whether explicitly provided or intuitive. To an agent operating in a file system, the presence of a file namedtest_utils.pyin atestsfolder implies a different purpose than a file with the same name located insrc/core_logic/Folder hierarchies, naming conventions, and timestamps all provide important signals that help both humans and agents understand how and when to utilize information. test_utils.py tests src/core_logic/ Letting agents navigate and retrieve data autonomously also enables progressive disclosure—in other words, allows agents to incrementally discover relevant context through exploration. Each interaction yields context that informs the next decision: file sizes suggest complexity; naming conventions hint at purpose; timestamps can be a proxy for relevance. Agents can assemble understanding layer by layer, maintaining only what's necessary in working memory and leveraging note-taking strategies for additional persistence. This self-managed context window keeps the agent focused on relevant subsets rather than drowning in exhaustive but potentially irrelevant information. Of course, there's a trade-off: runtime exploration is slower than retrieving pre-computed data. Not only that, but opinionated and thoughtful engineering is required to ensure that an LLM has the right tools and heuristics for effectively navigating its information landscape. Without proper guidance, an agent can waste context by misusing tools, chasing dead-ends, or failing to identify key information. In certain settings, the most effective agents might employ a hybrid strategy, retrieving some data up front for speed, and pursuing further autonomous exploration at its discretion. The decision boundary for the ‘right’ level of autonomy depends on the task. Claude Code is an agent that employs this hybrid model:CLAUDE.mdfiles are naively dropped into context up front, while primitives like glob and grep allow it to navigate its environment and retrieve files just-in-time, effectively bypassing the issues of stale indexing and complex syntax trees. The hybrid strategy might be better suited for contexts with less dynamic content, such as legal or finance work. As model capabilities improve, agentic design will trend towards letting intelligent models act intelligently, with progressively less human curation. Given the rapid pace of progress in the field, "do the simplest thing that works" will likely remain our best advice for teams building agents on top of Claude.

随着上下文长度的增加,模型捕获这些成对关系的能力会被逐渐拉薄,在上下文大小和注意力聚焦之间形成一种天然的张力。此外,模型从训练数据分布中习得注意力模式,而训练数据中较短的序列通常比较长的序列更常见。这意味着模型在上下文级依赖关系方面经验更少,专门参数也更少。

Context engineering for long-horizon tasks

位置编码插值(Position Encoding Interpolation)等技术允许模型通过将较长序列适配到原始训练时较小的上下文来处理更长序列,但会带来一定程度的 Token 位置理解降级。这些因素创造的是一个性能梯度而非断崖:模型在更长上下文中仍然保持较强能力,但与较短上下文中的表现相比,信息检索和长程推理的精度可能会有所下降。

Long-horizon tasks require agents to maintain coherence, context, and goal-directed behavior over sequences of actions where the token count exceeds the LLM’s context window. For tasks that span tens of minutes to multiple hours of continuous work, like large codebase migrations or comprehensive research projects, agents require specialized techniques to work around the context window size limitation. Waiting for larger context windows might seem like an obvious tactic. But it's likely that for the foreseeable future, context windows of all sizes will be subject to context pollution and information relevance concerns—at least for situations where the strongest agent performance is desired. To enable agents to work effectively across extended time horizons, we've developed a few techniques that address these context pollution constraints directly: compaction, structured note-taking, and multi-agent architectures. Compaction Compaction is the practice of taking a conversation nearing the context window limit, summarizing its contents, and reinitiating a new context window with the summary. Compaction typically serves as the first lever in context engineering to drive better long-term coherence. At its core, compaction distills the contents of a context window in a high-fidelity manner, enabling the agent to continue with minimal performance degradation. In Claude Code, for example, we implement this by passing the message history to the model to summarize and compress the most critical details. The model preserves architectural decisions, unresolved bugs, and implementation details while discarding redundant tool outputs or messages. The agent can then continue with this compressed context plus the five most recently accessed files. Users get continuity without worrying about context window limitations. The art of compaction lies in the selection of what to keep versus what to discard, as overly aggressive compaction can result in the loss of subtle but critical context whose importance only becomes apparent later. For engineers implementing compaction systems, we recommend carefully tuning your prompt on complex agent traces. Start by maximizing recall to ensure your compaction prompt captures every relevant piece of information from the trace, then iterate to improve precision by eliminating superfluous content. An example of low-hanging superfluous content is clearing tool calls and results – once a tool has been called deep in the message history, why would the agent need to see the raw result again? One of the safest lightest touch forms of compaction is tool result clearing, most recently launched as afeature on the Claude Developer Platform. Structured note-taking Structured note-taking, or agentic memory, is a technique where the agent regularly writes notes persisted to memory outside of the context window. These notes get pulled back into the context window at later times. This strategy provides persistent memory with minimal overhead. Like Claude Code creating a to-do list, or your custom agent maintaining a NOTES.md file, this simple pattern allows the agent to track progress across complex tasks, maintaining critical context and dependencies that would otherwise be lost across dozens of tool calls. Claude playing Pokémondemonstrates how memory transforms agent capabilities in non-coding domains. The agent maintains precise tallies across thousands of game steps—tracking objectives like "for the last 1,234 steps I've been training my Pokémon in Route 1, Pikachu has gained 8 levels toward the target of 10." Without any prompting about memory structure, it develops maps of explored regions, remembers which key achievements it has unlocked, and maintains strategic notes of combat strategies that help it learn which attacks work best against different opponents. After context resets, the agent reads its own notes and continues multi-hour training sequences or dungeon explorations. This coherence across summarization steps enables long-horizon strategies that would be impossible when keeping all the information in the LLM’s context window alone. As part of ourSonnet 4.5 launch, we releaseda memory toolin public beta on the Claude Developer Platform that makes it easier to store and consult information outside the context window through a file-based system. This allows agents to build up knowledge bases over time, maintain project state across sessions, and reference previous work without keeping everything in context. Sub-agent architectures Sub-agent architectures provide another way around context limitations. Rather than one agent attempting to maintain state across an entire project, specialized sub-agents can handle focused tasks with clean context windows. The main agent coordinates with a high-level plan while subagents perform deep technical work or use tools to find relevant information. Each subagent might explore extensively, using tens of thousands of tokens or more, but returns only a condensed, distilled summary of its work (often 1,000-2,000 tokens). This approach achieves a clear separation of concerns—the detailed search context remains isolated within sub-agents, while the lead agent focuses on synthesizing and analyzing the results. This pattern, discussed inHow we built our multi-agent research system, showed a substantial improvement over single-agent systems on complex research tasks. The choice between these approaches depends on task characteristics. For example:

这些现实意味着,深思熟虑的上下文工程对于构建强大智能体至关重要。

  • Compaction maintains conversational flow for tasks requiring extensive back-and-forth;
  • Note-taking excels for iterative development with clear milestones;
  • Multi-agent architectures handle complex research and analysis where parallel exploration pays dividends.

有效上下文的构成

Even as models continue to improve, the challenge of maintaining coherence across extended interactions will remain central to building more effective agents.

鉴于 LLM 受限于有限的注意力预算,良好的上下文工程意味着找到最小的高信号 Token 集合,以最大化期望结果的出现概率。说起来容易做起来难,但在下文中,我们将概述这一指导原则在上下文各组成要素中的实践意义。

Conclusion

系统提示应极其清晰,使用简单直接的语言,在合适的高度(Right Altitude)向智能体呈现信息。合适的高度是指介于两种常见失败模式之间的"金发姑娘区间"。在一个极端,我们看到工程师在提示中硬编码复杂、脆弱的逻辑来触发精确的智能体行为。这种方法会造成脆弱性,并随时间增加维护复杂度。在另一个极端,工程师有时提供模糊、笼统的指引,未能给 LLM 提供关于期望输出的具体信号,或错误地假设了共享上下文。最优高度在两者之间取得平衡:既要足够具体以有效引导行为,又要足够灵活以向模型提供有力的启发式规则来指导行为。

Context engineering represents a fundamental shift in how we build with LLMs. As models become more capable, the challenge isn't just crafting the perfect prompt—it's thoughtfully curating what information enters the model's limited attention budget at each step. Whether you're implementing compaction for long-horizon tasks, designing token-efficient tools, or enabling agents to explore their environment just-in-time, the guiding principle remains the same: find the smallest set of high-signal tokens that maximize the likelihood of your desired outcome. The techniques we've outlined will continue evolving as models improve. We're already seeing that smarter models require less prescriptive engineering, allowing agents to operate with more autonomy. But even as capabilities scale, treating context as a precious, finite resource will remain central to building reliable, effective agents. Get started with context engineering in the Claude Developer Platform today, and access helpful tips and best practices via ourmemory and context managementcookbook.

我们建议将提示组织成不同的部分(如 <background_information>、<instructions>、## Tool guidance、## Output description 等),并使用 XML 标签或 Markdown 标题来划分这些部分,尽管随着模型能力的提升,提示的精确格式可能变得不那么重要了。

Acknowledgements

<background_information>
<instructions>
## Tool guidance
## Output description

Written by Anthropic's Applied AI team: Prithvi Rajasekaran, Ethan Dixon, Carly Ryan, and Jeremy Hadfield, with contributions from team members Rafi Ayub, Hannah Moran, Cal Rueb, and Connor Jennings. Special thanks to Molly Vorwerck, Stuart Ritchie, and Maggie Vo for their support.

无论你如何组织系统提示,都应该力求用最少的信息完整勾勒出你期望的行为。(注意,最少并不意味着简短;你仍然需要预先为智能体提供充足的信息以确保其遵循期望行为。)最好的做法是先用最佳模型测试一个最简提示,观察其在你的任务上的表现,然后根据初始测试中发现的失败模式添加清晰的指令和示例来提升性能。

工具(Tools)允许智能体与其环境交互,并在工作过程中引入新的上下文。由于工具定义了智能体与其信息/操作空间之间的契约,工具在两方面促进效率至关重要:既要在返回信息时保持 Token 效率,又要鼓励高效的智能体行为。

在《为 AI 智体编写工具——由 AI 智能体编写》一文中,我们讨论了构建 LLM 易于理解且功能重叠最小的工具。与设计良好的代码库中的函数类似,工具应该是自包含的、对错误具有鲁棒性的,并且在用途上极其清晰。输入参数同样应具有描述性、无歧义,并发挥模型的固有能力。

我们观察到的最常见失败模式之一是工具集臃肿,涵盖了过多功能,或导致关于使用哪个工具的决策点模糊不清。如果一个人类工程师都无法明确判断在给定情况下应使用哪个工具,就不能指望 AI 智能体做得更好。正如我们稍后将讨论的,为智能体策展最小可行工具集也能带来更可靠的维护和更高效的上下文修剪。

提供示例,也称为少样本提示(Few-shot Prompting),是我们持续强烈推荐的知名最佳实践。然而,团队常常将大量边缘用例塞入提示中,试图阐明 LLM 在特定任务中应遵循的每一条规则。我们不建议这样做。相反,我们建议策展一组多样化的、典型的示例,来有效地展现智能体的期望行为。对于 LLM 来说,示例就是"一图胜千言"中的图片。

我们对上下文各组成部分(系统提示、工具、示例、消息历史等)的总体建议是:保持思考,让上下文既信息丰富又紧凑。现在,让我们深入探讨在运行时动态检索上下文。

上下文检索与智能体搜索

在《构建高效的 AI 智能体》一文中,我们强调了基于 LLM 的工作流与智能体之间的区别。自那篇文章发表以来,我们倾向于用一个简洁的定义来描述智能体:LLM 在循环中自主使用工具。

在与客户合作的过程中,我们看到业界正在趋向于这一简洁范式。随着底层模型变得更加强大,智能体的自主性水平也能相应提升:更智能的模型允许智能体独立应对细致入微的问题空间并从错误中恢复。

我们现在看到工程师在设计智能体上下文的方式上正在发生转变。如今,许多 AI 原生应用采用某种形式的基于嵌入(Embedding)的推理前检索来呈现重要上下文供智能体推理。随着业界向更具智能体特性的方法过渡,我们越来越多地看到团队用"即时"(Just-in-Time)上下文策略来增强这些检索系统。

采用"即时"方法构建的智能体不会预先处理所有相关数据,而是维护轻量级标识符(文件路径、存储的查询、网页链接等),并使用这些引用在运行时通过工具将数据动态加载到上下文中。Anthropic 的智能体编码解决方案 Claude Code 就采用了这种方法对大型数据库进行复杂数据分析。模型可以编写有针对性的查询、存储结果,并利用 Bash 命令(如 head 和 tail)来分析大量数据,而无需将完整数据对象加载到上下文中。这种方法反映了人类认知的工作方式:我们通常不会将整个信息库记忆下来,而是借助文件系统、收件箱、书签等外部组织和索引系统按需检索相关信息。

除了存储效率之外,这些引用的元数据提供了一种高效优化行为的机制,无论是显式提供的还是直觉性的。对于在文件系统中操作的智能体来说,tests 文件夹中名为 test_utils.py 的文件,与 src/core_logic/ 下同名文件所暗示的用途截然不同。文件夹层级结构、命名规范和时间戳都提供了重要信号,帮助人类和智能体理解如何以及何时使用信息。

让智能体自主导航和检索数据还支持渐进式披露(Progressive Disclosure)——换言之,允许智能体通过探索逐步发现相关上下文。每次交互都会产生上下文,为下一个决策提供信息:文件大小暗示复杂度;命名规范暗示用途;时间戳可以作为相关性的代理指标。智能体可以逐层构建理解,仅在工作记忆中保留必要内容,并利用笔记策略实现额外的持久化。这种自我管理的上下文窗口使智能体聚焦于相关子集,而非淹没在详尽但可能无关的信息中。

当然,这存在权衡:运行时探索比检索预计算数据更慢。不仅如此,还需要有主见且深思熟虑的工程设计来确保 LLM 拥有正确的工具和启发式方法来有效导航其信息环境。在缺乏适当引导的情况下,智能体可能会因误用工具、追逐死胡同或未能识别关键信息而浪费上下文。

在某些场景中,最有效的智能体可能采用混合策略:预先检索部分数据以提高速度,同时根据需要进行进一步的自主探索。"正确"自主程度的决策边界取决于具体任务。Claude Code 就是一个采用这种混合模型的智能体:CLAUDE.md 文件预先直接注入上下文,而 glob 和 grep 等原语允许它在环境中导航并即时检索文件,有效规避了索引过时和复杂语法树的问题。

混合策略可能更适合内容动态性较低的场景,例如法律或金融工作。随着模型能力的提升,智能体设计将趋向于让智能模型发挥智能,逐步减少人工策展。鉴于该领域的快速发展步伐,"做最简单且有效的事"可能仍然是我们为基于 Claude 构建智能体的团队提供的最佳建议。

长期任务的上下文工程

长期任务要求智能体在 Token 数量超过 LLM 上下文窗口的动作序列中保持连贯性、上下文和目标导向行为。对于持续数十分钟乃至数小时的任务(如大规模代码库迁移或综合研究项目),智能体需要专门的技术来应对上下文窗口大小的限制。

等待更大的上下文窗口可能看起来是一个显而易见的策略。但在可预见的未来,各种大小的上下文窗口都可能面临上下文污染(Context Pollution)和信息相关性问题——至少在需要最强智能体性能的场景中是如此。为使智能体能够在延长的时间跨度上有效工作,我们开发了几种直接应对这些上下文污染约束的技术:压缩(Compaction)、结构化笔记和多智能体架构。

压缩(Compaction)

压缩是指将接近上下文窗口限制的对话内容进行摘要总结,并以该摘要重新初始化一个新的上下文窗口。压缩通常作为上下文工程中提升长期连贯性的首要手段。其核心是以前高保真的方式提炼上下文窗口的内容,使智能体能够以最小的性能降级继续工作。

例如,在 Claude Code 中,我们通过将消息历史传递给模型来总结和压缩最关键的信息。模型会保留架构决策、未解决的 Bug 和实现细节,同时丢弃冗余的工具输出或消息。随后智能体可以基于这个压缩后的上下文加上最近访问的五个文件继续工作。用户获得连续性体验而无需担心上下文窗口限制。

压缩的艺术在于保留什么与丢弃什么的选择,因为过于激进的压缩可能导致微妙但关键的上下文丢失,而其重要性可能在稍后才会显现。对于实施压缩系统的工程师,我们建议在复杂的智能体轨迹上仔细调优你的提示。首先最大化召回率,确保压缩提示能捕捉轨迹中的每一条相关信息,然后通过消除多余内容来迭代提高精度。

清除工具调用和结果是一种简单的多余内容清理方式——一旦工具在消息历史深处被调用过,智能体为什么还需要再次查看原始结果?最安全、最轻量的压缩形式之一是工具结果清除(Tool Result Clearing),最近已作为 Claude Developer Platform 的一项功能发布。

结构化笔记(Structured Note-taking)

结构化笔记,也称为智能体记忆(Agentic Memory),是一种智能体定期将笔记写入上下文窗口之外的持久化存储的技术。这些笔记会在稍后被拉回上下文窗口。

这种策略以最小开销提供持久记忆。就像 Claude Code 创建待办事项列表,或你的自定义智能体维护一个 NOTES.md 文件一样,这个简单的模式允许智能体在复杂任务中跟踪进度,维护那些在数十次工具调用中可能丢失的关键上下文和依赖关系。

Claude 玩宝可梦(Pokémon)展示了记忆如何在非编码领域转变智能体的能力。智能体在数千个游戏步骤中维持精确的统计——跟踪目标,如"在过去的 1,234 步中,我一直在 1 号道路训练宝可梦,皮卡丘距离 10 级目标已提升 8 级"。在没有任何关于记忆结构的提示下,它会自主绘制已探索区域的地图,记住已解锁的关键成就,并维护战斗策略的笔记,帮助它学习哪些招式对不同对手最有效。

在上下文重置后,智能体会阅读自己的笔记并继续多小时的训练序列或地牢探索。这种跨摘要步骤的连贯性使得长期策略成为可能,而仅靠将所有信息保持在 LLM 上下文窗口中是无法实现的。

作为 Sonnet 4.5 发布的一部分,我们在 Claude Developer Platform 上发布了记忆工具(Memory Tool)的公开测试版,该工具通过基于文件的系统使存储和查阅上下文窗口之外的信息变得更加便捷。这使得智能体能够逐步构建知识库、跨会话维护项目状态,并在不必将所有内容保留在上下文中的情况下引用之前的工作。

子智能体架构(Sub-agent Architectures)

子智能体架构提供了另一种绕过上下文限制的方式。与其让一个智能体在整个项目中维护状态,不如让专门的子智能体在干净的上下文窗口中处理聚焦的任务。主智能体以高层计划进行协调,而子智能体执行深入的技术工作或使用工具查找相关信息。每个子智能体可能进行大量探索,使用数万个甚至更多 Token,但只返回浓缩、精炼的工作摘要(通常 1,000-2,000 Token)。

这种方法实现了清晰的关注点分离——详细的搜索上下文保持在子智能体内部隔离,而主智能体专注于综合和分析结果。在《我们如何构建多智能体研究系统》中讨论的这种模式,在复杂研究任务上相较单智能体系统展现了显著的性能提升。

这些方法之间的选择取决于任务特征。例如:

  • 压缩适用于需要大量反复交互的任务,保持对话流畅性;
  • 结构化笔记适合有明确里程碑的迭代开发;
  • 多智能体架构处理复杂的研接和分析任务,其中并行探索能带来收益。

即使模型持续改进,在扩展交互中保持连贯性这一挑战仍将是构建更有效智能体的核心。

结论

上下文工程代表了我们使用 LLM 进行开发方式的根本性转变。随着模型变得更加强大,挑战不仅仅在于精心编写完美的提示——更在于深思熟虑地策展在每个步骤中进入模型有限注意力预算的信息。无论你是在为长期任务实施压缩、设计 Token 高效的工具,还是让智能体即时探索环境,指导原则始终如一:找到最小的高信号 Token 集合,最大化期望结果的出现概率。

我们概述的技术将随着模型的改进而持续演进。我们已经看到,更智能的模型需要更少的规范性工程,允许智能体以更大的自主性运行。但即使能力在扩展,将上下文视为一种宝贵的有限资源,仍将是构建可靠、有效智能体的核心。

立即在 Claude Developer Platform 开始上下文工程实践,并通过我们的记忆和上下文管理 Cookbook 获取有用的技巧和最佳实践。

致谢

由 Anthropic 应用 AI 团队撰写:Prithvi Rajasekaran、Ethan Dixon、Carly Ryan 和 Jeremy Hadfield,团队成员 Rafi Ayub、Hannah Moran、Cal Rueb 和 Connor Jennings 亦有贡献。特别感谢 Molly Vorwerck、Stuart Ritchie 和 Maggie Vo 的支持。

在提示工程(Prompt Engineering)作为应用人工智能领域焦点数年之后,一个新术语逐渐崭露头角:上下文工程(Context Engineering)。基于语言模型的开发正在从寻找合适的提示词和短语,转向回答一个更根本的问题:"怎样的上下文配置最有可能让模型产生我们期望的行为?"

上下文(Context)指的是从大语言模型(LLM)采样时所包含的一组 Token。而工程(Engineering)问题的核心在于:如何在 LLM 固有约束下优化这些 Token 的效用,以持续达成预期结果。有效地驾驭 LLM 往往需要以上下文的方式进行思考——换言之,需要综合考量 LLM 在任意时刻所能获取的整体状态,以及该状态可能引发的潜在行为。

在本文中,我们将探索上下文工程这一新兴实践,并提供一个经过优化的心智模型,用于构建可控且高效的智能体(Agent)。

上下文工程与提示工程

在 Anthropic,我们将上下文工程视为提示工程的自然演进。提示工程指的是编写和组织 LLM 指令以获得最优结果的方法(参见我们的文档了解概览及实用的提示工程策略)。上下文工程则指在 LLM 推理过程中,策展和维护最优 Token(信息)集合的一整套策略,涵盖提示之外可能进入上下文的所有其他信息。

在 LLM 工程化的早期阶段,提示是 AI 工程工作的最大组成部分,因为除日常对话交互外,大多数用例都需要针对一次性分类或文本生成任务优化的提示。顾名思义,提示工程的核心关注点是如何编写有效的提示,尤其是系统提示。然而,随着我们转向构建更强大的、能够在多轮推理和更长时间跨度上运行的智能体,我们需要管理整个上下文状态的策略(系统指令、工具、模型上下文协议(Model Context Protocol, MCP)、外部数据、消息历史等)。

一个在循环中运行的智能体会产生越来越多可能与下一轮推理相关的数据,这些信息必须经过循环筛选和提炼。上下文工程就是从不断演化的海量可能信息中,策展出最适合进入有限上下文窗口的内容——这既是一门艺术,也是一门科学。

为什么上下文工程对构建强大智能体至关重要

尽管 LLM 处理速度极快且能管理越来越大的数据量,我们观察到 LLM 与人类一样,在达到某个临界点后会丧失注意力或产生困惑。"大海捞针"(Needle-in-a-Haystack)类基准测试的研究揭示了上下文衰退(Context Rot)的概念:随着上下文窗口中 Token 数量的增加,模型从该上下文中准确回忆信息的能力会下降。

虽然某些模型的衰退比其他模型更为平缓,但这一特征在所有模型中都会出现。因此,上下文必须被视为一种边际收益递减的有限资源。与工作记忆容量有限的人类类似,LLM 在解析大量上下文时也有一个"注意力预算"(Attention Budget)。每引入一个新 Token 都会消耗一部分预算,从而增加了精心策展可用 Token 的必要性。

这种注意力稀缺源于 LLM 的架构约束。LLM 基于 Transformer 架构,该架构使每个 Token 都能关注整个上下文中的其他所有 Token。对于 n 个 Token,这会产生 n² 的成对关系。

随着上下文长度的增加,模型捕获这些成对关系的能力会被逐渐拉薄,在上下文大小和注意力聚焦之间形成一种天然的张力。此外,模型从训练数据分布中习得注意力模式,而训练数据中较短的序列通常比较长的序列更常见。这意味着模型在上下文级依赖关系方面经验更少,专门参数也更少。

位置编码插值(Position Encoding Interpolation)等技术允许模型通过将较长序列适配到原始训练时较小的上下文来处理更长序列,但会带来一定程度的 Token 位置理解降级。这些因素创造的是一个性能梯度而非断崖:模型在更长上下文中仍然保持较强能力,但与较短上下文中的表现相比,信息检索和长程推理的精度可能会有所下降。

这些现实意味着,深思熟虑的上下文工程对于构建强大智能体至关重要。

有效上下文的构成

鉴于 LLM 受限于有限的注意力预算,良好的上下文工程意味着找到最小的高信号 Token 集合,以最大化期望结果的出现概率。说起来容易做起来难,但在下文中,我们将概述这一指导原则在上下文各组成要素中的实践意义。

系统提示应极其清晰,使用简单直接的语言,在合适的高度(Right Altitude)向智能体呈现信息。合适的高度是指介于两种常见失败模式之间的"金发姑娘区间"。在一个极端,我们看到工程师在提示中硬编码复杂、脆弱的逻辑来触发精确的智能体行为。这种方法会造成脆弱性,并随时间增加维护复杂度。在另一个极端,工程师有时提供模糊、笼统的指引,未能给 LLM 提供关于期望输出的具体信号,或错误地假设了共享上下文。最优高度在两者之间取得平衡:既要足够具体以有效引导行为,又要足够灵活以向模型提供有力的启发式规则来指导行为。

我们建议将提示组织成不同的部分(如 <background_information>、<instructions>、## Tool guidance、## Output description 等),并使用 XML 标签或 Markdown 标题来划分这些部分,尽管随着模型能力的提升,提示的精确格式可能变得不那么重要了。

<background_information>
<instructions>
## Tool guidance
## Output description

无论你如何组织系统提示,都应该力求用最少的信息完整勾勒出你期望的行为。(注意,最少并不意味着简短;你仍然需要预先为智能体提供充足的信息以确保其遵循期望行为。)最好的做法是先用最佳模型测试一个最简提示,观察其在你的任务上的表现,然后根据初始测试中发现的失败模式添加清晰的指令和示例来提升性能。

工具(Tools)允许智能体与其环境交互,并在工作过程中引入新的上下文。由于工具定义了智能体与其信息/操作空间之间的契约,工具在两方面促进效率至关重要:既要在返回信息时保持 Token 效率,又要鼓励高效的智能体行为。

在《为 AI 智体编写工具——由 AI 智能体编写》一文中,我们讨论了构建 LLM 易于理解且功能重叠最小的工具。与设计良好的代码库中的函数类似,工具应该是自包含的、对错误具有鲁棒性的,并且在用途上极其清晰。输入参数同样应具有描述性、无歧义,并发挥模型的固有能力。

我们观察到的最常见失败模式之一是工具集臃肿,涵盖了过多功能,或导致关于使用哪个工具的决策点模糊不清。如果一个人类工程师都无法明确判断在给定情况下应使用哪个工具,就不能指望 AI 智能体做得更好。正如我们稍后将讨论的,为智能体策展最小可行工具集也能带来更可靠的维护和更高效的上下文修剪。

提供示例,也称为少样本提示(Few-shot Prompting),是我们持续强烈推荐的知名最佳实践。然而,团队常常将大量边缘用例塞入提示中,试图阐明 LLM 在特定任务中应遵循的每一条规则。我们不建议这样做。相反,我们建议策展一组多样化的、典型的示例,来有效地展现智能体的期望行为。对于 LLM 来说,示例就是"一图胜千言"中的图片。

我们对上下文各组成部分(系统提示、工具、示例、消息历史等)的总体建议是:保持思考,让上下文既信息丰富又紧凑。现在,让我们深入探讨在运行时动态检索上下文。

上下文检索与智能体搜索

在《构建高效的 AI 智能体》一文中,我们强调了基于 LLM 的工作流与智能体之间的区别。自那篇文章发表以来,我们倾向于用一个简洁的定义来描述智能体:LLM 在循环中自主使用工具。

在与客户合作的过程中,我们看到业界正在趋向于这一简洁范式。随着底层模型变得更加强大,智能体的自主性水平也能相应提升:更智能的模型允许智能体独立应对细致入微的问题空间并从错误中恢复。

我们现在看到工程师在设计智能体上下文的方式上正在发生转变。如今,许多 AI 原生应用采用某种形式的基于嵌入(Embedding)的推理前检索来呈现重要上下文供智能体推理。随着业界向更具智能体特性的方法过渡,我们越来越多地看到团队用"即时"(Just-in-Time)上下文策略来增强这些检索系统。

采用"即时"方法构建的智能体不会预先处理所有相关数据,而是维护轻量级标识符(文件路径、存储的查询、网页链接等),并使用这些引用在运行时通过工具将数据动态加载到上下文中。Anthropic 的智能体编码解决方案 Claude Code 就采用了这种方法对大型数据库进行复杂数据分析。模型可以编写有针对性的查询、存储结果,并利用 Bash 命令(如 head 和 tail)来分析大量数据,而无需将完整数据对象加载到上下文中。这种方法反映了人类认知的工作方式:我们通常不会将整个信息库记忆下来,而是借助文件系统、收件箱、书签等外部组织和索引系统按需检索相关信息。

除了存储效率之外,这些引用的元数据提供了一种高效优化行为的机制,无论是显式提供的还是直觉性的。对于在文件系统中操作的智能体来说,tests 文件夹中名为 test_utils.py 的文件,与 src/core_logic/ 下同名文件所暗示的用途截然不同。文件夹层级结构、命名规范和时间戳都提供了重要信号,帮助人类和智能体理解如何以及何时使用信息。

让智能体自主导航和检索数据还支持渐进式披露(Progressive Disclosure)——换言之,允许智能体通过探索逐步发现相关上下文。每次交互都会产生上下文,为下一个决策提供信息:文件大小暗示复杂度;命名规范暗示用途;时间戳可以作为相关性的代理指标。智能体可以逐层构建理解,仅在工作记忆中保留必要内容,并利用笔记策略实现额外的持久化。这种自我管理的上下文窗口使智能体聚焦于相关子集,而非淹没在详尽但可能无关的信息中。

当然,这存在权衡:运行时探索比检索预计算数据更慢。不仅如此,还需要有主见且深思熟虑的工程设计来确保 LLM 拥有正确的工具和启发式方法来有效导航其信息环境。在缺乏适当引导的情况下,智能体可能会因误用工具、追逐死胡同或未能识别关键信息而浪费上下文。

在某些场景中,最有效的智能体可能采用混合策略:预先检索部分数据以提高速度,同时根据需要进行进一步的自主探索。"正确"自主程度的决策边界取决于具体任务。Claude Code 就是一个采用这种混合模型的智能体:CLAUDE.md 文件预先直接注入上下文,而 glob 和 grep 等原语允许它在环境中导航并即时检索文件,有效规避了索引过时和复杂语法树的问题。

混合策略可能更适合内容动态性较低的场景,例如法律或金融工作。随着模型能力的提升,智能体设计将趋向于让智能模型发挥智能,逐步减少人工策展。鉴于该领域的快速发展步伐,"做最简单且有效的事"可能仍然是我们为基于 Claude 构建智能体的团队提供的最佳建议。

长期任务的上下文工程

长期任务要求智能体在 Token 数量超过 LLM 上下文窗口的动作序列中保持连贯性、上下文和目标导向行为。对于持续数十分钟乃至数小时的任务(如大规模代码库迁移或综合研究项目),智能体需要专门的技术来应对上下文窗口大小的限制。

等待更大的上下文窗口可能看起来是一个显而易见的策略。但在可预见的未来,各种大小的上下文窗口都可能面临上下文污染(Context Pollution)和信息相关性问题——至少在需要最强智能体性能的场景中是如此。为使智能体能够在延长的时间跨度上有效工作,我们开发了几种直接应对这些上下文污染约束的技术:压缩(Compaction)、结构化笔记和多智能体架构。

压缩(Compaction)

压缩是指将接近上下文窗口限制的对话内容进行摘要总结,并以该摘要重新初始化一个新的上下文窗口。压缩通常作为上下文工程中提升长期连贯性的首要手段。其核心是以前高保真的方式提炼上下文窗口的内容,使智能体能够以最小的性能降级继续工作。

例如,在 Claude Code 中,我们通过将消息历史传递给模型来总结和压缩最关键的信息。模型会保留架构决策、未解决的 Bug 和实现细节,同时丢弃冗余的工具输出或消息。随后智能体可以基于这个压缩后的上下文加上最近访问的五个文件继续工作。用户获得连续性体验而无需担心上下文窗口限制。

压缩的艺术在于保留什么与丢弃什么的选择,因为过于激进的压缩可能导致微妙但关键的上下文丢失,而其重要性可能在稍后才会显现。对于实施压缩系统的工程师,我们建议在复杂的智能体轨迹上仔细调优你的提示。首先最大化召回率,确保压缩提示能捕捉轨迹中的每一条相关信息,然后通过消除多余内容来迭代提高精度。

清除工具调用和结果是一种简单的多余内容清理方式——一旦工具在消息历史深处被调用过,智能体为什么还需要再次查看原始结果?最安全、最轻量的压缩形式之一是工具结果清除(Tool Result Clearing),最近已作为 Claude Developer Platform 的一项功能发布。

结构化笔记(Structured Note-taking)

结构化笔记,也称为智能体记忆(Agentic Memory),是一种智能体定期将笔记写入上下文窗口之外的持久化存储的技术。这些笔记会在稍后被拉回上下文窗口。

这种策略以最小开销提供持久记忆。就像 Claude Code 创建待办事项列表,或你的自定义智能体维护一个 NOTES.md 文件一样,这个简单的模式允许智能体在复杂任务中跟踪进度,维护那些在数十次工具调用中可能丢失的关键上下文和依赖关系。

Claude 玩宝可梦(Pokémon)展示了记忆如何在非编码领域转变智能体的能力。智能体在数千个游戏步骤中维持精确的统计——跟踪目标,如"在过去的 1,234 步中,我一直在 1 号道路训练宝可梦,皮卡丘距离 10 级目标已提升 8 级"。在没有任何关于记忆结构的提示下,它会自主绘制已探索区域的地图,记住已解锁的关键成就,并维护战斗策略的笔记,帮助它学习哪些招式对不同对手最有效。

在上下文重置后,智能体会阅读自己的笔记并继续多小时的训练序列或地牢探索。这种跨摘要步骤的连贯性使得长期策略成为可能,而仅靠将所有信息保持在 LLM 上下文窗口中是无法实现的。

作为 Sonnet 4.5 发布的一部分,我们在 Claude Developer Platform 上发布了记忆工具(Memory Tool)的公开测试版,该工具通过基于文件的系统使存储和查阅上下文窗口之外的信息变得更加便捷。这使得智能体能够逐步构建知识库、跨会话维护项目状态,并在不必将所有内容保留在上下文中的情况下引用之前的工作。

子智能体架构(Sub-agent Architectures)

子智能体架构提供了另一种绕过上下文限制的方式。与其让一个智能体在整个项目中维护状态,不如让专门的子智能体在干净的上下文窗口中处理聚焦的任务。主智能体以高层计划进行协调,而子智能体执行深入的技术工作或使用工具查找相关信息。每个子智能体可能进行大量探索,使用数万个甚至更多 Token,但只返回浓缩、精炼的工作摘要(通常 1,000-2,000 Token)。

这种方法实现了清晰的关注点分离——详细的搜索上下文保持在子智能体内部隔离,而主智能体专注于综合和分析结果。在《我们如何构建多智能体研究系统》中讨论的这种模式,在复杂研究任务上相较单智能体系统展现了显著的性能提升。

这些方法之间的选择取决于任务特征。例如:

  • 压缩适用于需要大量反复交互的任务,保持对话流畅性;
  • 结构化笔记适合有明确里程碑的迭代开发;
  • 多智能体架构处理复杂的研接和分析任务,其中并行探索能带来收益。

即使模型持续改进,在扩展交互中保持连贯性这一挑战仍将是构建更有效智能体的核心。

结论

上下文工程代表了我们使用 LLM 进行开发方式的根本性转变。随着模型变得更加强大,挑战不仅仅在于精心编写完美的提示——更在于深思熟虑地策展在每个步骤中进入模型有限注意力预算的信息。无论你是在为长期任务实施压缩、设计 Token 高效的工具,还是让智能体即时探索环境,指导原则始终如一:找到最小的高信号 Token 集合,最大化期望结果的出现概率。

我们概述的技术将随着模型的改进而持续演进。我们已经看到,更智能的模型需要更少的规范性工程,允许智能体以更大的自主性运行。但即使能力在扩展,将上下文视为一种宝贵的有限资源,仍将是构建可靠、有效智能体的核心。

立即在 Claude Developer Platform 开始上下文工程实践,并通过我们的记忆和上下文管理 Cookbook 获取有用的技巧和最佳实践。

致谢

由 Anthropic 应用 AI 团队撰写:Prithvi Rajasekaran、Ethan Dixon、Carly Ryan 和 Jeremy Hadfield,团队成员 Rafi Ayub、Hannah Moran、Cal Rueb 和 Connor Jennings 亦有贡献。特别感谢 Molly Vorwerck、Stuart Ritchie 和 Maggie Vo 的支持。

After a few years of prompt engineering being the focus of attention in applied AI, a new term has come to prominence:context engineering. Building with language models is becoming less about finding the right words and phrases for your prompts, and more about answering the broader question of “what configuration of context is most likely to generate our model’s desired behavior?" Contextrefers to the set of tokens included when sampling from a large-language model (LLM). Theengineeringproblem at hand is optimizing the utility of those tokens against the inherent constraints of LLMs in order to consistently achieve a desired outcome. Effectively wrangling LLMs often requiresthinking in context— in other words: considering the holistic state available to the LLM at any given time and what potential behaviors that state might yield. In this post, we’ll explore the emerging art of context engineering and offer a refined mental model for building steerable, effective agents.

Context engineering vs. prompt engineering

At Anthropic, we view context engineering as the natural progression of prompt engineering. Prompt engineering refers to methods for writing and organizing LLM instructions for optimal outcomes (seeour docsfor an overview and useful prompt engineering strategies).Context engineeringrefers to the set of strategies for curating and maintaining the optimal set of tokens (information) during LLM inference, including all the other information that may land there outside of the prompts. In the early days of engineering with LLMs, prompting was the biggest component of AI engineering work, as the majority of use cases outside of everyday chat interactions required prompts optimized for one-shot classification or text generation tasks. As the term implies, the primary focus of prompt engineering is how to write effective prompts, particularly system prompts. However, as we move towards engineering more capable agents that operate over multiple turns of inference and longer time horizons, we need strategies for managing the entire context state (system instructions, tools,Model Context Protocol(MCP), external data, message history, etc). An agent running in a loop generates more and more data thatcouldbe relevant for the next turn of inference, and this information must be cyclically refined. Context engineering is theart and scienceof curating what will go into the limited context window from that constantly evolving universe of possible information.

Why context engineering is important to building capable agents

Despite their speed and ability to manage larger and larger volumes of data, we’ve observed that LLMs, like humans, lose focus or experience confusion at a certain point. Studies on needle-in-a-haystackstyle benchmarking have uncovered the concept ofcontext rot: as the number of tokens in the context window increases, the model’s ability to accurately recall information from that context decreases. While some models exhibit more gentle degradation than others, this characteristic emerges across all models. Context, therefore, must be treated as a finite resource with diminishing marginal returns. Like humans, who havelimited working memory capacity, LLMs have an “attention budget” that they draw on when parsing large volumes of context. Every new token introduced depletes this budget by some amount, increasing the need to carefully curate the tokens available to the LLM. This attention scarcity stems from architectural constraints of LLMs. LLMs are based on thetransformer architecture, which enables every token toattend to every other tokenacross the entire context. This results in n² pairwise relationships for n tokens. As its context length increases, a model's ability to capture these pairwise relationships gets stretched thin, creating a natural tension between context size and attention focus. Additionally, models develop their attention patterns from training data distributions where shorter sequences are typically more common than longer ones. This means models have less experience with, and fewer specialized parameters for, context-wide dependencies. Techniques likeposition encoding interpolationallow models to handle longer sequences by adapting them to the originally trained smaller context, though with some degradation in token position understanding. These factors create a performance gradient rather than a hard cliff: models remain highly capable at longer contexts but may show reduced precision for information retrieval and long-range reasoning compared to their performance on shorter contexts. These realities mean that thoughtful context engineering is essential for building capable agents.

The anatomy of effective context

Given that LLMs are constrained by a finite attention budget,goodcontext engineering means finding thesmallestpossibleset of high-signal tokens that maximize the likelihood of some desired outcome. Implementing this practice is much easier said than done, but in the following section, we outline what this guiding principle means in practice across the different components of context. System promptsshould be extremely clear and use simple, direct language that presents ideas at theright altitudefor the agent. The right altitude is the Goldilocks zone between two common failure modes. At one extreme, we see engineers hardcoding complex, brittle logic in their prompts to elicit exact agentic behavior. This approach creates fragility and increases maintenance complexity over time. At the other extreme, engineers sometimes provide vague, high-level guidance that fails to give the LLM concrete signals for desired outputs or falsely assumes shared context. The optimal altitude strikes a balance: specific enough to guide behavior effectively, yet flexible enough to provide the model with strong heuristics to guide behavior. We recommend organizing prompts into distinct sections (like<background_information>,<instructions>,## Tool guidance,## Output description, etc) and using techniques like XML tagging or Markdown headers to delineate these sections, although the exact formatting of prompts is likely becoming less important as models become more capable. <background_information> <instructions>

Tool guidance

Output description

Regardless of how you decide to structure your system prompt, you should be striving for the minimal set of information that fully outlines your expected behavior. (Note that minimal does not necessarily mean short; you still need to give the agent sufficient information up front to ensure it adheres to the desired behavior.) It’s best to start by testing a minimal prompt with the best model available to see how it performs on your task, and then add clear instructions and examples to improve performance based on failure modes found during initial testing. Toolsallow agents to operate with their environment and pull in new, additional context as they work. Because tools define the contract between agents and their information/action space, it’s extremely important that tools promote efficiency, both by returning information that is token efficient and by encouraging efficient agent behaviors. InWriting tools for AI agents – with AI agents, we discussed building tools that are well understood by LLMs and have minimal overlap in functionality. Similar to the functions of a well-designed codebase, tools should be self-contained, robust to error, and extremely clear with respect to their intended use. Input parameters should similarly be descriptive, unambiguous, and play to the inherent strengths of the model. One of the most common failure modes we see is bloated tool sets that cover too much functionality or lead to ambiguous decision points about which tool to use. If a human engineer can’t definitively say which tool should be used in a given situation, an AI agent can’t be expected to do better. As we’ll discuss later, curating a minimal viable set of tools for the agent can also lead to more reliable maintenance and pruning of context over long interactions. Providing examples, otherwise known as few-shot prompting, is a well known best practice that we continue to strongly advise. However, teams will often stuff a laundry list of edge cases into a prompt in an attempt to articulate every possible rule the LLM should follow for a particular task. We do not recommend this. Instead, we recommend working to curate a set of diverse, canonical examples that effectively portray the expected behavior of the agent. For an LLM, examples are the “pictures” worth a thousand words. Our overall guidance across the different components of context (system prompts,tools,examples,message history, etc) is to be thoughtful and keep your context informative, yet tight. Now let's dive into dynamically retrieving context at runtime.

Context retrieval and agentic search

InBuilding effective AI agents, we highlighted the differences between LLM-based workflows and agents. Since we wrote that post, we’ve gravitated towards asimple definitionfor agents: LLMs autonomously using tools in a loop. Working alongside our customers, we’ve seen the field converging on this simple paradigm. As the underlying models become more capable, the level of autonomy of agents can scale: smarter models allow agents to independently navigate nuanced problem spaces and recover from errors. We’re now seeing a shift in how engineers think about designing context for agents. Today, many AI-native applications employ some form of embedding-based pre-inference time retrieval to surface important context for the agent to reason over. As the field transitions to more agentic approaches, we increasingly see teams augmenting these retrieval systems with “just in time” context strategies. Rather than pre-processing all relevant data up front, agents built with the “just in time” approach maintain lightweight identifiers (file paths, stored queries, web links, etc.) and use these references to dynamically load data into context at runtime using tools. Anthropic’s agentic coding solutionClaude Codeuses this approach to perform complex data analysis over large databases. The model can write targeted queries, store results, and leverage Bash commands like head and tail to analyze large volumes of data without ever loading the full data objects into context. This approach mirrors human cognition: we generally don’t memorize entire corpuses of information, but rather introduce external organization and indexing systems like file systems, inboxes, and bookmarks to retrieve relevant information on demand. Beyond storage efficiency, the metadata of these references provides a mechanism to efficiently refine behavior, whether explicitly provided or intuitive. To an agent operating in a file system, the presence of a file namedtest_utils.pyin atestsfolder implies a different purpose than a file with the same name located insrc/core_logic/Folder hierarchies, naming conventions, and timestamps all provide important signals that help both humans and agents understand how and when to utilize information. test_utils.py tests src/core_logic/ Letting agents navigate and retrieve data autonomously also enables progressive disclosure—in other words, allows agents to incrementally discover relevant context through exploration. Each interaction yields context that informs the next decision: file sizes suggest complexity; naming conventions hint at purpose; timestamps can be a proxy for relevance. Agents can assemble understanding layer by layer, maintaining only what's necessary in working memory and leveraging note-taking strategies for additional persistence. This self-managed context window keeps the agent focused on relevant subsets rather than drowning in exhaustive but potentially irrelevant information. Of course, there's a trade-off: runtime exploration is slower than retrieving pre-computed data. Not only that, but opinionated and thoughtful engineering is required to ensure that an LLM has the right tools and heuristics for effectively navigating its information landscape. Without proper guidance, an agent can waste context by misusing tools, chasing dead-ends, or failing to identify key information. In certain settings, the most effective agents might employ a hybrid strategy, retrieving some data up front for speed, and pursuing further autonomous exploration at its discretion. The decision boundary for the ‘right’ level of autonomy depends on the task. Claude Code is an agent that employs this hybrid model:CLAUDE.mdfiles are naively dropped into context up front, while primitives like glob and grep allow it to navigate its environment and retrieve files just-in-time, effectively bypassing the issues of stale indexing and complex syntax trees. The hybrid strategy might be better suited for contexts with less dynamic content, such as legal or finance work. As model capabilities improve, agentic design will trend towards letting intelligent models act intelligently, with progressively less human curation. Given the rapid pace of progress in the field, "do the simplest thing that works" will likely remain our best advice for teams building agents on top of Claude.

Context engineering for long-horizon tasks

Long-horizon tasks require agents to maintain coherence, context, and goal-directed behavior over sequences of actions where the token count exceeds the LLM’s context window. For tasks that span tens of minutes to multiple hours of continuous work, like large codebase migrations or comprehensive research projects, agents require specialized techniques to work around the context window size limitation. Waiting for larger context windows might seem like an obvious tactic. But it's likely that for the foreseeable future, context windows of all sizes will be subject to context pollution and information relevance concerns—at least for situations where the strongest agent performance is desired. To enable agents to work effectively across extended time horizons, we've developed a few techniques that address these context pollution constraints directly: compaction, structured note-taking, and multi-agent architectures. Compaction Compaction is the practice of taking a conversation nearing the context window limit, summarizing its contents, and reinitiating a new context window with the summary. Compaction typically serves as the first lever in context engineering to drive better long-term coherence. At its core, compaction distills the contents of a context window in a high-fidelity manner, enabling the agent to continue with minimal performance degradation. In Claude Code, for example, we implement this by passing the message history to the model to summarize and compress the most critical details. The model preserves architectural decisions, unresolved bugs, and implementation details while discarding redundant tool outputs or messages. The agent can then continue with this compressed context plus the five most recently accessed files. Users get continuity without worrying about context window limitations. The art of compaction lies in the selection of what to keep versus what to discard, as overly aggressive compaction can result in the loss of subtle but critical context whose importance only becomes apparent later. For engineers implementing compaction systems, we recommend carefully tuning your prompt on complex agent traces. Start by maximizing recall to ensure your compaction prompt captures every relevant piece of information from the trace, then iterate to improve precision by eliminating superfluous content. An example of low-hanging superfluous content is clearing tool calls and results – once a tool has been called deep in the message history, why would the agent need to see the raw result again? One of the safest lightest touch forms of compaction is tool result clearing, most recently launched as afeature on the Claude Developer Platform. Structured note-taking Structured note-taking, or agentic memory, is a technique where the agent regularly writes notes persisted to memory outside of the context window. These notes get pulled back into the context window at later times. This strategy provides persistent memory with minimal overhead. Like Claude Code creating a to-do list, or your custom agent maintaining a NOTES.md file, this simple pattern allows the agent to track progress across complex tasks, maintaining critical context and dependencies that would otherwise be lost across dozens of tool calls. Claude playing Pokémondemonstrates how memory transforms agent capabilities in non-coding domains. The agent maintains precise tallies across thousands of game steps—tracking objectives like "for the last 1,234 steps I've been training my Pokémon in Route 1, Pikachu has gained 8 levels toward the target of 10." Without any prompting about memory structure, it develops maps of explored regions, remembers which key achievements it has unlocked, and maintains strategic notes of combat strategies that help it learn which attacks work best against different opponents. After context resets, the agent reads its own notes and continues multi-hour training sequences or dungeon explorations. This coherence across summarization steps enables long-horizon strategies that would be impossible when keeping all the information in the LLM’s context window alone. As part of ourSonnet 4.5 launch, we releaseda memory toolin public beta on the Claude Developer Platform that makes it easier to store and consult information outside the context window through a file-based system. This allows agents to build up knowledge bases over time, maintain project state across sessions, and reference previous work without keeping everything in context. Sub-agent architectures Sub-agent architectures provide another way around context limitations. Rather than one agent attempting to maintain state across an entire project, specialized sub-agents can handle focused tasks with clean context windows. The main agent coordinates with a high-level plan while subagents perform deep technical work or use tools to find relevant information. Each subagent might explore extensively, using tens of thousands of tokens or more, but returns only a condensed, distilled summary of its work (often 1,000-2,000 tokens). This approach achieves a clear separation of concerns—the detailed search context remains isolated within sub-agents, while the lead agent focuses on synthesizing and analyzing the results. This pattern, discussed inHow we built our multi-agent research system, showed a substantial improvement over single-agent systems on complex research tasks. The choice between these approaches depends on task characteristics. For example:

  • Compaction maintains conversational flow for tasks requiring extensive back-and-forth;
  • Note-taking excels for iterative development with clear milestones;
  • Multi-agent architectures handle complex research and analysis where parallel exploration pays dividends.

Even as models continue to improve, the challenge of maintaining coherence across extended interactions will remain central to building more effective agents.

Conclusion

Context engineering represents a fundamental shift in how we build with LLMs. As models become more capable, the challenge isn't just crafting the perfect prompt—it's thoughtfully curating what information enters the model's limited attention budget at each step. Whether you're implementing compaction for long-horizon tasks, designing token-efficient tools, or enabling agents to explore their environment just-in-time, the guiding principle remains the same: find the smallest set of high-signal tokens that maximize the likelihood of your desired outcome. The techniques we've outlined will continue evolving as models improve. We're already seeing that smarter models require less prescriptive engineering, allowing agents to operate with more autonomy. But even as capabilities scale, treating context as a precious, finite resource will remain central to building reliable, effective agents. Get started with context engineering in the Claude Developer Platform today, and access helpful tips and best practices via ourmemory and context managementcookbook.

Acknowledgements

Written by Anthropic's Applied AI team: Prithvi Rajasekaran, Ethan Dixon, Carly Ryan, and Jeremy Hadfield, with contributions from team members Rafi Ayub, Hannah Moran, Cal Rueb, and Connor Jennings. Special thanks to Molly Vorwerck, Stuart Ritchie, and Maggie Vo for their support.