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

Đang mở Phù hợp với người mới
#328 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
ai, backend

Hướng nghiên cứu

Bắt đầu tại cptr/utils/ai.py, ở stream_anthropic và chat_completion, hai luồng yêu cầu được nêu trong issue. Kiểm tra cách mỗi luồng xây dựng trường system của Anthropic và xem lại các bài kiểm thử hiện có cho những luồng này; issue không nêu tên tệp kiểm thử. Hoàn thành khi cả hai luồng gửi lời nhắc hệ thống dưới dạng khối văn bản được lưu trong bộ nhớ đệm, đồng thời giữ nguyên điều kiện bảo vệ khi system rỗng, và báo cáo mức sử dụng của Anthropic cho biết bộ nhớ đệm được tạo ở lượt đầu tiên và được đọc ở các lượt sau.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
768
Fork
108
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

  • Có Dockerfile hoặc tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của open-webui/computer

Tất cả issue của open-webui/computer

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.