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
Fix Rust Anthropic cache cost accounting
Magnus Müllerd34bee8801f9
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex de0031a9b..105336577 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -1430,6 +1430,12 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- Live proof after feature 272 showed real cache reads on real_v8 task 3: run `kh7f32qhnhq1n7zaqcqvpy5vwn881842` scored `1.0`, reported `total_prompt_cached_tokens: 221555`, and reduced agent `usage.total_cost` to `$0.0266685` for 13 invocations. The preceding five-task gate without Rust cache markers had `total_prompt_cached_tokens: 0` on every task and task-3 agent cost `$0.997485`.- Proof: terminal `cargo fmt --check -p browser-use-agent -p browser-use-llm`, terminal `cargo test -p browser-use-agent driver_passes_populated_per_call_request_to_open_stream -- --nocapture`, terminal `cargo test -p browser-use-llm build_body_marks_cache_control_breakpoints -- --nocapture`, and terminal `cargo build -p browser-use-cli`.+274. Rust Anthropic cache usage accounting parity+ - The Rust Anthropic decoder now follows the official Anthropic usage formula: total input tokens are `input_tokens + cache_read_input_tokens + cache_creation_input_tokens`.+ - Rust usage keeps cache-read tokens visible for discounted read pricing and cache-creation tokens visible for cache-write pricing, while the Python `browser_use.rust.Agent` usage bridge handles raw Anthropic fields and canonical terminal token-count events without negative prompt costs.+ - This fixes the 2026-06-04 cache gate where dashboard usage showed negative `total_prompt_cost` after Anthropic returned tiny raw `input_tokens` plus large `cache_read_input_tokens`.+ - Proof: terminal `cargo test -p browser-use-llm decoder_normalizes_anthropic_cached_usage_to_inclusive_input -- --nocapture`, terminal `cargo test -p browser-use-agent usage_total_nonzero_is_preserved -- --nocapture`, terminal `cargo test -p browser-use-providers anthropic_messages_provider_parses_text_tool_use_and_usage -- --nocapture`, browser-use `.venv/bin/python -m pytest -q tests/ci/test_rust_agent.py -k 'anthropic_raw_cache_reads or terminal_usage_prices_token_count_events'`, and browser-use `.venv/bin/python -m py_compile browser_use/rust/service.py tests/ci/test_rust_agent.py`.+## Current Verification- terminal `cargo test -p browser-use-agent driver_passes_populated_per_call_request_to_open_stream -- --nocapture`diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex fa43174a9..061ed6c88 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -2269,6 +2269,13 @@ def _float_value(value: Any) -> float:return 0.0+def _int_field(usage: dict[str, Any], keys: tuple[str, ...]) -> int:+ for key in keys:+ if key in usage:+ return _int_value(usage.get(key))+ return 0++def _token_count_usage(payload: dict[str, Any]) -> dict[str, Any] | None:info = payload.get('info')if isinstance(info, dict):@@ -2297,21 +2304,27 @@ def _optional_positive_int(value: Any) -> int | None:return integer if integer > 0 else None-def _chat_invoke_usage_from_payload(usage: dict[str, Any]) -> ChatInvokeUsage | None:+def _input_usage_buckets(usage: dict[str, Any]) -> tuple[int, int, int]:+ cache_read_tokens = _int_field(usage, ('cache_read_input_tokens', 'input_cached_tokens', 'cached_input_tokens'))+ cache_creation_tokens = _int_field(+ usage, ('cache_creation_input_tokens', 'prompt_cache_creation_tokens', 'input_cache_creation_tokens')+ )input_tokens = _int_value(usage.get('input_tokens'))- cached_input_tokens = _int_value(usage.get('input_cached_tokens') or usage.get('cached_input_tokens'))+ if 'cache_read_input_tokens' in usage or 'cache_creation_input_tokens' in usage:+ input_tokens += cache_read_tokens+ return input_tokens, cache_read_tokens, cache_creation_tokens+++def _chat_invoke_usage_from_payload(usage: dict[str, Any]) -> ChatInvokeUsage | None:+ input_tokens, cached_input_tokens, cache_creation_tokens = _input_usage_buckets(usage)completion_tokens = _usage_completion_tokens(usage)- total_tokens = _usage_total_tokens(usage, input_tokens, completion_tokens)+ total_tokens = _usage_total_tokens(usage, input_tokens, cache_creation_tokens, completion_tokens)if input_tokens == 0 and completion_tokens == 0 and total_tokens == 0:return Nonereturn ChatInvokeUsage(prompt_tokens=input_tokens,prompt_cached_tokens=cached_input_tokens or None,- prompt_cache_creation_tokens=_optional_positive_int(- usage.get('prompt_cache_creation_tokens')- or usage.get('cache_creation_input_tokens')- or usage.get('input_cache_creation_tokens')- ),+ prompt_cache_creation_tokens=cache_creation_tokens or None,prompt_image_tokens=_optional_positive_int(usage.get('prompt_image_tokens') or usage.get('image_tokens')),completion_tokens=completion_tokens,total_tokens=total_tokens,@@ -2330,13 +2343,18 @@ def _usage_completion_tokens(usage: dict[str, Any]) -> int:return _int_value(usage.get('output_tokens')) + _reasoning_output_tokens(usage)-def _usage_total_tokens(usage: dict[str, Any], input_tokens: int, completion_tokens: int) -> int:+def _usage_total_tokens(+ usage: dict[str, Any], input_tokens: int, cache_creation_tokens: int, completion_tokens: int+) -> int:+ computed_total = input_tokens + cache_creation_tokens + completion_tokens+ if 'cache_read_input_tokens' in usage or 'cache_creation_input_tokens' in usage:+ return computed_totalreported_total = usage.get('total_tokens')if reported_total is not None:total_tokens = _int_value(reported_total)- if total_tokens > 0 or input_tokens + completion_tokens == 0:+ if total_tokens > 0 or computed_total == 0:return total_tokens- return input_tokens + completion_tokens+ return computed_totaldef _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary:@@ -2353,12 +2371,14 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummarypayload = _event_payload(event)if event_type == 'model.usage':usage = _model_usage_payload(payload)- event_input_tokens = _int_value(usage.get('input_tokens'))+ event_input_tokens, event_cached_input_tokens, event_cache_creation_tokens = _input_usage_buckets(usage)event_completion_tokens = _usage_completion_tokens(usage)input_tokens += event_input_tokens- cached_input_tokens += _int_value(usage.get('input_cached_tokens') or usage.get('cached_input_tokens'))+ cached_input_tokens += event_cached_input_tokenscompletion_tokens += event_completion_tokens- total_tokens += _usage_total_tokens(usage, event_input_tokens, event_completion_tokens)+ total_tokens += _usage_total_tokens(+ usage, event_input_tokens, event_cache_creation_tokens, event_completion_tokens+ )cost += _float_value(usage.get('cost_usd') or usage.get('cost') or payload.get('cost_usd') or payload.get('cost'))invocations += 1continue@@ -2366,10 +2386,9 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummarytoken_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'))+ input_tokens, cached_input_tokens, cache_creation_tokens = _input_usage_buckets(token_usage)completion_tokens = _usage_completion_tokens(token_usage)- total_tokens = _usage_total_tokens(token_usage, input_tokens, completion_tokens)+ total_tokens = _usage_total_tokens(token_usage, input_tokens, cache_creation_tokens, completion_tokens)token_count_invocations += 1invocations = max(invocations, token_count_invocations)@@ -2474,10 +2493,9 @@ def _terminal_turn_usage_payload(events: list[dict[str, Any]]) -> dict[str, Any]def _terminal_laminar_usage_summary(raw_usage: dict[str, Any] | None) -> dict[str, int | float] | None:if raw_usage is None:return None- input_tokens = _int_value(raw_usage.get('input_tokens'))- cached_input_tokens = _int_value(raw_usage.get('input_cached_tokens') or raw_usage.get('cached_input_tokens'))+ input_tokens, cached_input_tokens, cache_creation_tokens = _input_usage_buckets(raw_usage)output_tokens = _usage_completion_tokens(raw_usage)- total_tokens = _usage_total_tokens(raw_usage, input_tokens, output_tokens)+ total_tokens = _usage_total_tokens(raw_usage, input_tokens, cache_creation_tokens, output_tokens)cost = _float_value(raw_usage.get('cost_usd') or raw_usage.get('cost') or raw_usage.get('total_cost'))summary: dict[str, int | float] = {'input_tokens': input_tokens,@@ -2485,6 +2503,8 @@ def _terminal_laminar_usage_summary(raw_usage: dict[str, Any] | None) -> dict[st'output_tokens': output_tokens,'total_tokens': total_tokens,}+ if cache_creation_tokens:+ summary['cache_creation_input_tokens'] = cache_creation_tokensreasoning_output_tokens = _reasoning_output_tokens(raw_usage)if reasoning_output_tokens:summary['reasoning_output_tokens'] = reasoning_output_tokensdiff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 0b4dd96d8..4da30ca7b 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -1723,6 +1723,43 @@ async def test_rust_terminal_usage_prices_token_count_events(monkeypatch):assert summary.by_model['claude-sonnet-4-6'].cost == pytest.approx(0.001296)+async def test_rust_terminal_usage_prices_anthropic_raw_cache_reads(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': 'model.usage',+ 'payload': {+ 'usage': {+ 'input_tokens': 12,+ 'cache_read_input_tokens': 183250,+ 'cache_creation_input_tokens': 44,+ 'output_tokens': 3088,+ },+ },+ },+ ]++ summary = await _usage_from_events_with_costs(events, 'claude-sonnet-4-6', TokenCost(include_cost=True))++ assert summary.total_prompt_tokens == 183262+ assert summary.total_prompt_cached_tokens == 183250+ assert summary.total_completion_tokens == 3088+ assert summary.total_tokens == 186394+ assert summary.total_prompt_cost == pytest.approx(0.055176)+ assert summary.total_prompt_cached_cost == pytest.approx(0.054975)+ assert summary.total_completion_cost == pytest.approx(0.04632)+ assert summary.total_cost == pytest.approx(0.101496)+ assert summary.by_model['claude-sonnet-4-6'].prompt_tokens == 183262+ assert summary.by_model['claude-sonnet-4-6'].cost == pytest.approx(0.101496)++def test_rust_history_supports_structured_output():from browser_use.rust.service import _history_from_events