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: By model cost compute
2pac4195cdfb6eb5
diff --git a/browser_use/tokens/service.py b/browser_use/tokens/service.pyindex c7a159260..aab1aa0e4 100644--- a/browser_use/tokens/service.py+++ b/browser_use/tokens/service.py@@ -407,6 +407,7 @@ class TokenCost:# Calculate cost record by record using the updated calculate_cost functioncost = await self.calculate_cost(entry.model, entry.usage)if cost:+ stats.cost += cost.total_costtotal_prompt_cost += cost.prompt_costtotal_completion_cost += cost.completion_costtotal_prompt_cached_cost += cost.prompt_read_cached_cost or 0diff --git a/browser_use/tokens/tests/test_cost.py b/browser_use/tokens/tests/test_cost.pyindex 2fec84688..fc003916b 100644--- a/browser_use/tokens/tests/test_cost.py+++ b/browser_use/tokens/tests/test_cost.py@@ -19,7 +19,7 @@ async def test_iterative_country_generation():"""Test token cost tracking with iterative country generation"""# Initialize token cost service- tc = TokenCost()+ tc = TokenCost(include_cost=True)# System prompt that explains the iterative tasksystem_prompt = """You are a country name generator. When asked, you will provide exactly ONE country name and nothing else.@@ -75,8 +75,14 @@ Only output the country name, no numbers, no punctuation, just the name."""print(f'Total tokens: {summary.total_tokens:,}')print(f'Total cost: ${summary.total_cost:.6f}')+ expected_cost = 0+ expected_invocations = 0+print('\n📊 Cost breakdown by model:')for model, stats in summary.by_model.items():+ expected_cost += stats.cost+ expected_invocations += stats.invocations+print(f'\n{model}:')print(f' Calls: {stats.invocations}')print(f' Prompt tokens: {stats.prompt_tokens:,}')@@ -85,6 +91,11 @@ Only output the country name, no numbers, no punctuation, just the name."""print(f' Cost: ${stats.cost:.6f}')print(f' Average tokens per call: {stats.average_tokens_per_invocation:.1f}')+ assert summary.entry_count == expected_invocations, f'Expected {expected_invocations} invocations, got {summary.entry_count}'+ assert abs(summary.total_cost - expected_cost) < 1e-6, (+ f'Expected total cost ${expected_cost:.6f}, got ${summary.total_cost:.6f}'+ )+if __name__ == '__main__':# Run the test