bug: Anthropic provider: `system` sent as plain string — prompt caching never engages

未关闭 适合新手
#328 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
ai, backend

调研方向

从 cptr/utils/ai.py 中的 stream_anthropic 和 chat_completion 开始,这两个请求路径在 issue 中有提及。检查它们各自如何构建 Anthropic 的 system 字段,并查看这些路径现有的测试;issue 没有指定测试文件。完成标准是:两个路径都将系统提示作为缓存文本块发送,同时保留空 system 的保护逻辑,并且 Anthropic 使用情况报告显示首轮创建缓存、后续轮次读取缓存。

由索引模型根据 Issue 内容生成。

描述

Summary

The Anthropic provider sends system as a plain string in the /v1/messages body. Anthropic only creates a cache entry when a system text block carries a cache_control breakpoint — a plain string is never eligible. Result: prompt caching is silently dead for every Anthropic connection; each turn of a chat re-reads the entire system prompt at full input-token price (no cache_creation_input_tokens / cache_read_input_tokens in the Anthropic usage analytics).

Where (cptr/utils/ai.py, HEAD):

  • stream_anthropic (line ~437) — the main chat path:
    body = {
        "model": form_data.model,
        "system": form_data.instructions,
        ...
    }
    
  • chat_completion (line ~144) — the non-streaming path (title/summary generation):
    if system:
        body["system"] = system
    

Proposed fix

Send the system as a cached block list in both call sites:

body["system"] = [
    {
        "type": "text",
        "text": system,
        "cache_control": {"type": "ephemeral"},
    }
]

(Keep the if system: / empty→None guard as-is.)

Notes

  • Anthropic's minimum cacheable prefix is 1024 tokens; cptr's system prompt (instructions + memory + skills + tools) comfortably exceeds it, so the breakpoint pays off from the second turn of every chat.
  • The client side is already ready: stream_anthropic parses cache_read_input_tokens and cache_creation_input_tokens from the response usage — only the request side never opts in.
  • #27 ("feat: ephemeral caching header for anthropic models") asked for a per-model configurable ephemeral cache header and was closed 2026-06-11 with "Request parameters support added". That makes caching possible via per-model request_params, but the provider's default still sends a plain string, so nothing is cached unless the user wires it up manually. Making the breakpoint the default (still overridable via request_params) would fix it for everyone.

Verification

After the fix: first turn of a chat shows cache_creation_input_tokens > 0, subsequent turns show cache_read_input_tokens > 0 — visible in the Anthropic console usage analytics and in cptr's per-request usage logging.

主要语言
Python
星标
768
派生
108
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

open-webui/computer 的其他 Issue

查看 open-webui/computer 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。