Skip to content

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

Bridge Rust cost calculation setting

Magnus Müllerf67f88933f38
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 71354f0ac..04e87898b 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -1091,6 +1091,11 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- This preserves the visible run-start metadata and upgrade prompt formatting for direct helper calls and normal Rust-backed runs.
- Proof: `test_rust_agent_run_metadata_logs_match_browser_use`.
+216. Rust Agent terminal cost-calculation env parity
+ - Rust-backed runs now export `BU_USE_CALCULATE_COST=true` to the terminal subprocess when Browser Use callers pass `calculate_cost=True`.
+ - 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`.
+
## 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`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 13ed387a9..c61730447 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -4334,6 +4334,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):
def _run_env(self) -> dict[str, str]:
env = os.environ.copy()
env.update(_llm_env_overrides(self.llm))
+ if self.settings.calculate_cost:
+ env['BU_USE_CALCULATE_COST'] = '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 f2aed3240..ed72d8c1c 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3139,10 +3139,12 @@ async def test_rust_agent_updates_action_models_for_page(monkeypatch):
assert agent.DoneAgentOutput is not None
-def test_rust_agent_initializes_runtime_metadata_and_observability():
+def test_rust_agent_initializes_runtime_metadata_and_observability(monkeypatch):
from browser_use.rust import Agent
from browser_use.tokens.service import TokenCost
+ monkeypatch.delenv('BU_USE_CALCULATE_COST', raising=False)
+
class LLM:
model = 'gpt-test'
provider = 'test'
@@ -3170,6 +3172,10 @@ def test_rust_agent_initializes_runtime_metadata_and_observability():
assert agent.token_cost_service.include_cost is True
assert str(id(llm)) in agent.token_cost_service.registered_llms
assert agent.DoneAgentOutput is not None
+ assert agent._run_env()['BU_USE_CALCULATE_COST'] == 'true'
+
+ default_agent = Agent(task='Inspect default metadata.', llm=LLM())
+ assert 'BU_USE_CALCULATE_COST' not in default_agent._run_env()
async def test_rust_agent_logger_name_matches_browser_use(monkeypatch):