PULSE
AI 认知方法 HOWTO重要度 62

生产级AI智能体的四层上下文工程实践

AWS · 2026-11-04 · 原题《Four layers of context engineering for production AI agents: How Deep Insight applies them in working code | Amazon Web Services》 · 原文约 8 分钟 · 阅读原文 ↗
生产级AI智能体 · 四层上下文工程 核心问题:上下文窗口溢出 → 四层递进补偿 1 架构隔离 独立上下文窗口 共享字典传压缩消息 2 提示控制 设定输出token预算 限制回传信息量 3 文件外化 重型内容存文件 智能体只回摘要 4 验证防护 独立验证智能体 最后一道防线 每层补偿上一层的遗留局限 验证效果(Deep Insight 开源实现) Coder 上下文 ≤ 25K token 写执行工具 token 减少约 95% 系统提示缩小约 97% 按需加载技能:不全量放入系统提示 可落地的上下文工程实践 四层协同,而非孤立技巧的集合
作者主张通过架构隔离、提示控制、文件外化和独立验证四层上下文工程,解决生产级AI智能体上下文窗口溢出的瓶颈,并在Deep Insight开源实现中验证了显著效果。

做法与步骤

  1. 第一层架构隔离:为每个专业智能体分配独立上下文窗口,仅通过共享字典传递压缩后的消息、线索、计划和历史,使最耗上下文的Coder智能体保持在25K token以内。
  2. 第二层提示控制:在提示中为每个智能体设定明确的输出token预算(如Coder 1000-1500、Validator 800),限制返回给Supervisor的信息量,防止长响应污染上下文。
  3. 第三层文件外化:将重型内容(如分析代码、验证结果)保存到文件系统,智能体只返回摘要,参考实现中单个写执行工具将每个脚本添加到上下文的token减少约95%。
  4. 第四层验证与防护:用独立验证智能体和防护措施捕获错误、防止上下文溢出,作为最后一道防线。
  5. 按需加载技能:将技能按需加载而非全部放入系统提示,使系统提示缩小约97%。
  6. 四层相互补偿:每层解决前一层遗留的局限,共同构成可落地的上下文工程实践,而非孤立技巧的集合。

对实践的启示

对构建生产级AI智能体的团队而言,应把上下文工程视为分层架构问题:先做多智能体隔离,再用提示约束输出,接着把重型内容外化到文件,最后用独立验证兜底。按需加载技能和设定token预算能大幅降低成本和延迟。

作者承认的反方与局限

作者承认单靠架构隔离不够,单靠提示也不够,每层都有其局限,需要后续层补偿;但未讨论四层全部实施后的额外复杂度和运维成本。

原文摘引

As the number of tokens in the context window increases, the model’s ability to accurately recall information from that context decreases (Context rot).
Start with the strongest model and a simple prompt. Then incrementally add specific instructions based on actually observed failure cases.
▸ 展开原文全文(43 段,抓取自 AWS)

Artificial Intelligence

Four layers of context engineering for production AI agents: How Deep Insight applies them in working code

When production AI agents handle tasks such as data analysis and report generation, context management can become a major bottleneck. A 200K-token window sounds generous until a real-world task burns through most of it in minutes. Data analysis and report generation leave the agent failing mid-execution, losing accuracy, or driving up costs. A body of published guidance already addresses this, but the individual techniques are easier to read about than to apply. Applied together, they make a measurable difference: in the reference implementation this post walks through, a single write-and-execute tool cuts the tokens each script adds to context by about 95 percent, and loading skills on demand shrinks the system prompt by about 97 percent.

This series organizes that guidance into practices you can apply directly. Anthropic’s engineering posts, together with features built into the Strands Agents SDK, converge on four layers of context engineering: isolate context at the architecture level, control it through prompts, externalize it to the filesystem, and verify it with independent agents and safeguards. In this post (Part 2), you will learn how each layer works and what it looks like in working code. We applied these techniques in Deep Insight, an open source multi-agent data analysis solution built on the Strands Agents SDK and Amazon Bedrock AgentCore, which serves as the reference implementation throughout.

Post series

- Part 1: 5 production challenges in multi-agent systems and how Deep Insight solved them.

- Part 2: Four layers of context engineering for production AI agents: How Deep Insight applies them in working code.

- Part 3: Harness engineering in practice: moving Deep Insight from dev to production. (Coming soon!)

The first post defined five core challenges that production multi-agent systems must solve. It introduced how Strands Agents SDK, Amazon Bedrock and Bedrock AgentCore, and AWS Fargate each address different layers of the problem, with code examples. This second post takes the four layers of context engineering practice that sit on top of that architecture and shows each one implemented. The third post guides you through deploying and operating agents in a secure private network on Amazon Virtual Private Cloud (Amazon VPC), covering everything needed for real-world operations.

