open-webui/backend
Claude c98e8decd4
fix: correct cache-token accounting and stream_options handling in Anthropic-compat usage
Adversarial review of the previous commit surfaced three defects:

- Cached tokens were double counted: OpenAI's prompt_tokens includes
  cached tokens while Anthropic's input_tokens excludes them (total
  input = input_tokens + cache_creation_input_tokens +
  cache_read_input_tokens). The converter passed the inclusive count
  through as input_tokens while also emitting the cache fields, so a
  {prompt_tokens: 1000, cached_tokens: 800} response claimed 1800 input
  tokens to Anthropic clients. Cache counts are now subtracted whenever
  the input count is derived from prompt_tokens; an Anthropic-native
  input_tokens key is taken as already exclusive.
- A model with stream_response=Off flips the stream flag after the
  payload conversion injected stream_options, and upstreams like OpenAI
  and vLLM reject stream:false requests carrying stream_options with
  HTTP 400. The OpenAI router now drops stream_options from any
  non-streaming payload before sending; this also covers the
  pre-existing variant where the web UI sends stream_options for models
  with the usage capability enabled.
- The streaming usage capture lacked the null/zero hardening the
  non-streaming path already had: explicit null token values leaked
  JSON null into message_delta usage (breaking schema-validating
  Anthropic SDK clients), and a reported 0 (e.g. Ollama omitting
  prompt_eval_count for KV-cached prompts) clobbered the pre-counted
  input_tokens. Both paths now share one hardened extraction helper
  that treats missing/null/0 counts as unreported.

Ref: https://github.com/open-webui/docs/issues/1328

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0119K4sbiCAFKq9rFaS6i5ij
2026-07-21 15:02:45 +00:00
..
data refac: mv backend files to /open_webui dir 2024-09-04 16:54:48 +02:00
open_webui fix: correct cache-token accounting and stream_options handling in Anthropic-compat usage 2026-07-21 15:02:45 +00:00
.dockerignore fix: litellm config issue 2024-02-24 22:35:11 -08:00
.gitignore refac 2024-09-06 04:59:20 +02:00
dev.sh refac 2026-03-24 19:43:30 -05:00
requirements-min.txt chore: bump Python backend dependencies, drop unused peewee (#25786) 2026-06-29 02:02:18 -05:00
requirements.txt chore: bump Python backend dependencies, drop unused peewee (#25786) 2026-06-29 02:02:18 -05:00
start.sh refac 2026-06-17 03:01:11 +02:00
start_windows.bat refac 2026-06-17 03:01:11 +02:00