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 nested model usage
Magnus Müller0d236058380f
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex b4306d361..258729921 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -689,10 +689,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- The fallback text mirrors terminal current-main synthetic tool-result semantics while preserving concrete `tool.output`, failure, abort, and provider input-item results when those are present.- Proof: `test_rust_history_reconstructs_terminal_tool_finished_results`.+136. Rust terminal nested model-usage reconstruction+ - Terminal `model.usage` events with provider usage nested under a `usage` object now populate Browser Use `UsageSummary`.+ - This preserves prompt, cached prompt, completion, cost, and invocation data for replay/import logs that use terminal's nested usage shape instead of the older flat usage payload.+ - Proof: `test_rust_history_reconstructs_terminal_nested_model_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` (107 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (108 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 ffce975a8..1df299664 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -1399,6 +1399,11 @@ def _token_count_usage(payload: dict[str, Any]) -> dict[str, Any] | None:return raw_usage if isinstance(raw_usage, dict) else None+def _model_usage_payload(payload: dict[str, Any]) -> dict[str, Any]:+ usage = payload.get('usage')+ return usage if isinstance(usage, dict) else payload++def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary:input_tokens = 0cached_input_tokens = 0@@ -1411,10 +1416,11 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummaryevent_type = _event_type(event)payload = _event_payload(event)if event_type == 'model.usage':- input_tokens += _int_value(payload.get('input_tokens'))- cached_input_tokens += _int_value(payload.get('input_cached_tokens') or payload.get('cached_input_tokens'))- output_tokens += _int_value(payload.get('output_tokens'))- cost += _float_value(payload.get('cost_usd') or payload.get('cost'))+ usage = _model_usage_payload(payload)+ input_tokens += _int_value(usage.get('input_tokens'))+ cached_input_tokens += _int_value(usage.get('input_cached_tokens') or usage.get('cached_input_tokens'))+ output_tokens += _int_value(usage.get('output_tokens'))+ cost += _float_value(usage.get('cost_usd') or usage.get('cost') or payload.get('cost_usd') or payload.get('cost'))invocations += 1continueif event_type == 'token_count':diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex db1db5974..7ca6abbce 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -820,6 +820,43 @@ 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_nested_model_usage():+ from browser_use.rust.service import _history_from_events++ history = _history_from_events(+ [+ {+ 'event_type': 'model.usage',+ 'payload': {+ 'usage': {+ 'input_tokens': 17,+ 'cached_input_tokens': 5,+ 'output_tokens': 11,+ },+ 'cost_usd': 0.0123,+ },+ },+ {'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 == 17+ assert history.usage.total_prompt_cached_tokens == 5+ assert history.usage.total_completion_tokens == 11+ assert history.usage.total_tokens == 28+ assert history.usage.total_cost == 0.0123+ assert history.usage.entry_count == 1+ assert history.usage.by_model['gpt-test'].prompt_tokens == 17+ assert history.usage.by_model['gpt-test'].completion_tokens == 11+ assert history.usage.by_model['gpt-test'].cost == 0.0123++def test_rust_history_supports_structured_output():from browser_use.rust.service import _history_from_events