Context Management
Context Management is the key to sustaining long conversations. As conversations grow longer and tool outputs accumulate, the system intelligently manages the context window through a three-layer compression mechanism — retaining critical information, freeing up precious space, and keeping the conversation unbroken.
Three-Layer Compression Architecture
Section titled “Three-Layer Compression Architecture”| Layer | Name | Trigger | Compression Granularity | Cost |
|---|---|---|---|---|
| 1 | Microcompact | Time interval | Single tool result | Zero |
| 2 | Auto Compact | Token count reaches threshold | A batch of messages → summary | One LLM call |
| 3 | Session Memory Compact | When Auto Compact is triggered | Session memory → summary | Zero |
Microcompact — Tool Result Cleanup
Section titled “Microcompact — Tool Result Cleanup”Microcompact is the lightest form of compression — it doesn’t generate summaries, it simply cleans up old tool results that take up a lot of space.
Cleanable Tools
Section titled “Cleanable Tools”Only tools that produce large outputs are cleaned:
| Tool | Typical Output |
|---|---|
📖 read | File contents |
✏️ write | Write results |
🔍 grep | Search results |
🔎 find | File listings |
⚡ script | Script execution results |
Cleanup Strategy
Section titled “Cleanup Strategy”| Strategy | Trigger | Mechanism |
|---|---|---|
| 🕐 Time-based | Last assistant message > 60 minutes ago | Directly replace old tool results with placeholders, keep most recent 5 |
💡 The time-based strategy retains at least 5 recent tool results to prevent the model from completely losing its working context.
Auto Compact — Automatic Summary Compression
Section titled “Auto Compact — Automatic Summary Compression”When microcompact isn’t enough, Auto Compact kicks in — compressing a batch of old messages into a concise summary.
Trigger Conditions
Section titled “Trigger Conditions”| Configuration | Default | Description |
|---|---|---|
contextTokensLimit | 25,000 | Context token limit |
autoCompactBufferTokens | 13,000 | Safety buffer |
| Trigger threshold | 12,000 | contextTokensLimit - autoCompactBufferTokens |
Compression Prompt
Section titled “Compression Prompt”Auto Compact uses a 9-part structured prompt to ensure the summary covers all critical dimensions:
| # | Section | Content |
|---|---|---|
| 1 | Primary Request and Intent | The user’s core request and intent |
| 2 | Key Technical Concepts | Key technical concepts involved |
| 3 | Files and Code Sections | Relevant files and code snippets (including full code) |
| 4 | Errors and Fixes | Errors encountered and their fixes |
| 5 | Problem Solving | Problem-solving process |
| 6 | All User Messages | All user messages (excluding tool results) |
| 7 | Pending Tasks | Tasks awaiting completion |
| 8 | Current Work | What is currently being worked on |
| 9 | Optional Next Step | Optional next action |
Message Retention Policy
Section titled “Message Retention Policy”Compression doesn’t discard all old messages — it retains recent messages:
| Configuration | Value | Description |
|---|---|---|
minTokens | 10,000 | Minimum tokens to retain |
minTextBlockMessages | 5 | Minimum messages with text blocks to retain |
maxTokens | 40,000 | Maximum tokens to retain |
📌 API invariant protection:
tool_resultentries in retained messages must have correspondingtool_useentries. During compression, the retention range is extended backwards to ensure pairs remain complete.
Session Memory Compact
Section titled “Session Memory Compact”When Memory System has Session Memory entries, compression can be completed at zero cost:
This is exactly the convergence point of the memory system and context management — Session Memory, continuously accumulated during the conversation, becomes a high-quality summary at compression time without requiring an additional LLM call.
Post-Compression Context Recovery
Section titled “Post-Compression Context Recovery”After compression, the system automatically re-injects recently read files to help the model quickly return to its working state:
| Configuration | Value | Description |
|---|---|---|
| Max files to recover | 5 | The 5 most recently read files |
| Total token budget | 10,000 | Total limit for all recovered files |
| Per-file token limit | 2,000 | Maximum tokens per file |
System Prompt Assembly
Section titled “System Prompt Assembly”The system prompt is provided via the SystemPrompt configuration item and passed as the Agent’s Instruction. When an agent definition exists, the agent’s system prompt is used; otherwise, the default system prompt is used.
Message Assembly Pipeline
Section titled “Message Assembly Pipeline”Before each API call, messages go through a complete assembly pipeline:
Attachment System
Section titled “Attachment System”The attachment system dynamically injects contextual information during conversations:
| Attachment Type | Injection Timing | Description |
|---|---|---|
| 📋 AgentPrompt | Every turn | AGENT.md snapshot, containing agent capability description |
| 📝 TodoList | Every turn | Pending task list |
| 🧠 SessionMemory | Every turn | Latest 5 session memories (up to 5,000 tokens) |
| 🗂️ UserMemory | Every turn | User memories filtered by importance |
Next Steps
Section titled “Next Steps”- Memory System — Dive deeper into how Session Memory and User Memory work
- Remote Tool Calling — Learn the full lifecycle of tool calls
- Session Management — Understand where context management fits within sessions