What is context engineering?

Context engineering is the discipline of optimizing the information in a large language model (LLM)’s limited context window so agents perform at their best. Anthropic Claude Sonnet 4.5, available on Amazon Bedrock, offers a 200K context window, but complex production tasks quickly exceed it. For example, a seemingly straightforward request like “Analyze sales data and create a DOCX report” runs through several stages: data loading and exploration (5 min), category-based sales analysis (3 min), generating five charts for time-series analysis (10 min), statistical validation (3 min), and final report generation (5 min). Together they consume over 150K tokens across 15 minutes.

Processing such tasks in a single context causes problems: the window overflows mid-task, stale context degrades performance, and errors require re-running the whole pipeline. As context grows, attention to important information drops, and inefficiency raises cost under per-token pricing.

“As the number of tokens in the context window increases, the model’s ability to accurately recall information from that context decreases (Context rot).”

— Anthropic, “Effective Context Engineering for AI Agents” (2025.09)

Context engineering solves exactly these problems, and it is not only a collection of techniques. Spanning architecture design, prompt writing, tool implementation, and verification, it’s a layered discipline where each layer compensates for what the previous ones miss. This post details the four-layer approach you can apply to manage context efficiently.

How to handle tasks that exceed the context window

Anthropic has published several techniques for handling long-horizon tasks. Deep Insight groups these techniques into four layers, with each layer addressing the limitations of the others. The following table sets out the four layers.

| Layer | Practice | Core Principle | Limitations |

| Layer 1: Architecture | Isolating Context Through Multi-Agent Architecture | Each agent operates in an independent context | When isolation alone is not enough |

| Layer 2: Prompt | Output Control and Behavioral Rules | Limiting context intake through prompts | When prompts alone are not enough |

| Layer 3: Tools | File-based Context Externalization | Keeping heavy content in files outside the context | When issues still arise |

| Layer 4: Verification Agents and Safeguards | Independent Verification and Safeguards | Catching errors and preventing context overflow | Final line of defense |

The sections that follow take each layer in order, stating what the layer prescribes, why the preceding layers alone are insufficient, and how the practice is implemented in Deep Insight.

1. Isolating context through multi-agent architecture

Give each specialized agent its own context window and pass only compressed results to the orchestrator. Deep Insight does this with a hierarchy of Coordinator, Planner, Plan Reviewer, Supervisor, and Tool Agents (Coder, Validator, Reporter, Tracker, Auditor).

With a single agent, all information, from user requests through data exploration, analysis, chart generation, validation, and reporting, accumulates in one context and easily exceeds the window. Giving each agent its own context keeps even the Coder, which uses the most, under 25K tokens total. Each agent receives only what it needs and signals others with a completion notice and brief summary.

The key to isolation is minimizing shared information. Deep Insight uses a Python dictionary, shared_state, that stores only four things:

- messages : The previous agent’s final message (not the full conversation history).

- clues : Compressed summaries collected from each agent.

- full_plan : A checklist generated by Planner and updated by Tracker.

- history : A brief log of agent names and messages.

The clues use a fixed CLUES_FORMAT: instead of each agent’s full conversation (tens of thousands of tokens), only compressed core results are passed.

This pattern is the one recommended in Anthropic’s “How We Built Our Multi-Agent Research System” post, which describes an orchestrator-worker pattern in which a lead agent delegates to specialized subagents, with each agent working only within its own context. You do not need to build it yourself: the agents-as-tools pattern in the Strands Agents SDK provides this isolation at the framework level.

2. Controlling context inflow through prompts

Layer 1 isolation alone is not sufficient: even with independent contexts, an agent that generates long responses inflates the Supervisor’s context. Layer 2 limits each agent’s output through prompts.

For Deep Insight, we use prompts to limit how much information each agent returns to the Supervisor. Three rules do most of the work, as Deep Insight’s prompts show.

“Start with the strongest model and a simple prompt. Then incrementally add specific instructions based on actually observed failure cases.”

— Anthropic, “Effective Context Engineering for AI Agents” (2025.09)

Implementation detail 2-1: Explicit per-agent output token budgets

Each agent’s prompt specifies a maximum response token count, preventing long responses that pollute the Supervisor’s context.

| Agent | Output Token Budget | Reason |

| Coder | 1,000–1,500 tokens | Analysis code is saved to files, so the agent returns only a summary of state, completed tasks, and insights |

| Validator | 800 tokens | Validation results are saved to citations.json . The agent returns only a summ