fix(backend/copilot): document provider-dependent chunk.usage null behaviour

Not all OpenRouter-proxied providers honour stream_options={"include_usage":
True} — some always return chunk.usage=None. Add a comment at the guarded
check making this explicit and noting the tiktoken fallback activates in
that case (fail-open).
This commit is contained in:
Zamil Majdy
2026-03-15 22:39:03 +07:00
parent d6cec14543
commit 8e6d140580

View File

@@ -254,6 +254,11 @@ async def stream_chat_completion_baseline(
# where prompt_tokens already includes cached tokens
# (unlike Anthropic's native API). Use += to sum all
# tool-call rounds since each API call is independent.
# NOTE: stream_options={"include_usage": True} is not
# universally supported — some providers (Mistral, Llama
# via OpenRouter) always return chunk.usage=None. When
# that happens, tokens stay 0 and the tiktoken fallback
# below activates. Fail-open: one round is estimated.
if chunk.usage:
turn_prompt_tokens += chunk.usage.prompt_tokens or 0
turn_completion_tokens += chunk.usage.completion_tokens or 0