fix: enable token usage tracking and configurable stream timeout for Ollama provider#8493
Open
kasjens wants to merge 7 commits intoaaif-goose:mainfrom
Open
fix: enable token usage tracking and configurable stream timeout for Ollama provider#8493kasjens wants to merge 7 commits intoaaif-goose:mainfrom
kasjens wants to merge 7 commits intoaaif-goose:mainfrom
Conversation
…e tracking in streaming responses
…rue) to enable token usage tracking while allowing older Ollama builds to opt out
…ng stream_options instead of silently defaulting to enabled
…back usage parsing for Ollama-native token fields (prompt_eval_count/eval_count)
…ive token counters (prompt_eval_count/eval_count)
… stall errors on every chunk
jamadeo
reviewed
Apr 13, 2026
| input_limit.or(model_config.context_limit) | ||
| } | ||
|
|
||
| fn resolve_ollama_stream_usage() -> bool { |
Member
There was a problem hiding this comment.
Can we do without a config flag? Maybe https://docs.ollama.com/api-reference/get-version and flag this on a minimum version?
Author
There was a problem hiding this comment.
Thanks @jamadeo! I considered that but went with the config flag because:
- Ollama isn't always direct — users behind proxies or compatible API servers (LiteLLM, LocalAI) may not expose
/api/version. - Version ≠ capability — custom builds/forks may support
stream_optionswithout a recognizable version string. - It defaults to enabled — so modern installs work out of the box. Only users on older builds need to set
OLLAMA_STREAM_USAGE=false.
Happy to add version detection as a best-effort first pass with the config flag as fallback if you'd prefer that approach!
Member
There was a problem hiding this comment.
Makes sense, and seems like most of the time you'd never add it at all
michaelneale
approved these changes
Apr 14, 2026
Collaborator
michaelneale
left a comment
There was a problem hiding this comment.
I think this is worth having in, thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three related fixes for the Ollama provider:
Token usage tracking: The provider was unconditionally stripping
stream_options: {"include_usage": true}from requests (fix: prevent Ollama provider from hanging on tool-calling requests #7723), preventing Ollama from returning token counts in streaming responses. This is now gated behindOLLAMA_STREAM_USAGE(default:true) so modern Ollama builds get usage tracking while older builds can opt out withOLLAMA_STREAM_USAGE=false. Invalid values are handled safely — a warning is logged andstream_optionsis disabled.Fallback usage parsing: Added fallback for Ollama-native token fields (
prompt_eval_count,eval_count) inget_usage(), so token tracking works even when Ollama doesn't translate to standard OpenAI field names (prompt_tokens,completion_tokens). OpenAI fields take precedence when both are present. Null OpenAI fields (e.g."completion_tokens": null) correctly fall through to the Ollama-native fields instead of silently dropping usage.Configurable stream timeout: The hardcoded 30s per-chunk timeout was too aggressive for slower models (CPU inference, large parameter counts, complex reasoning). The timeout is now configurable via a resolution chain:
OLLAMA_STREAM_TIMEOUT>GOOSE_STREAM_TIMEOUT>OLLAMA_TIMEOUT> default (120s). Zero values are treated as invalid and skipped to prevent immediate stall errors on every chunk.Testing
OLLAMA_STREAM_USAGEdefault-on and opt-out behaviorRelated Issues
Relates to #8479
Relates to #8476
Relates to #7723