Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Reconstruct Rust terminal reasoning token usage
Magnus Müller7eb26f03395b
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex f0926a465..f36193908 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -709,10 +709,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- This keeps pre-compaction model outputs, tool actions, tool results, usage, attachments, and final results off Python-visible history surfaces while preserving post-compaction run data.- Proof: `test_rust_history_applies_terminal_session_compaction_boundary`.+140. Rust terminal reasoning-token usage reconstruction+ - Terminal `token_count` usage snapshots now preserve `reasoning_output_tokens` on Browser Use `UsageSummary` surfaces.+ - The wrapper treats reasoning tokens as completion usage, matching Browser Use's LLM usage accounting, and preserves terminal-reported cumulative `total_tokens` instead of recomputing totals from visible output tokens only.+ - Proof: `test_rust_history_reconstructs_terminal_reasoning_token_usage`.+## 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` (111 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (112 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.pyindex d4cc640e5..257e63107 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -1533,10 +1533,32 @@ def _model_usage_payload(payload: dict[str, Any]) -> dict[str, Any]:return usage if isinstance(usage, dict) else payload+def _reasoning_output_tokens(usage: dict[str, Any]) -> int:+ return _int_value(+ usage.get('reasoning_output_tokens')+ or usage.get('output_reasoning_tokens')+ or usage.get('reasoning_tokens')+ )+++def _usage_completion_tokens(usage: dict[str, Any]) -> int:+ return _int_value(usage.get('output_tokens')) + _reasoning_output_tokens(usage)+++def _usage_total_tokens(usage: dict[str, Any], input_tokens: int, completion_tokens: int) -> int:+ reported_total = usage.get('total_tokens')+ if reported_total is not None:+ total_tokens = _int_value(reported_total)+ if total_tokens > 0 or input_tokens + completion_tokens == 0:+ return total_tokens+ return input_tokens + completion_tokens++def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary:input_tokens = 0cached_input_tokens = 0- output_tokens = 0+ completion_tokens = 0+ total_tokens = 0cost = 0.0invocations = 0token_count_invocations = 0@@ -1546,9 +1568,12 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummarypayload = _event_payload(event)if event_type == 'model.usage':usage = _model_usage_payload(payload)- input_tokens += _int_value(usage.get('input_tokens'))+ event_input_tokens = _int_value(usage.get('input_tokens'))+ event_completion_tokens = _usage_completion_tokens(usage)+ input_tokens += event_input_tokenscached_input_tokens += _int_value(usage.get('input_cached_tokens') or usage.get('cached_input_tokens'))- output_tokens += _int_value(usage.get('output_tokens'))+ completion_tokens += event_completion_tokens+ total_tokens += _usage_total_tokens(usage, event_input_tokens, event_completion_tokens)cost += _float_value(usage.get('cost_usd') or usage.get('cost') or payload.get('cost_usd') or payload.get('cost'))invocations += 1continue@@ -1558,17 +1583,16 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummarycontinueinput_tokens = _int_value(token_usage.get('input_tokens'))cached_input_tokens = _int_value(token_usage.get('cached_input_tokens') or token_usage.get('input_cached_tokens'))- output_tokens = _int_value(token_usage.get('output_tokens'))+ completion_tokens = _usage_completion_tokens(token_usage)+ total_tokens = _usage_total_tokens(token_usage, input_tokens, completion_tokens)token_count_invocations += 1invocations = max(invocations, token_count_invocations)-- total_tokens = input_tokens + output_tokensby_model = {model: ModelUsageStats(model=model,prompt_tokens=input_tokens,- completion_tokens=output_tokens,+ completion_tokens=completion_tokens,total_tokens=total_tokens,cost=cost,invocations=invocations,@@ -1579,7 +1603,7 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummarytotal_prompt_cost=0.0,total_prompt_cached_tokens=cached_input_tokens,total_prompt_cached_cost=0.0,- total_completion_tokens=output_tokens,+ total_completion_tokens=completion_tokens,total_completion_cost=0.0,total_tokens=total_tokens,total_cost=cost,diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex b415e0e8d..ccc1f1b07 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -1032,6 +1032,51 @@ def test_rust_history_reconstructs_terminal_token_count_usage():assert history.usage.by_model['gpt-test'].completion_tokens == 31+def test_rust_history_reconstructs_terminal_reasoning_token_usage():+ from browser_use.rust.service import _history_from_events++ history = _history_from_events(+ [+ {+ 'event_type': 'token_count',+ 'payload': {+ 'info': {+ 'last_token_usage': {+ 'cached_input_tokens': 2,+ 'input_tokens': 50,+ 'output_tokens': 20,+ 'reasoning_output_tokens': 5,+ 'total_tokens': 75,+ },+ 'total_token_usage': {+ 'cached_input_tokens': 10,+ 'input_tokens': 100,+ 'output_tokens': 40,+ 'reasoning_output_tokens': 15,+ 'total_tokens': 155,+ },+ }+ },+ },+ {'event_type': 'session.done', 'payload': {'result': 'final answer'}},+ ],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=None,+ process_error=None,+ )++ assert history.usage is not None+ assert history.usage.total_prompt_tokens == 100+ assert history.usage.total_prompt_cached_tokens == 10+ assert history.usage.total_completion_tokens == 55+ assert history.usage.total_tokens == 155+ assert history.usage.entry_count == 1+ assert history.usage.by_model['gpt-test'].completion_tokens == 55+ assert history.usage.by_model['gpt-test'].total_tokens == 155++def test_rust_history_reconstructs_terminal_nested_model_usage():from browser_use.rust.service import _history_from_events