Skip to content

fix(tokens): add stream options for usage inclusion#90

Merged
andev0x merged 1 commit into
mainfrom
feature/tokens
Jul 23, 2026
Merged

fix(tokens): add stream options for usage inclusion#90
andev0x merged 1 commit into
mainfrom
feature/tokens

Conversation

@andev0x

@andev0x andev0x commented Jul 23, 2026

Copy link
Copy Markdown
Member
  • update openai.go to include stream options
  • modify openrouter.go to include stream options
  • adjust ui/stream.go to handle usage from stream options

- update openai.go to include stream options
- modify openrouter.go to include stream options
- adjust ui/stream.go to handle usage from stream options
@andev0x

andev0x commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

Fixes

  1. internal/providers/openai.go — Two bugs fixed:
  • Added streamOptions struct + StreamOptions field on openaiRequest; ExecuteStream now sends stream_options: {"include_usage": true} so OpenRouter emits the final usage chunk
  • Moved chunk.Usage capture before the len(chunk.Choices) == 0 guard — the final usage chunk has "choices": [] so it was being silently dropped
  1. internal/providers/openrouter.go — Same two bugs:
  • Added StreamOptions to openrouterRequest and set it in ExecuteStream
  • Moved usage capture before the choices-empty check
  1. internal/ui/stream.go — Replaced concrete type assertion rawStream.(*providers.StreamResult) with a usageProvider interface check. Both OpenAIStreamResult and OpenRouterStreamResult implement Usage() (input, output int) but were invisible to the old ollama-only type assertion — so the fallback len(content)/4 (user prompt only, missing system+context) was always used, showing 4-7 tokens instead of ~1000.
  2. internal/llm/openai.go — Added streamOptions struct + StreamOptions field to openAIReq; StreamResponse now sends stream_options: {"include_usage": true}.

@andev0x
andev0x merged commit 9a9b194 into main Jul 23, 2026
3 checks passed
@andev0x
andev0x deleted the feature/tokens branch July 23, 2026 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant