/compact 工作原理
触发时总结上下文
压缩之后的 messages
上下文总结模板
1.
Primary Request and Intent
用户的请求和意图
2.
Key Technical Concepts
关键技术概念
3.
Files and Code Sections
文件和代码片段
4.
Errors and fixes
错误和修复
5.
Problem Solving
已解决的问题
6.
All user messages
全部用户消息 · 一字不改
7.
Pending Tasks
待办任务
8.
Current Work
当前工作
9.
Optional Next Step
下一步 · 原话引用
提示词设计机制
01
禁止调用工具
Read
Edit
Bash
Write
Grep
02
先想后写 · 思维链
<analysis>
<summary>
03
一字不改 · 防意图偏移
Include EVERY user message
VERBATIM
, in chronological order — do not paraphrase or summarize.
End with a
DIRECT QUOTE
of the user's most recent message — preserve the original intent.
压缩后的 messages布局
1
全局 CLAUDE.md
UserContext
2
对话总结 summary
Summary
3
上一轮 assistant 回复
MessagesToKeep
4
最近碰过的 5 个文件 Read 结果
Attachments
5
MCP instructions
HookResult
6
SessionStart hook
HookResult
7
Compacted 提示条
UI chrome
8
你压缩之后敲的第一句话
UserMessage
三个重要组件
Summary
压缩远端历史
· 几十轮对话改写成 9 节模板正文 · 只剩全局概述,缺少细节
MessagesToKeep
保留近期现场
· 最后几条原始消息 · 如果是工具调用,tool_use 和 tool_result 成对保留
Attachments
恢复工作材料
· 最近碰过的文件用 Read 工具读取内容
最多 5 个文件
单文件 ≤ 5k token