[Bug]:缓存命中率极低
Do you need to file an issue?
- [x] I have searched the existing issues and this bug is not already filed.
- [x] I believe this is a legitimate bug, not just a question or feature request.
Describe the bug
每次轮对话的缓存都没有命中,是不是上下文中间有变化
Steps to reproduce
No response
Expected Behavior
No response
Related Module
Other
Configuration Used
No response
Logs and screenshots
Additional Information
- DeepTutor Version:1.7.1
- Operating System:docker
- Python Version:
- Node.js Version:
- Browser (if applicable):
- Related Issues:
Cache behaviour depends on the provider, so the screenshot alone is not enough to tell whether this is a bug:
- Anthropic uses explicit
cache_controlbreakpoints (the agent loop adds them for supported models; see #2078). - OpenAI-compatible providers (DeepSeek, Qwen, etc.) usually use automatic prefix caching and ignore
cache_control. A cache hit then requires an identical prefix above the provider's minimum length.
Could you share:
- provider and model name;
- one request's usage fields (
prompt_tokens,prompt_cache_hit_tokens/cached_tokens,cache_creation_input_tokens,cache_read_input_tokens); - whether the model, system prompt, tool list or selected knowledge bases change between turns;
- the prompt length — short prompts often fall below the provider's cache minimum.
With those, we can tell whether the prefix is actually changing or the provider simply does not cache this request shape.
- 我使用的API是 OpenAI Responses ,模型为gpt-6.1-terra
- 不清楚需要提供什么,下边提供了完整的请求体
- 对话过程中没有对配置进行修改
- 图片上显示,每次对话都只有3.07k进行了缓存,下一次提问,并没有缓存之前的回答,有时甚至一点缓存都没有
谢谢,这两份请求体足够定位了。结论先说:问题不在 workspace 快照"中间变化"导致整段失效,而在 GPT-6.1 的隐式缓存只在最后一条可缓存消息处打断点;下一轮尾部总是新的,上一轮的断点就永远匹配不上,最后只剩开头的 developer 指令能命中(约 3.0k tokens),正好对应截图里的 25%。
我按 Responses input 逐项对比了这两份请求(tiktoken o200k_base 估算):
| 项 | req1 | req2 |
|---|---|---|
| 总 tokens | 10,428 | 11,378 |
| 前 7 项 | 逐字节相同 | 逐字节相同 |
| 稳定前缀 | additional_tools 4,908 + developer 1,742 + runtime context 95 + tools manifest 2,511 + extended_tools 217 + 首条 user 32 + assistant 605 = 10,110 tokens |
同左 |
| 第一处分叉 | [Runtime context: workspace] 里的本轮目录 turn_1793512078957_... |
turn_1793512180785_... |
也就是说,请求里有 10k tokens 的完全稳定前缀,但 body 里没有任何 prompt_cache_options / prompt_cache_breakpoint。GPT-5.6+(包括 gpt-6.1-terra)默认是 implicit 模式,只在"最新一条 eligible message 的末尾"写缓存断点。下一轮末尾多了 user 消息、workspace 快照又换了 turn_<id>,断点找不到,只能回退到开头连续的 developer block —— 也就是那 3.0k tokens。官方文档把这个坑单独写了一节:"A shared prefix is not always a cached prefix"。
另外要确认一下这两份 body 的来源:它们带 client_metadata(x-codex-installation-id、turn_id)和 additional_tools developer item,而 DeepTutor 1.7.1 的 Codex provider 发的是 instructions + tools + prompt_cache_key,没有这些字段。所以它们更像是 Codex 客户端/中转转发后的请求,而不是 DeepTutor 直接发出的。方便的话请确认 provider 的 base URL,以及是不是第三方中转 —— 如果中转会重写 body,显式缓存字段可能被丢掉。
建议的修复方向(我可以直接发 PR):
- 在 Responses 路径上为 GPT-5.6+ 增加显式缓存:顶层
prompt_cache_options: {mode: "explicit", ttl: "30m"}; - 在稳定前缀的最后一条消息(上一轮 assistant 回复或 developer 指令)上放
prompt_cache_breakpoint; - workspace 快照继续放在尾部,不参与断点。
这样每轮可以复用约 10k 的历史前缀,而不是 3k。
在 PR 合入前,可以先做两个对照实验:
- 两轮之间是否超过 30 分钟(默认 TTL 是 30 分钟);
- 用同一个模型直连
api.openai.com再测一次,排除中转路由/改写的影响。
I opened #2358 with the explicit-cache fix.
What it does for gpt-6.1-terra:
- sends
prompt_cache_options: {"mode": "explicit", "ttl": "30m"}; - marks the last four durable user messages with
prompt_cache_breakpoint, stopping before the trailing runtime snapshot + learner message.
The four-message window is deliberate: explicit mode only walks the breakpoints present in the incoming request. If each turn marked only the newest message, the next request would not contain the boundary the previous request cached. Keeping the last four means the previous turn's boundary is still there, so the lookup can match the entry it just wrote.
Why not mark the assistant reply? The Responses input schema accepts prompt_cache_breakpoint on input_text/input_image/input_file, not on output_text. The assistant reply and tool output after the boundary are re-read, but the ~10k-token history becomes reusable.
Gates: GPT-5.6+ model names, native OpenAI binding (or a custom OpenAI-spec binding; api.openai.com custom endpoints too), SDK capability check, and a per-model fallback that retries without the fields if a provider rejects them. Relays that strip or refuse the fields will keep working but will not gain the cache reuse.
So after this lands, the same two requests should move from ~3.0k cached to roughly the full shared prefix (~10k), assuming the relay forwards the fields. If it does not, the provider base URL is the thing to check next.
Fixed on local dev (b266d1a14de659124233dde4bc71c0700b3ad2b7). 226 history and agent-loop tests passed; unchanged sent prefixes remain byte-stable while newer runtime snapshots append.
will be fixed in the next releases!