Skip to content

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

Reconstruct Rust terminal token usage

Magnus Müller3608fd969c56
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 82e508f34..d68ed87d1 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -614,10 +614,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- Proof: `test_rust_history_surfaces_terminal_stream_error_message`.
- Evidence: real_v8 cloud task `18` reached Browser Use cloud and emitted terminal `stream_error` provider messages before this reconstruction fix.
+121. Rust Agent terminal token-count usage reconstruction
+ - Terminal current-main `token_count` events are now reconstructed into Browser Use `UsageSummary` totals.
+ - The wrapper uses the latest cumulative `total_token_usage` snapshot so Python callers see prompt, cached prompt, completion, total token, and per-model usage values for terminal-main runs.
+ - Proof: `test_rust_history_reconstructs_terminal_token_count_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` (93 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (94 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 b3381a8c4..dffa674d7 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -819,22 +819,57 @@ def _browser_state_from_events(events: list[dict[str, Any]]) -> BrowserStateHist
return BrowserStateHistory(url=url, title=title, tabs=tabs, interacted_element=[])
+def _int_value(value: Any) -> int:
+ try:
+ return int(value or 0)
+ except (TypeError, ValueError):
+ return 0
+
+
+def _float_value(value: Any) -> float:
+ try:
+ return float(value or 0.0)
+ except (TypeError, ValueError):
+ return 0.0
+
+
+def _token_count_usage(payload: dict[str, Any]) -> dict[str, Any] | None:
+ info = payload.get('info')
+ if isinstance(info, dict):
+ raw_usage = info.get('total_token_usage') or info.get('last_token_usage')
+ else:
+ raw_usage = payload.get('total_token_usage') or payload.get('last_token_usage')
+ return raw_usage if isinstance(raw_usage, dict) else None
+
+
def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary:
input_tokens = 0
cached_input_tokens = 0
output_tokens = 0
cost = 0.0
invocations = 0
+ token_count_invocations = 0
for event in events:
- if _event_type(event) != 'model.usage':
- continue
+ event_type = _event_type(event)
payload = _event_payload(event)
- input_tokens += int(payload.get('input_tokens') or 0)
- cached_input_tokens += int(payload.get('input_cached_tokens') or payload.get('cached_input_tokens') or 0)
- output_tokens += int(payload.get('output_tokens') or 0)
- cost += float(payload.get('cost_usd') or payload.get('cost') or 0.0)
- invocations += 1
+ 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'))
+ invocations += 1
+ continue
+ if event_type == 'token_count':
+ token_usage = _token_count_usage(payload)
+ if token_usage is None:
+ continue
+ input_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'))
+ token_count_invocations += 1
+
+ invocations = max(invocations, token_count_invocations)
total_tokens = input_tokens + output_tokens
by_model = {
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 0be450175..e8719428e 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -256,6 +256,49 @@ def test_rust_events_reconstruct_browser_use_history():
assert history.usage.total_completion_tokens == 7
+def test_rust_history_reconstructs_terminal_token_count_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': 5,
+ 'output_tokens': 7,
+ 'total_tokens': 12,
+ },
+ 'total_token_usage': {
+ 'cached_input_tokens': 13,
+ 'input_tokens': 29,
+ 'output_tokens': 31,
+ 'total_tokens': 60,
+ },
+ }
+ },
+ },
+ {'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 == 29
+ assert history.usage.total_prompt_cached_tokens == 13
+ assert history.usage.total_completion_tokens == 31
+ assert history.usage.total_tokens == 60
+ assert history.usage.entry_count == 1
+ assert history.usage.by_model['gpt-test'].prompt_tokens == 29
+ assert history.usage.by_model['gpt-test'].completion_tokens == 31
+
+
def test_rust_history_supports_structured_output():
from browser_use.rust.service import _history_from_events