上下文是预算不是仓库Context is a budget, not a warehouse
窗口再大注意力也会稀释:静态前置、按需检索、压缩与笔记,把每个 token 花在高信号处。
Bigger windows still dilute attention — static content up front, just-in-time retrieval, compaction and notes spend tokens where signal is highest.
提示词效果未实测。Prompt effect has not been tested.
提示词是可复跑的模板,不是效果承诺。换模型、换版本、换输入都可能改变结果;用本页的检查点自己核对,并把官方出处当作机制依据而不是效果保证。A prompt is a re-runnable template, not a promise of results. Changing the model, version or input can change the outcome. Use the checkpoints on this page to verify it yourself, and treat the official sources as evidence of mechanism rather than a guarantee of effect.
这一篇讲什么What this guide covers
上下文工程管的是:每次推理时,窗口里该放哪些 token。 Anthropic 官方定义:提示词工程写指令,上下文工程在推理过程中 策划并维护最优的 token 集合——系统指令、工具、外部数据、消息历史都算。 窗口是预算,不是仓库:塞得越多,不等于用得越好。 效果未实测;以下机制均出自官方文档与论文。
Context engineering decides which tokens belong in the window at each inference. Anthropic's official definition: prompt engineering writes instructions; context engineering curates and maintains the optimal set of tokens during inference — system instructions, tools, external data, message history all count. The window is a budget, not a warehouse: stuffing more in does not mean getting more out. We have not run hands-on tests; the mechanisms below come from official docs and papers.
注意力预算与位置效应Attention budget and position effects
Anthropic 把这种现象叫 context rot: 上下文 token 越多,模型从中准确召回信息的能力越差。 根源在 transformer 架构:n 个 token 有 n² 对注意关系, 序列越长,捕捉这些关系的能力被摊得越薄。 训练数据里短序列更多,模型对长程依赖的经验也更少。
Lost in the Middle 论文在多文档问答与键值检索两个任务上发现: 相关信息在开头或结尾时表现最好,落在长上下文中段时显著变差。 显式支持长上下文的模型也一样。 结论:关键信息别埋在中段,窗口大不等于处处好使。
Anthropic calls the phenomenon context rot: as context tokens grow, the model's ability to accurately recall information from that context declines. The root is the transformer architecture: n tokens form n² pairwise attention relations, and the longer the sequence, the thinner the capacity to capture them gets stretched. Short sequences dominate training data, so models also have less experience with long-range dependencies.
The Lost in the Middle paper tested multi-document QA and key-value retrieval: performance is highest when relevant information sits at the beginning or end, and degrades significantly when it lands in the middle of a long context. Explicitly long-context models behave the same way. Takeaway: do not bury key information mid-context; a big window is not uniformly good.
静态前置,动态后置Static first, dynamic last
OpenAI 的提示词缓存按前缀匹配: 请求先查提示词开头部分是否已缓存,命中就复用,未命中则全量处理并缓存前缀。 超过 1024 token 自动启用,不用改请求。 由此推出摆法:指令、示例、工具定义放前面,用户相关的变化内容放最后。
三个细节决定命中率: 工具定义与顺序必须逐次完全一致; 多轮历史只在 messages 数组末尾追加,不重排; 响应里的 cached_tokens 字段可查命中量,长期不用的前缀会被逐出。 缓存按组织隔离;官方说明缓存过程不存储数据,符合零数据保留条件。
OpenAI's prompt caching matches on prefixes: a request first checks whether the beginning of the prompt is already cached; a hit reuses it, a miss processes everything and caches the prefix. It activates automatically above 1024 tokens — no request changes needed. The placement rule follows: instructions, examples, and tool definitions up front; user-specific variable content at the end.
Three details decide the hit rate: tool definitions and their order must be exactly identical across requests; multi-turn history is only appended to the end of the messages array, never reordered; the cached_tokens field in the response reports hits, and prefixes unused for a while are evicted. Caching is organization-scoped; the official docs state no data is stored during the process, making it eligible for zero data retention.
按需检索与渐进披露Just-in-time retrieval and progressive disclosure
另一种省预算的方式:不预载数据,只保留轻量标识符。 文件路径、存储的查询、链接,运行时用工具按需加载。 Anthropic 以 Claude Code 为例: 写定向查询、用 head/tail 看片段,大数据对象从不整体进窗口。 标识符的元数据自带信号:目录、命名、时间戳暗示用途与相关性。 agent 由此逐层探索,工作记忆只留当前需要的部分。
取舍官方也写明了:运行时探索比预检索慢; 缺引导时 agent 会滥用工具、追死胡同、漏掉关键信息。 混合策略常见:一部分数据预载求快,其余留给自主探索。
Another way to save budget: do not pre-load data — keep lightweight identifiers. File paths, stored queries, links; load on demand at runtime with tools. Anthropic cites Claude Code as its example: write targeted queries, inspect slices with head/tail, and never load whole data objects into the window. Identifier metadata carries signal: directories, naming, and timestamps hint at purpose and relevance. The agent explores layer by layer, keeping only what is currently needed in working memory.
The official trade-off is explicit: runtime exploration is slower than pre-computed retrieval; without guidance the agent misuses tools, chases dead ends, and misses key information. Hybrid strategies are common: pre-load some data for speed, leave the rest to autonomous exploration.
长任务的三种手法Three techniques for long tasks
任务超过窗口时,Anthropic 给了三条路:
- 压缩(compaction):接近上限时摘要历史、重开窗口。先调召回再调精度;清理旧工具结果是最轻的一档。
- 结构化笔记:agent 定期把笔记写到窗口外,之后再读回。进度与依赖不因窗口重置而丢。
- 子代理:各子代理用干净窗口做深度探索,只回传蒸馏后的摘要,主代理管综合。
选择按任务特征:压缩保对话连贯,笔记适合有里程碑的迭代开发, 子代理适合并行探索有收益的研究分析。
When a task outgrows the window, Anthropic offers three routes:
- Compaction: summarize history near the limit and reinitialize the window. Tune recall first, then precision; clearing old tool results is the lightest touch.
- Structured note-taking: the agent periodically writes notes outside the window and reads them back later. Progress and dependencies survive window resets.
- Sub-agents: each explores deeply in a clean window and returns only a distilled summary; the lead agent synthesizes.
Choose by task shape: compaction preserves conversational flow, notes suit milestone-driven iterative development, and sub-agents suit research and analysis where parallel exploration pays off.
失败模式Failure modes
- 边缘情况清单:把每条规则都塞进提示词。官方建议改用多样、典型的示例。
- 工具集膨胀:功能重叠、选择模糊。人都分不清该用哪个工具,agent 更分不清。
- 压缩过狠:丢掉当时不起眼、后来才关键的细节。压缩提示要在复杂轨迹上调。
- 等更大窗口:官方判断是窗口再大也有相关性问题,策展省不掉。
系统提示本身也有两个极端:硬编码 if-else 太脆,空泛指引太虚。 官方建议从最小提示起步,按失败模式逐步加指令和示例。
- Edge-case laundry lists: stuffing every rule into the prompt. The official advice is diverse, canonical examples instead.
- Bloated tool sets: overlapping functionality, ambiguous choices. If a human cannot say which tool to use, the agent cannot do better.
- Over-aggressive compaction: dropping details that look trivial now but matter later. Tune the compaction prompt on complex traces.
- Waiting for bigger windows: the official judgment is that relevance problems persist at any size — curation cannot be skipped.
System prompts have two extremes of their own: hardcoded if-else logic is brittle, vague guidance is empty. The official advice: start minimal, then add instructions and examples based on observed failure modes.
出处Sources
- Anthropic Engineering · Effective context engineering for AI agents
- OpenAI Cookbook · Prompt Caching 101
- Lost in the Middle(arXiv 2307.03172)
核验日 2026-11-07,三条均直抓成功。 OpenAI 文档主站本次网络不可达,官方内容取自其官方仓库 raw 文件。
- Anthropic Engineering · Effective context engineering for AI agents
- OpenAI Cookbook · Prompt Caching 101
- Lost in the Middle (arXiv 2307.03172)
Verified 2026-11-07; all three fetched directly with HTTP 200. The OpenAI doc site was unreachable from this network; official content was taken from a raw file in its official repository.
出处Sources
| 来源Source | 链接URL |
|---|---|
| Anthropic Engineering · Effective context engineering for AI agents(2026-11-07 直抓 200)Anthropic Engineering · Effective context engineering for AI agents (direct fetch 200 on 2026-11-07) | https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agentshttps://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents |
| OpenAI Cookbook · Prompt Caching 101(官方仓库 raw 文件,2026-11-07 直抓 200)OpenAI Cookbook · Prompt Caching 101 (official repo raw file, direct fetch 200 on 2026-11-07) | https://raw.githubusercontent.com/openai/openai-cookbook/main/examples/Prompt_Caching101.ipynbhttps://raw.githubusercontent.com/openai/openai-cookbook/main/examples/Prompt_Caching101.ipynb |
| Lost in the Middle 论文(arXiv 2307.03172,2026-11-07 直抓摘要页)Lost in the Middle paper (arXiv 2307.03172, abstract page direct-fetched 2026-11-07) | https://arxiv.org/abs/2307.03172https://arxiv.org/abs/2307.03172 |
资料快照日期:2026-11-07;单项复核范围与日期见对应正文或来源记录。产品功能与条款可能变化,使用前请核对原始来源。Source snapshot: 2026-11-07; item-specific rechecks and their scope are recorded in the text or source notes. Features and terms may change; check the original sources before use.