Files
nexus/wiki/concepts/上下文刷新.md

2.2 KiB
Raw Blame History

title, type, tags, sources, last_updated
title type tags sources last_updated
上下文刷新Memory Flush concept
openclaw
memory
context-window
compaction
养龙虾5天血泪史
2026-04-23

Definition

上下文刷新是 OpenClaw 的压缩前内存保护机制——在压缩器运行前,自动触发静默回合,提示 Agent 将重要上下文写入磁盘,确保关键信息在压缩后仍然可用。

How It Works

对话累积 → 接近 Context Window → Memory Flush 触发 → Agent 写入 memory/YYYY-MM-DD.md → 压缩运行 → 摘要丢失但重要内容存活

配置示例

{
  "compaction": {
    "memoryFlush": {
      "enabled": true,
      "softThresholdTokens": 4000
    }
  }
}

参数说明

参数 说明
enabled 是否启用内存刷新
softThresholdTokens 触发刷新的 token 阈值

阈值设置原则

  • 小上下文模型8K-32K4000-8000 合适
  • 大上下文模型128K-200K需要更高阈值避免过早触发导致上下文碎片化
  • 公式Context Window × 0.2 ~ 0.4

The Gap: Memory Flush Only Runs Once Per Compression Cycle

"内存刷新每个压缩周期只触发一次。如果会话足够长,有两三次压缩,只有第一次得到刷新处理。之后的一切都处于风险中。"

补充方案Context Pruning

{
  "contextPruning": {
    "mode": "cache-ttl",
    "ttl": "6h",
    "keepLastAssistants": 3
  }
}
  • 在 6 小时后积极修剪旧上下文
  • 同时保留最后 3 个助手响应
  • 与 Memory Flush 结合:早期将重要内容写入磁盘,旧上下文在溢出前被清理

Key Insight

"如果在磁盘上,它能在压缩中存活。如果只在对话中,它就有风险。"

Connections