Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Request Rust OpenAI-compatible usage

Magnus Müller2a8c9c0d4781
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 04e87898b..433137c7d 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -1096,10 +1096,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This lets terminal current-main provider usage/cost recording honor the same public Agent setting while leaving default runs unchanged.
- Proof: `test_rust_agent_initializes_runtime_metadata_and_observability`.
+217. Rust Agent OpenAI-compatible usage request parity
+ - Cost-enabled Rust-backed OpenRouter and DeepSeek runs now export `LLM_BROWSER_OPENAI_COMPAT_INCLUDE_USAGE=true`.
+ - This asks terminal current-main OpenAI-compatible providers to include token usage in responses so Browser Use-style usage/cost reconstruction has data to report.
+ - Proof: `test_rust_agent_requests_openai_compatible_usage_for_cost_calculation`.
+
## Current Verification
- `python3 -m py_compile browser_use/agent/service.py browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`
-- `uv run pytest -q tests/ci/test_rust_agent.py` (184 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (185 tests)
- `cargo build -q -p browser-use-cli` on terminal branch `magnus/browser-use-rust-main-integration`
- `cargo test -q -p browser-use-cli run_codex_session_command_accepts_task_id_and_model -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index c61730447..89fea20a2 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -4336,6 +4336,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):
env.update(_llm_env_overrides(self.llm))
if self.settings.calculate_cost:
env['BU_USE_CALCULATE_COST'] = 'true'
+ if _llm_provider_name(self.llm) in {'openrouter', 'deepseek'}:
+ env['LLM_BROWSER_OPENAI_COMPAT_INCLUDE_USAGE'] = 'true'
browser_mode = self._browser_mode()
env['LLM_BROWSER_BROWSER_MODE'] = browser_mode
cdp_url = _extract_cdp_url(self.browser_session) or _extract_profile_cdp_url(self.browser_profile)
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index ed72d8c1c..2a419f809 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -1942,6 +1942,46 @@ def test_rust_agent_bridges_llm_credentials_to_terminal_env(monkeypatch):
assert agent._run_existing_argv(max_steps=3)[-4] == session_command
+def test_rust_agent_requests_openai_compatible_usage_for_cost_calculation(monkeypatch):
+ from browser_use.rust import Agent
+
+ class LLM:
+ def __init__(self, provider, model):
+ self.provider = provider
+ self.model = model
+ self.api_key = 'test-key'
+
+ monkeypatch.delenv('LLM_BROWSER_OPENAI_COMPAT_INCLUDE_USAGE', raising=False)
+ monkeypatch.delenv('BU_USE_CALCULATE_COST', raising=False)
+
+ openrouter_env = Agent(
+ task='Track OpenRouter usage.',
+ llm=LLM('openrouter', 'openrouter/model'),
+ calculate_cost=True,
+ )._run_env()
+ deepseek_env = Agent(
+ task='Track DeepSeek usage.',
+ llm=LLM('deepseek', 'deepseek-chat'),
+ calculate_cost=True,
+ )._run_env()
+ default_env = Agent(
+ task='Default OpenRouter usage.',
+ llm=LLM('openrouter', 'openrouter/model'),
+ )._run_env()
+ openai_env = Agent(
+ task='OpenAI usage.',
+ llm=LLM('openai', 'gpt-test'),
+ calculate_cost=True,
+ )._run_env()
+
+ assert openrouter_env['BU_USE_CALCULATE_COST'] == 'true'
+ assert openrouter_env['LLM_BROWSER_OPENAI_COMPAT_INCLUDE_USAGE'] == 'true'
+ assert deepseek_env['BU_USE_CALCULATE_COST'] == 'true'
+ assert deepseek_env['LLM_BROWSER_OPENAI_COMPAT_INCLUDE_USAGE'] == 'true'
+ assert 'LLM_BROWSER_OPENAI_COMPAT_INCLUDE_USAGE' not in default_env
+ assert 'LLM_BROWSER_OPENAI_COMPAT_INCLUDE_USAGE' not in openai_env
+
+
def test_rust_agent_translates_browser_profile_cdp_url(monkeypatch):
from browser_use.rust import Agent