Lesson 06

Context Compact 上下文压缩

Agent 可以策略性遗忘,从而永远工作下去

// Three-Layer Pipeline 三层压缩管道

Layer 1: micro_compact 微压缩 (silent, every turn) Before 压缩前: tool_result: { ...5000 chars... } After 压缩后: [placeholder] // Replace old tool_result // with short placeholder every turn tokens > 50000? YES NO Continue 继续 Layer 2: auto_compact 自动压缩 (threshold triggered) save transcript LLM summarize replace all Before: ~80000 tokens After: ~12000 tokens Summary preserves: key decisions + current task or model decides Layer 3: compact tool 手动压缩 (model-triggered) Model calls: compact(summary="We finished auth module, now working on dashboard. Key files: ...") Key Insight 关键洞察 "Strategic forgetting" "策略性遗忘" The agent keeps working forever by intelligently compressing its memory. infinite context = finite window
LAYER 01

微压缩 micro_compact

每轮静默执行。把旧的 tool_result 替换为短占位符,只保留最新一轮的完整结果。用户无感知,但持续节省 token。

silent, every turn, no LLM call

LAYER 02

自动压缩 auto_compact

当 token 数超过阈值(如 50000)时触发。保存完整 transcript 到文件,用 LLM 生成摘要替换所有消息,保留关键决策和当前任务。

threshold: 50000 tokens

LAYER 03

手动压缩 compact tool

模型主动调用 compact 工具,附带自己的总结。适合完成大阶段性工作后主动清理上下文。模型知道什么重要、什么可以忘记。

model-triggered, with custom summary

试一试 Try It Out

1

"逐个读取 src/ 目录下的所有 Java 文件(触发对旧结果的微压缩)"

"Read every Java file in the src/ directory one by one (triggers micro-compact on older results)"

2

"持续读取文件,直到触发自动压缩"

"Keep reading files until compression triggers automatically"

3

"使用 compact 工具手动压缩对话"

"Use the compact tool to manually compress the conversation"