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

Aperta Adatta ai principianti
#328 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
ai, backend

Direzione di ricerca

Inizia in cptr/utils/ai.py, in stream_anthropic e chat_completion, i due percorsi di richiesta menzionati nell’issue. Controlla come ciascuno costruisce il campo system di Anthropic e rivedi i test esistenti per questi percorsi; l’issue non indica un file di test. Il lavoro è completo quando entrambi i percorsi inviano il prompt di sistema come blocco di testo memorizzato nella cache, mantenendo il controllo per il sistema vuoto, e i dati di utilizzo di Anthropic segnalano la creazione della cache al primo turno e le letture della cache nei turni successivi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
768
Fork
108
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di open-webui/computer

Tutte le issue di open-webui/computer

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.