Skip to content

Add OpenRouter pricing fallback for token costs#4886

Merged
sauravpanda merged 1 commit into
mainfrom
fix/openrouter-token-pricing
May 22, 2026
Merged

Add OpenRouter pricing fallback for token costs#4886
sauravpanda merged 1 commit into
mainfrom
fix/openrouter-token-pricing

Conversation

@sauravpanda
Copy link
Copy Markdown
Collaborator

@sauravpanda sauravpanda commented May 22, 2026

Summary

  • fetch and cache OpenRouter /models metadata as a fallback pricing source
  • convert OpenRouter per-token pricing into Browser Use ModelPricing
  • force OpenRouter pricing for registered ChatOpenRouter instances and OpenAI-compatible clients using the OpenRouter base URL

Tests

  • uv run pytest tests/ci/test_openrouter_token_cost.py -q
  • uv run ruff check browser_use/tokens/service.py browser_use/tokens/openrouter_pricing.py tests/ci/test_openrouter_token_cost.py
  • uv run ruff format --check browser_use/tokens/service.py browser_use/tokens/openrouter_pricing.py tests/ci/test_openrouter_token_cost.py
  • pre-commit hooks via git commit, including pyright

Summary by cubic

Adds a fallback for token pricing by fetching OpenRouter /api/v1/models and converting prices to our ModelPricing. Also forces OpenRouter pricing for ChatOpenRouter and OpenAI-compatible clients using the OpenRouter base URL to avoid mismatched rates.

  • New Features
    • Fetches and caches OpenRouter model metadata (1h TTL) and converts prompt/completion/cache prices to ModelPricing.
    • Supports openrouter/... and openrouter-... model IDs; prefixes openrouter/ to disambiguate same-named upstream models when needed.
    • Falls back to OpenRouter pricing when LiteLLM data is missing and uses it in cost calculations for registered LLMs.

Written for commit db39960. Summary will update on new commits. Review in cubic

@github-actions
Copy link
Copy Markdown

Agent Task Evaluation Results: 2/2 (100%)

View detailed results
Task Result Reason
browser_use_pip ✅ Pass Skipped - API key not available (fork PR or missing secret)
amazon_laptop ✅ Pass Skipped - API key not available (fork PR or missing secret)

Check the evaluate-tasks job for detailed task execution logs.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: db39960431

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return llm

self.registered_llms[instance_id] = llm
self._pricing_model_names[llm.model] = self._get_pricing_model_name(llm)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Key pricing overrides by provider+model, not model alone

register_llm stores _pricing_model_names under llm.model only, and calculate_cost later looks up overrides by the same plain model string. If both an OpenRouter client and a non-OpenRouter client use the same model id (for example openai/gpt-4o-mini), the second registration overwrites the first and all cost calculations for that model id use the wrong pricing source. This silently misprices usage in mixed-provider setups and makes results depend on registration order.

Useful? React with 👍 / 👎.

@sauravpanda sauravpanda enabled auto-merge May 22, 2026 20:58
@sauravpanda sauravpanda merged commit fb9357f into main May 22, 2026
99 checks passed
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="browser_use/tokens/service.py">

<violation number="1" location="browser_use/tokens/service.py:351">
P1: Pricing-source mapping is keyed only by model name, so same-named models from different providers overwrite each other and can produce incorrect cost calculations.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

return llm

self.registered_llms[instance_id] = llm
self._pricing_model_names[llm.model] = self._get_pricing_model_name(llm)
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot May 22, 2026

Choose a reason for hiding this comment

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

P1: Pricing-source mapping is keyed only by model name, so same-named models from different providers overwrite each other and can produce incorrect cost calculations.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At browser_use/tokens/service.py, line 351:

<comment>Pricing-source mapping is keyed only by model name, so same-named models from different providers overwrite each other and can produce incorrect cost calculations.</comment>

<file context>
@@ -340,6 +348,7 @@ def register_llm(self, llm: BaseChatModel) -> BaseChatModel:
 			return llm
 
 		self.registered_llms[instance_id] = llm
+		self._pricing_model_names[llm.model] = self._get_pricing_model_name(llm)
 
 		# Store the original method
</file context>
Fix with Cubic

@sauravpanda sauravpanda deleted the fix/openrouter-token-pricing branch May 22, 2026 20:58
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