Deep Insight explains how four context engineering layers are used in working code for production AI agents.
Four context layers isolate agents, limit prompts, use files, and verify outputs to keep production AI agents within 200,000 tokens.
Key Takeaways
- Deep Insight applies four context-engineering layers—architecture, prompts, tools, and verification—to keep production AI agents within a 200,000-token window.
- Long data-analysis workflows can consume over 150,000 tokens in a single context, causing overflow, degraded accuracy, and higher costs.
- Each specialized agent runs in its own isolated context and shares only compressed summaries, preventing unnecessary token accumulation.
- Prompts enforce strict output limits and standardized formats, while tools move heavy code and data into files instead of keeping them in context.
For builders who run production AI agents on Amazon Bedrock, the second part of the Deep Insight guide shows how context engineering for production AI agents can keep long data-analysis tasks inside a 200,000-token window. Drawing on Anthropic’s September 2025 guidance and the open source Deep Insight reference implementation, built on the Strands Agents SDK and Amazon Bedrock AgentCore, the guide reports that a single write-and-execute tool cuts the tokens each script adds to context by about 95 percent, while loading skills on demand shrinks the system prompt by about 97 percent.
Why a 200,000-token window still runs short
Context engineering means controlling what a large language model (LLM) sees during a task so the agent stays accurate, affordable, and able to finish. Anthropic Claude Sonnet 4.5, available on Amazon Bedrock, offers a 200,000 context window, but complex production work can exhaust it quickly.
A request such as “Analyze sales data and create a DOCX report” moves through several stages: data loading and exploration for 5 minutes, category-based sales analysis for 3 minutes, generating five charts for time-series analysis for 10 minutes, statistical validation for 3 minutes, and final report generation for 5 minutes. Together, those stages consume over 150,000 tokens across 15 minutes.
When all of that happens in one context, the window can overflow mid-task, stale information can reduce quality, and errors can force the whole pipeline to run again. As context grows, attention to important information drops, and inefficiency raises cost under per-token pricing. Anthropic describes this recall problem as context rot.
The four-layer approach used by Deep Insight
Deep Insight groups Anthropic’s guidance into four layers. Each layer addresses a weakness left by the previous one: architecture isolates work, prompts limit what agents return, tools move heavy content into files, and verification agents and safeguards catch the remaining failures.
Layer 1: Architecture gives each agent its own window
With a single agent, every step accumulates in one context: user requests, data exploration, analysis, chart generation, validation, and reporting. Deep Insight avoids that by using a hierarchy of Coordinator, Planner, Plan Reviewer, Supervisor, and Tool Agents: Coder, Validator, Reporter, Tracker, and Auditor.
Each specialized agent works in an independent context. Even the Coder, which needs the most room, stays under 25,000 tokens total. Agents receive only the information they need and pass back a completion notice and brief summary.
The key is minimizing shared information. Deep Insight uses a Python dictionary named shared_state that stores only four items:
- 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.
Clues use a fixed CLUES_FORMAT so only compressed core results move between agents. This follows the orchestrator-worker pattern described in Anthropic’s “How We Built Our Multi-Agent Research System” post, where a lead agent delegates to specialized subagents and each agent works inside its own context. The agents-as-tools pattern in the Strands Agents SDK provides that isolation at the framework level.
Layer 2: Prompts limit what leaves each agent
Isolation alone is not enough. If an agent returns a long response, it can still inflate the Supervisor’s context. Layer 2 uses prompts to control output.
Deep Insight sets explicit token budgets for each agent. The Coder returns 1,000 to 1,500 tokens because its analysis code is saved to files, so the agent only reports state, completed tasks, and insights. The Validator returns 800 tokens because validation results are saved to citations.json. The Reporter returns 1,000 tokens because it generates the report as a DOCX file and only summarizes the result.
All agents use one standardized format: Status, Completed Tasks, Key Insights, and Generated Files. This gives the Supervisor a compact, predictable update without dragging the agent’s full reasoning history into context.
The Coder’s prompt also enforces a self-contained code philosophy. It states that variables do not persist between scripts. Every script must include all imports and load data directly. That removes the need to keep past code in context just to reuse a variable from an earlier step.
The Supervisor’s prompt adds a completion rule: do not move to the next agent until all checklist items in the current agent’s section are marked complete. That rule is kept in the prompt because judging whether the Coder’s work is finished requires interpreting the plan alongside Tracker updates, which is difficult to express in code alone.
Anthropic’s “Writing Effective Tools for Agents” post suggests giving tools a CONCISE or DETAILED response option. Deep Insight applies the same idea one level up, through each agent’s system prompt. Anthropic’s guidance also recommends starting with a strong model and a simple prompt, then adding specific instructions based on observed failure cases.
Layer 3: Tools move heavy content into files
Architecture and prompts reduce context inflow, but they cannot remove all heavy work. When the Coder generates 100 lines of Python code, that code can enter context if the agent has to pass it as a message. Even with “respond briefly” instructions, some code cannot be written briefly.
Deep Insight solves this with file-based externalization. The rule is simple: keep pointers in context, keep actual data in files.
The conventional approach uses separate file-write and execute tools. That can add 100 lines directly to context for each script, accumulating over 1,000 lines across a task. The fix is to collapse the two steps into a single tool.
The Coder agent uses write_and_execute_tool. It writes the script, runs it, and returns only a summary: that 105 lines were written, execution succeeded, and analysis of five categories completed. The code stays in the file, and about three lines of summary remain in context. Based on this example, the tool saves about 95 percent of the tokens.
Loading skills on demand is another part of the same externalization idea. Instead of carrying every capability in the system prompt at once, Deep Insight brings in what is needed when it is needed, shrinking the system prompt by about 97 percent.
Layer 4: Verification agents and safeguards close the loop
The final layer handles errors that survive the first three. Verification agents and independent safeguards are used to catch mistakes and prevent context overflow before they become larger failures.
In Deep Insight, this layer works through roles such as Validator and Auditor. The Validator saves validation results to citations.json and returns only a summary, keeping evidence out of the shared context. The architecture therefore separates work, compresses output, externalizes heavy content, and checks results independently.
What this changes for builders
The practical effect is fewer mid-task failures, less stale context, lower cost, and less need to rerun an entire pipeline after one step breaks. For teams building production AI agents, the message is not simply “use a bigger model.” It is to design context as a system: separate responsibilities, constrain outputs, move heavy payloads outside the window, and verify what remains.
The Deep Insight series frames this work as part of a broader production path. The first post defined five core challenges in multi-agent systems and showed how Strands Agents SDK, Amazon Bedrock and Bedrock AgentCore, and AWS Fargate address different layers of the problem. The third post will move from development to production, guiding deployment and operation of agents in a secure private network on Amazon Virtual Private Cloud, covering everything needed for real-world operations.

Comments (0)