Claude Cookbook:上下文工程三招——記憶、壓縮(compaction)、清除工具結果
Context engineering: memory, compaction, and tool clearing
摘要分析
Anthropic 2026-03-20 的 cookbook 比較三種官方上下文管理機制:Compaction(compact_20260112)在輸入達 150K 時把對話摘要、之後只帶摘要;Tool-result clearing(clear_tool_uses_20250919)把舊工具結果換成占位符,零推論成本;Memory tool(memory_20250818)讓模型在 /memories 讀寫檔案跨 session 保留。實測清除工具結果讓峰值上下文從 335K 降到 173K。建議依瓶頸選:工具輸出肥用 clearing、長對話用 compaction、跨 session 用 memory。
- Compaction:betas=["compact-2026-01-12"],context_management edits type=compact_20260112,trigger input_tokens 150_000
- Clearing:type=clear_tool_uses_20250919,設 trigger/keep/clear_at_least,exclude_tools 保護 memory
- Memory:tools 加 {"type":"memory_20250818","name":"memory"},自己實作 view/create/str_replace/insert/delete/rename
- 回傳的 compaction block 要序列化回歷史,API 會丟掉它之前的內容
- 選擇原則:工具輸出肥→clearing;對話長→compaction;要跨 session→memory
Published 2026-03-20, this Claude Cookbook compares three first-party context-management primitives. Compaction (compact_20260112 with beta header compact-2026-01-12) summarizes the whole transcript when input reaches a trigger (default 150K, minimum 50K) and continues from the summary; it is lossy, so custom instructions should name details that must survive. Tool-result clearing (clear_tool_uses_20250919) replaces old tool_result content with placeholders while keeping tool_use records, at zero inference cost; parameters include trigger, keep and clear_at_least. The memory tool (memory_20250818) lets the model read and write files under /memories that persist across sessions, with your app implementing the handler and guarding path traversal. Measured results: clearing cut peak context from about 335K to 173K tokens, and a memory-enabled second session needed 4 file reads instead of 8. Choose by bottleneck: clearing for bulky tool output, compaction for long dialogue, memory for cross-session knowledge.
- Compaction: beta compact-2026-01-12, edit type compact_20260112, trigger 150K
- Clearing: clear_tool_uses_20250919 with trigger/keep/clear_at_least
- Memory: tool type memory_20250818 with six commands you implement
- Serialize the compaction block back into history
- Pick by bottleneck: clearing, compaction, or memory
關鍵字:context engineeringcompactionmemory tooltool clearingClaude API
來源原文:https://platform.claude.com/cookbook/tool-use-context-engineering-context-engineering-tools
本頁為報導內容的摘要與分析,版權屬原媒體所有;請以原文為準。
