Skip to content

Fix: Token usage display for Open WebUI and other OpenAI-compatible proxies#9516

Closed
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/openwebui-token-usage
Closed

Fix: Token usage display for Open WebUI and other OpenAI-compatible proxies#9516
roomote-v0[bot] wants to merge 1 commit intomainfrom
fix/openwebui-token-usage

Conversation

@roomote-v0
Copy link
Copy Markdown
Contributor

@roomote-v0 roomote-v0 bot commented Nov 23, 2025

Summary

This PR fixes issue #9514 where the context length indicator always displays 0 tokens when using Open WebUI as an OpenAI-compatible proxy.

Problem

When connecting the Roo Code extension to an OpenAI-compatible API through Open WebUI, the token usage always shows 0 despite the API responses correctly including completion_tokens, prompt_tokens, and total_tokens fields.

Solution

Enhanced the BaseOpenAiCompatibleProvider class to:

  • Check for alternative usage data locations (usage_metadata field in addition to usage)
  • Support multiple token field naming conventions used by various proxies:
    • prompt_tokens / input_tokens / promptTokens
    • completion_tokens / output_tokens / completionTokens
    • Various cache token field variations
  • Add debug logging when usage data is present but tokens aren't extracted correctly

Testing

  • All existing tests pass (657 tests passed)
  • The fix handles different field naming conventions gracefully
  • Falls back to 0 for missing fields (preserving backward compatibility)

Related Issue

Fixes #9514


Important

Fixes token usage display in BaseOpenAiCompatibleProvider for Open WebUI by supporting alternative field names and adding debug logging.

  • Behavior:
    • Fixes token usage display issue in BaseOpenAiCompatibleProvider for Open WebUI and other proxies.
    • Checks usage_metadata field as alternative to usage for token data.
    • Supports multiple token field naming conventions: prompt_tokens, input_tokens, promptTokens, completion_tokens, output_tokens, completionTokens.
    • Adds debug logging for cases where usage data is present but tokens aren't extracted.
  • Testing:
    • All existing tests pass (657 tests).
    • Handles different field naming conventions gracefully.
    • Falls back to 0 for missing fields, maintaining backward compatibility.

This description was created by Ellipsis for 42b730a. You can customize this summary. It will automatically update as commits are pushed.

- Add support for alternative field names used by various OpenAI-compatible proxies
- Check for usage_metadata field in addition to usage field
- Try multiple token field variations (prompt_tokens, input_tokens, promptTokens, etc.)
- Add debug logging when tokens are present but not extracted correctly
- Fixes issue where Open WebUI proxy responses show 0 tokens despite having valid usage data

Fixes #9514
@roomote-v0 roomote-v0 bot requested review from cte, jr and mrubens as code owners November 23, 2025 15:29
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Nov 23, 2025
@roomote-v0
Copy link
Copy Markdown
Contributor Author

roomote-v0 bot commented Nov 23, 2025

Rooviewer Clock   See task on Roo Cloud

Review complete. Found 2 issues that should be addressed:

  • The usage_metadata fallback lacks evidence and may not work correctly if the field has a different structure
  • Missing test coverage for the new field name variations and alternative token extraction paths

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Nov 23, 2025
Comment on lines +195 to 198
} else if ((chunk as any).usage_metadata) {
// Some proxies use usage_metadata instead of usage
lastUsage = (chunk as any).usage_metadata
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage_metadata fallback assumes this field has the same structure as the standard usage field, but there's no evidence from the issue description that Open WebUI uses this field name. The issue shows Open WebUI returns standard usage.prompt_tokens and usage.completion_tokens fields. If a proxy does use usage_metadata with a different structure, processUsageMetrics will still fail to extract tokens since it only checks for standard field names. This fallback should either be removed or documented with evidence of which proxies use it and what structure they return.

Fix it with Roo Code or mention @roomote and request a fix.

@@ -218,15 +223,34 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
}

protected processUsageMetrics(usage: any, modelInfo?: any): ApiStreamUsageChunk {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR adds support for alternative field names but doesn't include tests to verify this functionality works. Tests should cover: (1) extracting tokens from input_tokens/output_tokens field names, (2) extracting tokens from camelCase field names like promptTokens/completionTokens, (3) extracting cache tokens from alternative field locations, and (4) the debug logging when tokens aren't extracted despite usage data being present. Without tests, it's difficult to verify the fix actually resolves the Open WebUI issue and won't regress in the future.

Fix it with Roo Code or mention @roomote and request a fix.

@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Jan 6, 2026
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] Context length always shows zero tokens when using Open WebUI as proxy for OpenAI-compatible APIs

2 participants