为什么需要上下文管理?
长时间对话会导致上下文窗口接近模型上限,造成响应变慢、成本增加甚至请求失败。OpenClaw 提供两个机制自动管理上下文:
- Compaction(压缩):将过长的对话历史压缩为摘要
- Context Pruning(修剪):清理过时的工具调用结果
Compaction 配置
{
agents: {
defaults: {
compaction: {
mode: "safeguard",
timeoutSeconds: 900,
reserveTokensFloor: 24000,
identifierPolicy: "strict",
identifierInstructions: "Preserve deployment IDs...",
postCompactionSections: ["Session Startup", "Red Lines"],
model: "openrouter/anthropic/claude-sonnet-4-6",
memoryFlush: {
enabled: true,
softThresholdTokens: 6000,
systemPrompt: "Session nearing compaction...",
prompt: "Write lasting notes to memory/YYYY-MM-DD.md"
}
}
}
}
}
参数说明
| 参数 | 说明 |
|---|---|
mode |
"safeguard" 使用分块摘要压缩历史 |
timeoutSeconds |
压缩操作超时时间 |
reserveTokensFloor |
压缩后至少保留的 token 数 |
identifierPolicy |
"strict" 保留标识符,"off" 不保留,"custom" 自定义 |
postCompactionSections |
压缩后重新注入的 AGENTS.md 标题段落 |
model |
用于执行压缩的模型 |
Memory Flush
memoryFlush 是压缩前的记忆保存机制——在自动压缩触发前,AI 会先执行一轮静默操作,将重要信息写入持久化记忆文件:
softThresholdTokens:触发阈值prompt:告诉 AI 将哪些信息写入记忆
Context Pruning 配置
{
agents: {
defaults: {
contextPruning: {
mode: "cache-ttl",
ttl: "1h",
keepLastAssistants: 3,
softTrimRatio: 0.3,
hardClearRatio: 0.5,
minPrunableToolChars: 50000,
softTrim: {
maxChars: 4000,
headChars: 1500,
tailChars: 1500
},
hardClear: {
enabled: true,
placeholder: "[Old content cleared]"
}
}
}
}
}
修剪机制
- Soft Trim:超过
softTrimRatio阈值时,将工具结果截断为headChars + tailChars - Hard Clear:超过
hardClearRatio阈值时,替换为 placeholder 文本 - 图片内容永远不会被修剪或清除
- 修剪只影响发送给模型的上下文,不修改磁盘上的历史记录
关键参数
ttl:工具结果的存活时间,超过后可被修剪keepLastAssistants:保留最近 N 轮助手回复不被修剪minPrunableToolChars:工具结果至少超过此字符数才会被修剪