Skip to content

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

Aggregate full Rust token usage

Magnus Müllerf853ba85574c
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index ecaf978c0..a63abc318 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -116,6 +116,11 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
synthesizes a partial final answer from the latest memory/tool evidence;
if no history exists, the saved dashboard payload still includes a
non-empty failure response with the stage error.
+ - Rust `token_count` usage reconstruction now treats summed per-turn
+ `last_token_usage` as the billed usage source when it exceeds the latest
+ cumulative context counters. Dashboard usage therefore reflects the whole
+ agent run instead of only the latest context-sized prompt after
+ recompute/compaction paths.
## Current Proof
@@ -154,6 +159,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
- browser-use `uv run pytest tests/ci/test_rust_agent.py::test_rust_sdk_client_reads_large_json_rpc_lines tests/ci/test_rust_agent.py::test_rust_agent_run_leaves_initial_navigation_for_sdk_by_default tests/ci/test_rust_agent.py::test_rust_agent_initial_actions_can_pre_navigate_existing_cdp_session tests/ci/test_rust_agent.py::test_rust_agent_translates_browser_use_args_to_terminal -q`
- browser-use `PYTHONPATH=. uv run pytest tests/ci/test_rust_agent.py -q -k 'pre_navigates_cdp_session_before_sdk_by_default or initial_actions_can_pre_navigate_existing_cdp_session or direct_initial_navigation_defaults_on_for_cdp or direct_initial_navigation_can_be_disabled'`
- browser-use `uv run pytest -q tests/ci/test_rust_agent.py -k "pre_navigates_cdp_session_before_sdk_by_default or keeps_initial_navigation_when_direct_state_mismatches or initial_actions_can_pre_navigate_existing_cdp_session or direct_initial_navigation_defaults_on_for_cdp"`
+- browser-use `uv run pytest -q tests/ci/test_rust_agent.py -k "terminal_token_count_usage or sums_token_count_last_usage_when_latest_total_underreports or terminal_usage_prices_token_count_events or terminal_usage_sums_token_count_cache_creation"`
- browser-use `python -m py_compile browser_use/rust/service.py`
- evaluations-internal `uv run python -m py_compile eval/service.py`
- evaluations-internal `python -m py_compile eval/task_types.py`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index f68b95aac..244af4306 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2921,7 +2921,11 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary
cost = 0.0
invocations = 0
token_count_invocations = 0
+ token_count_input_tokens = 0
+ token_count_cached_input_tokens = 0
token_count_cache_creation_tokens = 0
+ token_count_completion_tokens = 0
+ token_count_total_tokens = 0
for event in events:
event_type = _event_type(event)
@@ -2942,16 +2946,29 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary
token_usage = _token_count_usage(payload)
if token_usage is None:
continue
- input_tokens, cached_input_tokens, total_cache_creation_tokens = _input_usage_buckets(token_usage)
+ total_input_tokens, total_cached_input_tokens, total_cache_creation_tokens = _input_usage_buckets(token_usage)
+ total_completion_tokens = _usage_completion_tokens(token_usage)
+ total_usage_tokens = _usage_total_tokens(
+ token_usage, total_input_tokens, total_cache_creation_tokens, total_completion_tokens
+ )
last_usage = _token_count_last_usage(payload)
if isinstance(last_usage, dict):
- _, _, last_cache_creation_tokens = _input_usage_buckets(last_usage)
+ last_input_tokens, last_cached_input_tokens, last_cache_creation_tokens = _input_usage_buckets(last_usage)
+ last_completion_tokens = _usage_completion_tokens(last_usage)
+ token_count_input_tokens += last_input_tokens
+ token_count_cached_input_tokens += last_cached_input_tokens
token_count_cache_creation_tokens += last_cache_creation_tokens
+ token_count_completion_tokens += last_completion_tokens
+ token_count_total_tokens += _usage_total_tokens(
+ last_usage, last_input_tokens, last_cache_creation_tokens, last_completion_tokens
+ )
elif total_cache_creation_tokens:
token_count_cache_creation_tokens = total_cache_creation_tokens
- cache_creation_tokens = token_count_cache_creation_tokens
- completion_tokens = _usage_completion_tokens(token_usage)
- total_tokens = _usage_total_tokens(token_usage, input_tokens, cache_creation_tokens, completion_tokens)
+ input_tokens = max(total_input_tokens, token_count_input_tokens)
+ cached_input_tokens = max(total_cached_input_tokens, token_count_cached_input_tokens)
+ cache_creation_tokens = max(total_cache_creation_tokens, token_count_cache_creation_tokens)
+ completion_tokens = max(total_completion_tokens, token_count_completion_tokens)
+ total_tokens = max(total_usage_tokens, token_count_total_tokens)
token_count_invocations += 1
invocations = max(invocations, token_count_invocations)
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 582817c9c..4c1e5f031 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -1646,6 +1646,68 @@ def test_rust_history_reconstructs_terminal_token_count_usage():
assert history.usage.by_model['gpt-test'].completion_tokens == 31
+def test_rust_history_sums_token_count_last_usage_when_latest_total_underreports():
+ 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': 20,
+ 'input_tokens': 100,
+ 'output_tokens': 10,
+ 'total_tokens': 110,
+ },
+ 'total_token_usage': {
+ 'cached_input_tokens': 20,
+ 'input_tokens': 100,
+ 'output_tokens': 10,
+ 'total_tokens': 110,
+ },
+ }
+ },
+ },
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'last_token_usage': {
+ 'cached_input_tokens': 80,
+ 'input_tokens': 200,
+ 'output_tokens': 20,
+ 'total_tokens': 220,
+ },
+ # Some terminal recompute paths preserve only the latest context
+ # counters here. Dashboard usage should still reflect the full run.
+ 'total_token_usage': {
+ 'cached_input_tokens': 80,
+ 'input_tokens': 200,
+ 'output_tokens': 20,
+ 'total_tokens': 220,
+ },
+ }
+ },
+ },
+ {'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 == 300
+ assert history.usage.total_prompt_cached_tokens == 100
+ assert history.usage.total_completion_tokens == 30
+ assert history.usage.total_tokens == 330
+ assert history.usage.entry_count == 2
+
+
def test_rust_history_reconstructs_terminal_reasoning_token_usage():
from browser_use.rust.service import _history_from_events