Skip to content

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

Avoid double-counting mixed Rust usage events

Magnus Müllerdcb196990076
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index b619ea549..fd1ac69db 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -126,6 +126,10 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
dashboard token counts, cached/cache-creation buckets, and cost fields on
one basis instead of showing summed cost beside latest-context token
counters.
+ - Rust cost-enabled usage reconstruction now treats `token_count` as a
+ fallback billing source when provider `model.usage` events are absent.
+ Mixed streams therefore do not double-count usage by pricing both provider
+ usage and context occupancy counters.
## Current Proof
@@ -165,7 +169,7 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
- 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 `uv run pytest -q tests/ci/test_rust_agent.py -k "terminal_nested_model_usage or token_count_does_not_shrink_model_usage_totals or terminal_usage_prices_token_count_events or terminal_usage_prices_anthropic_raw_cache_reads or terminal_usage_sums_token_count_cache_creation or priced_summary_sums_cache_read_tokens or mixed_events_do_not_shrink_totals or sums_token_count_last_usage_when_latest_total_underreports"`
+- browser-use `uv run pytest -q tests/ci/test_rust_agent.py -k "terminal_nested_model_usage or token_count_does_not_shrink_model_usage_totals or terminal_usage_prices_token_count_events or terminal_usage_prices_anthropic_raw_cache_reads or terminal_usage_sums_token_count_cache_creation or priced_summary_sums_cache_read_tokens or mixed_events_do_not_shrink_totals or priced_usage_prefers_model_usage_over_token_count or sums_token_count_last_usage_when_latest_total_underreports"`
- 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 87f712c2f..b4cf61d4a 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -3020,6 +3020,7 @@ async def _usage_from_events_with_costs(
summed_completion_tokens = 0
summed_total_tokens = 0
summed_invocations = 0
+ has_model_usage = any(_event_type(event) == 'model.usage' for event in events)
for event in events:
event_type = _event_type(event)
@@ -3028,6 +3029,8 @@ async def _usage_from_events_with_costs(
if event_type == 'model.usage':
raw_usage = _model_usage_payload(payload)
elif event_type == 'token_count':
+ if has_model_usage:
+ continue
raw_usage = _token_count_last_usage(payload)
if not isinstance(raw_usage, dict):
continue
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 7bcb56417..8641e6069 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -2145,6 +2145,65 @@ def test_rust_terminal_usage_mixed_events_do_not_shrink_totals():
assert summary.total_tokens == 570
+async def test_rust_terminal_priced_usage_prefers_model_usage_over_token_count(monkeypatch):
+ from browser_use.rust.service import _usage_from_events_with_costs
+ from browser_use.tokens.service import TokenCost
+
+ async def fail_fetch(_self):
+ raise AssertionError('custom model pricing should not fetch remote pricing')
+
+ monkeypatch.setattr(TokenCost, '_fetch_and_cache_pricing_data', fail_fetch)
+
+ summary = await _usage_from_events_with_costs(
+ [
+ {
+ 'event_type': 'model.usage',
+ 'payload': {
+ 'input_tokens': 500,
+ 'cached_input_tokens': 200,
+ 'input_cache_creation_tokens': 50,
+ 'output_tokens': 20,
+ 'total_tokens': 570,
+ },
+ },
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'last_token_usage': {
+ 'input_tokens': 500,
+ 'cached_input_tokens': 200,
+ 'input_cache_creation_tokens': 50,
+ 'output_tokens': 20,
+ 'total_tokens': 570,
+ },
+ 'total_token_usage': {
+ 'input_tokens': 500,
+ 'cached_input_tokens': 200,
+ 'input_cache_creation_tokens': 50,
+ 'output_tokens': 20,
+ 'total_tokens': 570,
+ },
+ }
+ },
+ },
+ ],
+ 'claude-sonnet-4-6',
+ TokenCost(include_cost=True),
+ )
+
+ assert summary.total_prompt_tokens == 500
+ assert summary.total_prompt_cached_tokens == 200
+ assert summary.total_prompt_cache_creation_tokens == 50
+ assert summary.total_completion_tokens == 20
+ assert summary.total_tokens == 570
+ assert summary.entry_count == 1
+ assert summary.total_prompt_cached_cost == pytest.approx(200 * (0.30 / 1_000_000))
+ assert summary.total_prompt_cache_creation_cost == pytest.approx(50 * (3.75 / 1_000_000))
+ assert summary.total_completion_cost == pytest.approx(20 * (15 / 1_000_000))
+ assert summary.total_cost == pytest.approx((300 * 3 + 200 * 0.30 + 50 * 3.75 + 20 * 15) / 1_000_000)
+
+
async def test_rust_token_summary_does_not_double_count_cache_reads(monkeypatch):
from browser_use.llm.views import ChatInvokeUsage
from browser_use.tokens.service import TokenCost