Skip to content

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

Align Rust priced usage token totals

Magnus Müllerdbf1ff2a8a6c
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index a63abc318..b619ea549 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -121,6 +121,11 @@ This branch keeps the Python `Agent` unchanged unless callers explicitly import
cumulative context counters. Dashboard usage therefore reflects the whole
agent run instead of only the latest context-sized prompt after
recompute/compaction paths.
+ - Rust cost-enabled usage reconstruction now reports token totals from the
+ same summed per-turn usage entries used to calculate costs. This keeps
+ dashboard token counts, cached/cache-creation buckets, and cost fields on
+ one basis instead of showing summed cost beside latest-context token
+ counters.
## Current Proof
@@ -160,6 +165,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 `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 244af4306..87f712c2f 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2964,11 +2964,11 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary
)
elif total_cache_creation_tokens:
token_count_cache_creation_tokens = total_cache_creation_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)
+ input_tokens = max(input_tokens, total_input_tokens, token_count_input_tokens)
+ cached_input_tokens = max(cached_input_tokens, total_cached_input_tokens, token_count_cached_input_tokens)
+ cache_creation_tokens = max(cache_creation_tokens, total_cache_creation_tokens, token_count_cache_creation_tokens)
+ completion_tokens = max(completion_tokens, total_completion_tokens, token_count_completion_tokens)
+ total_tokens = max(total_tokens, total_usage_tokens, token_count_total_tokens)
token_count_invocations += 1
invocations = max(invocations, token_count_invocations)
@@ -3014,6 +3014,12 @@ async def _usage_from_events_with_costs(
total_prompt_cache_creation_cost = 0.0
total_cost = 0.0
priced_invocations = 0
+ summed_prompt_tokens = 0
+ summed_prompt_cached_tokens = 0
+ summed_prompt_cache_creation_tokens = 0
+ summed_completion_tokens = 0
+ summed_total_tokens = 0
+ summed_invocations = 0
for event in events:
event_type = _event_type(event)
@@ -3028,6 +3034,12 @@ async def _usage_from_events_with_costs(
chat_usage = _chat_invoke_usage_from_payload(raw_usage)
if chat_usage is None:
continue
+ summed_invocations += 1
+ summed_prompt_tokens += chat_usage.prompt_tokens
+ summed_prompt_cached_tokens += chat_usage.prompt_cached_tokens or 0
+ summed_prompt_cache_creation_tokens += chat_usage.prompt_cache_creation_tokens or 0
+ summed_completion_tokens += chat_usage.completion_tokens
+ summed_total_tokens += chat_usage.total_tokens
cost = await token_cost_service.calculate_cost(model, chat_usage)
if cost is None:
continue
@@ -3038,24 +3050,43 @@ async def _usage_from_events_with_costs(
total_prompt_cache_creation_cost += cost.prompt_cache_creation_cost or 0.0
total_cost += cost.total_cost
- if priced_invocations == 0:
+ if priced_invocations == 0 and summed_invocations == 0:
return summary
model_stats = dict(summary.by_model)
stats = model_stats.get(model) or ModelUsageStats(model=model)
- stats.cost = total_cost
+ if summed_invocations:
+ stats.prompt_tokens = summed_prompt_tokens
+ stats.completion_tokens = summed_completion_tokens
+ stats.total_tokens = summed_total_tokens
+ stats.invocations = max(stats.invocations, summed_invocations)
+ if priced_invocations:
+ stats.cost = total_cost
model_stats[model] = stats
- return summary.model_copy(
- update={
- 'total_prompt_cost': total_prompt_cost,
- 'total_prompt_cached_cost': total_prompt_cached_cost,
- 'total_prompt_cache_creation_cost': total_prompt_cache_creation_cost,
- 'total_completion_cost': total_completion_cost,
- 'total_cost': total_cost,
- 'by_model': model_stats,
- }
- )
+ update: dict[str, Any] = {'by_model': model_stats}
+ if summed_invocations:
+ update.update(
+ {
+ 'total_prompt_tokens': summed_prompt_tokens,
+ 'total_prompt_cached_tokens': summed_prompt_cached_tokens,
+ 'total_prompt_cache_creation_tokens': summed_prompt_cache_creation_tokens,
+ 'total_completion_tokens': summed_completion_tokens,
+ 'total_tokens': summed_total_tokens,
+ 'entry_count': max(summary.entry_count, summed_invocations),
+ }
+ )
+ if priced_invocations:
+ update.update(
+ {
+ 'total_prompt_cost': total_prompt_cost,
+ 'total_prompt_cached_cost': total_prompt_cached_cost,
+ 'total_prompt_cache_creation_cost': total_prompt_cache_creation_cost,
+ 'total_completion_cost': total_completion_cost,
+ 'total_cost': total_cost,
+ }
+ )
+ return summary.model_copy(update=update)
def _terminal_model_turn_event_ranges(events: list[dict[str, Any]]) -> list[tuple[int, int]]:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 4c1e5f031..7bcb56417 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -1790,6 +1790,58 @@ def test_rust_history_reconstructs_terminal_nested_model_usage():
assert history.usage.by_model['gpt-test'].cost == 0.0123
+def test_rust_history_token_count_does_not_shrink_model_usage_totals():
+ from browser_use.rust.service import _history_from_events
+
+ history = _history_from_events(
+ [
+ {
+ 'event_type': 'model.usage',
+ 'payload': {
+ 'input_tokens': 120_000,
+ 'cached_input_tokens': 119_000,
+ 'input_cache_creation_tokens': 4_000,
+ 'output_tokens': 2_000,
+ 'total_tokens': 126_000,
+ },
+ },
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'last_token_usage': {
+ 'input_tokens': 8_000,
+ 'cached_input_tokens': 7_000,
+ 'output_tokens': 200,
+ 'total_tokens': 8_200,
+ },
+ 'total_token_usage': {
+ 'input_tokens': 8_000,
+ 'cached_input_tokens': 7_000,
+ 'output_tokens': 200,
+ 'total_tokens': 8_200,
+ },
+ },
+ },
+ },
+ {'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 == 120_000
+ assert history.usage.total_prompt_cached_tokens == 119_000
+ assert history.usage.total_prompt_cache_creation_tokens == 4_000
+ assert history.usage.total_completion_tokens == 2_000
+ assert history.usage.total_tokens == 126_000
+ assert history.usage.entry_count == 1
+
+
async def test_rust_terminal_usage_prices_token_count_events(monkeypatch):
from browser_use.rust.service import _usage_from_events_with_costs
from browser_use.tokens.service import TokenCost
@@ -1982,6 +2034,117 @@ async def test_rust_terminal_usage_sums_token_count_cache_creation(monkeypatch):
assert summary.total_cost == pytest.approx(0.11142345)
+async def test_rust_terminal_usage_priced_summary_sums_cache_read_tokens(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)
+
+ events = [
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'last_token_usage': {
+ 'cached_input_tokens': 1000,
+ 'input_cache_creation_tokens': 200,
+ 'input_tokens': 1001,
+ 'output_tokens': 10,
+ 'total_tokens': 1211,
+ },
+ 'total_token_usage': {
+ 'cached_input_tokens': 1000,
+ 'input_cache_creation_tokens': 200,
+ 'input_tokens': 1001,
+ 'output_tokens': 10,
+ 'total_tokens': 1211,
+ },
+ },
+ },
+ },
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'last_token_usage': {
+ 'cached_input_tokens': 1000,
+ 'input_cache_creation_tokens': 50,
+ 'input_tokens': 1001,
+ 'output_tokens': 20,
+ 'total_tokens': 1071,
+ },
+ 'total_token_usage': {
+ 'cached_input_tokens': 1000,
+ 'input_cache_creation_tokens': 200,
+ 'input_tokens': 1001,
+ 'output_tokens': 30,
+ 'total_tokens': 1231,
+ },
+ },
+ },
+ },
+ ]
+
+ summary = await _usage_from_events_with_costs(events, 'claude-sonnet-4-6', TokenCost(include_cost=True))
+
+ assert summary.entry_count == 2
+ assert summary.total_prompt_tokens == 2002
+ assert summary.total_prompt_cached_tokens == 2000
+ assert summary.total_prompt_cache_creation_tokens == 250
+ assert summary.total_completion_tokens == 30
+ assert summary.total_tokens == 2282
+ assert summary.total_prompt_cached_cost == pytest.approx(2000 * (0.30 / 1_000_000))
+ assert summary.total_prompt_cache_creation_cost == pytest.approx(250 * (3.75 / 1_000_000))
+ assert summary.total_completion_cost == pytest.approx(30 * (15 / 1_000_000))
+ assert summary.by_model['claude-sonnet-4-6'].prompt_tokens == 2002
+ assert summary.by_model['claude-sonnet-4-6'].total_tokens == 2282
+ assert summary.by_model['claude-sonnet-4-6'].invocations == 2
+
+
+def test_rust_terminal_usage_mixed_events_do_not_shrink_totals():
+ from browser_use.rust.service import _usage_from_events
+
+ summary = _usage_from_events(
+ [
+ {
+ 'event_type': 'model.usage',
+ 'payload': {
+ 'usage': {
+ 'input_tokens': 500,
+ 'cached_input_tokens': 200,
+ 'input_cache_creation_tokens': 50,
+ 'output_tokens': 20,
+ 'total_tokens': 570,
+ }
+ },
+ },
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'total_token_usage': {
+ 'input_tokens': 10,
+ 'cached_input_tokens': 5,
+ 'output_tokens': 1,
+ 'total_tokens': 11,
+ }
+ }
+ },
+ },
+ ],
+ 'claude-sonnet-4-6',
+ )
+
+ 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
+
+
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