open-webui/backend/open_webui
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
internal refac 2026-06-29 12:40:20 -05:00
migrations refac 2026-07-16 01:42:24 -04:00
models chore: format 2026-07-20 22:11:42 -04:00
retrieval chore: format 2026-07-20 22:11:42 -04:00
routers fix: correct cache-token accounting and stream_options handling in Anthropic-compat usage 2026-07-21 15:02:45 +00:00
socket refac 2026-07-16 00:30:44 -04:00
static refac 2026-07-20 22:27:13 -04:00
storage refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00
tools refac 2026-07-16 00:58:34 -04:00
utils fix: correct cache-token accounting and stream_options handling in Anthropic-compat usage 2026-07-21 15:02:45 +00:00
__init__.py refac 2026-06-17 03:01:11 +02:00
alembic.ini fix: Alembic CLI commands from failing 2025-08-15 04:17:47 -04:00
config.py refac 2026-07-14 17:34:00 -04:00
constants.py refac 2026-06-29 05:46:51 -05:00
env.py refac 2026-07-09 17:28:05 -05:00
events.py refac 2026-07-16 01:27:52 -04:00
functions.py fix: don't mutate caller's payload when resolving a pipe base model (#26906) 2026-07-10 13:33:24 -05:00
main.py chore: format 2026-07-20 22:11:42 -04:00
tasks.py refac 2026-07-16 21:57:43 -04:00