八、Context 压缩与缓存
八、Context 压缩与缓存
核心思想是:稳定 system prompt + context 压缩
稳定 system prompt
- session 初始化时,会按照
SOUL.md、工具schema、memory、skills、model等信息依次拼接组成 system prompt - 且在多轮对话中保持 system prompt 不变
临时 plugin context
临时添加的 plugin context 并不会加入到 system prompt,而是加入到 user prompt
context 压缩
- 替换老 tool 结果,成本低,无需调用LLM
- 基于 token 预算确定边界,保留开头和最近消息,中间消息用 LLM 总结摘要
亮点
- 稳定 system prompt 提高 K/V cache 命中率:在单个 session 中,保持 system prompt 前缀稳定,临时 plugin context 不加入 system prompt
- 多级压缩:1、删除删除老 tool 结果;2、LLM 总结摘要
参考
This post is licensed under CC BY 4.0 by the author.