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
feat(tokens): allow overriding model pricing URL
matt-greathouse1c5020050c93
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 08e61d51e..9d9f751ce 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -187,6 +187,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):file_system_path: str | None = None,task_id: str | None = None,calculate_cost: bool = False,+ pricing_url: str | None = None,display_files_in_done_text: bool = True,include_tool_call_examples: bool = False,vision_detail_level: Literal['auto', 'low', 'high'] = 'auto',@@ -413,7 +414,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):)# Token cost service- self.token_cost_service = TokenCost(include_cost=calculate_cost)+ self.token_cost_service = TokenCost(include_cost=calculate_cost, pricing_url=pricing_url)self.token_cost_service.register_llm(llm)self.token_cost_service.register_llm(page_extraction_llm)self.token_cost_service.register_llm(judge_llm)diff --git a/browser_use/code_use/service.py b/browser_use/code_use/service.pynew file mode 100644index 000000000..e69de29bbdiff --git a/browser_use/config.py b/browser_use/config.pyindex a951c4a39..3452df4ec 100644--- a/browser_use/config.py+++ b/browser_use/config.py@@ -76,6 +76,13 @@ class OldConfig:raise AssertionError('BROWSER_USE_CLOUD_UI_URL must be a valid URL if set')return url+ @property+ def BROWSER_USE_MODEL_PRICING_URL(self) -> str:+ url = os.getenv('BROWSER_USE_MODEL_PRICING_URL', '')+ if url and '://' not in url:+ raise AssertionError('BROWSER_USE_MODEL_PRICING_URL must be a valid URL if set')+ return url+# Path configuration@propertydef XDG_CACHE_HOME(self) -> Path:@@ -195,6 +202,7 @@ class FlatEnvConfig(BaseSettings):BROWSER_USE_CLOUD_SYNC: bool | None = Field(default=None)BROWSER_USE_CLOUD_API_URL: str = Field(default='https://api.browser-use.com')BROWSER_USE_CLOUD_UI_URL: str = Field(default='')+ BROWSER_USE_MODEL_PRICING_URL: str = Field(default='')# Path configurationXDG_CACHE_HOME: str = Field(default='~/.cache')diff --git a/browser_use/tokens/service.py b/browser_use/tokens/service.pyindex edc735b86..3c7e9e597 100644--- a/browser_use/tokens/service.py+++ b/browser_use/tokens/service.py@@ -50,10 +50,11 @@ class TokenCost:CACHE_DIR_NAME = 'browser_use/token_cost'CACHE_DURATION = timedelta(days=1)- PRICING_URL = 'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json'+ DEFAULT_PRICING_URL = 'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json'- def __init__(self, include_cost: bool = False):+ def __init__(self, include_cost: bool = False, pricing_url: str | None = None):self.include_cost = include_cost or os.getenv('BROWSER_USE_CALCULATE_COST', 'false').lower() == 'true'+ self.pricing_url = pricing_url or CONFIG.BROWSER_USE_MODEL_PRICING_URL or self.DEFAULT_PRICING_URLself.usage_history: list[TokenUsageEntry] = []self.registered_llms: dict[str, BaseChatModel] = {}@@ -118,10 +119,20 @@ class TokenCost:cached = CachedPricingData.model_validate_json(await anyio.Path(cache_file).read_text())# Check if cache is still valid- return datetime.now() - cached.timestamp < self.CACHE_DURATION+ if datetime.now() - cached.timestamp >= self.CACHE_DURATION:+ return False++ return self._cache_source_matches(cached)except Exception:return False+ def _cache_source_matches(self, cached: CachedPricingData) -> bool:+ """Only use cached pricing files from the same source URL."""+ if cached.source_url is None:+ return self.pricing_url == self.DEFAULT_PRICING_URL++ return cached.source_url == self.pricing_url+async def _load_from_cache(self, cache_file: Path) -> None:"""Load pricing data from a specific cache file"""try:@@ -137,13 +148,13 @@ class TokenCost:"""Fetch pricing data from LiteLLM GitHub and cache it with timestamp"""try:async with httpx.AsyncClient() as client:- response = await client.get(self.PRICING_URL, timeout=30)+ response = await client.get(self.pricing_url, timeout=30)response.raise_for_status()self._pricing_data = response.json()# Create cache object with timestamp- cached = CachedPricingData(timestamp=datetime.now(), data=self._pricing_data or {})+ cached = CachedPricingData(timestamp=datetime.now(), source_url=self.pricing_url, data=self._pricing_data or {})# Ensure cache directory existsself._cache_dir.mkdir(parents=True, exist_ok=True)diff --git a/browser_use/tokens/views.py b/browser_use/tokens/views.pyindex dfa55179a..5a3deb14f 100644--- a/browser_use/tokens/views.py+++ b/browser_use/tokens/views.py@@ -65,6 +65,7 @@ class CachedPricingData(BaseModel):"""Cached pricing data with timestamp"""timestamp: datetime+ source_url: str | None = Nonedata: dict[str, Any]diff --git a/tests/ci/test_token_cost_pricing_url.py b/tests/ci/test_token_cost_pricing_url.pynew file mode 100644index 000000000..68e5c2f3f--- /dev/null+++ b/tests/ci/test_token_cost_pricing_url.py@@ -0,0 +1,205 @@+from datetime import datetime+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from pydantic_core import core_schema++from browser_use.agent.service import Agent+from browser_use.code_use.service import CodeAgent+from browser_use.llm.views import ChatInvokeUsage+from browser_use.tokens.service import TokenCost+from browser_use.tokens.views import CachedPricingData+from tests.ci.conftest import create_mock_llm+++def pricing_entry(input_cost: float = 0.000001, output_cost: float = 0.000002) -> dict[str, float | None]:+ return {+ 'input_cost_per_token': input_cost,+ 'output_cost_per_token': output_cost,+ 'cache_read_input_token_cost': None,+ 'cache_creation_input_token_cost': None,+ }+++def build_async_client(response_json: dict) -> AsyncMock:+ response = MagicMock()+ response.raise_for_status = MagicMock()+ response.json.return_value = response_json++ client = AsyncMock()+ client.get = AsyncMock(return_value=response)+ client.__aenter__ = AsyncMock(return_value=client)+ client.__aexit__ = AsyncMock(return_value=None)+ return client+++class TestTokenCostPricingUrl:+ @pytest.mark.asyncio+ async def test_uses_default_pricing_url_when_no_override(self, monkeypatch, tmp_path):+ monkeypatch.setenv('XDG_CACHE_HOME', str(tmp_path))+ monkeypatch.delenv('BROWSER_USE_MODEL_PRICING_URL', raising=False)++ client = build_async_client({})++ with patch('browser_use.tokens.service.httpx.AsyncClient', return_value=client):+ token_cost = TokenCost(include_cost=True)+ await token_cost.initialize()++ assert token_cost.pricing_url == TokenCost.DEFAULT_PRICING_URL+ client.get.assert_awaited_once_with(TokenCost.DEFAULT_PRICING_URL, timeout=30)++ @pytest.mark.asyncio+ async def test_uses_env_pricing_url_override(self, monkeypatch, tmp_path):+ override_url = 'https://pricing.example/env.json'+ monkeypatch.setenv('XDG_CACHE_HOME', str(tmp_path))+ monkeypatch.setenv('BROWSER_USE_MODEL_PRICING_URL', override_url)++ client = build_async_client({})++ with patch('browser_use.tokens.service.httpx.AsyncClient', return_value=client):+ token_cost = TokenCost(include_cost=True)+ await token_cost.initialize()++ assert token_cost.pricing_url == override_url+ client.get.assert_awaited_once_with(override_url, timeout=30)++ @pytest.mark.asyncio+ async def test_constructor_pricing_url_overrides_env(self, monkeypatch, tmp_path):+ constructor_url = 'https://pricing.example/constructor.json'+ monkeypatch.setenv('XDG_CACHE_HOME', str(tmp_path))+ monkeypatch.setenv('BROWSER_USE_MODEL_PRICING_URL', 'https://pricing.example/env.json')++ client = build_async_client({})++ with patch('browser_use.tokens.service.httpx.AsyncClient', return_value=client):+ token_cost = TokenCost(include_cost=True, pricing_url=constructor_url)+ await token_cost.initialize()++ assert token_cost.pricing_url == constructor_url+ client.get.assert_awaited_once_with(constructor_url, timeout=30)++ @pytest.mark.asyncio+ async def test_ignores_cache_files_from_different_source_url(self, monkeypatch, tmp_path):+ override_url = 'https://pricing.example/right.json'+ cache_dir = tmp_path / 'browser_use' / 'token_cost'+ cache_dir.mkdir(parents=True)+ cache_file = cache_dir / 'pricing_legacy.json'+ cache_file.write_text(+ CachedPricingData(+ timestamp=datetime.now(),+ source_url='https://pricing.example/wrong.json',+ data={'wrong-model': pricing_entry()},+ ).model_dump_json()+ )++ monkeypatch.setenv('XDG_CACHE_HOME', str(tmp_path))+ client = build_async_client({'right-model': pricing_entry()})++ with patch('browser_use.tokens.service.httpx.AsyncClient', return_value=client):+ token_cost = TokenCost(include_cost=True, pricing_url=override_url)+ pricing = await token_cost.get_model_pricing('right-model')++ assert pricing is not None+ assert pricing.model == 'right-model'+ client.get.assert_awaited_once_with(override_url, timeout=30)++ @pytest.mark.asyncio+ async def test_legacy_cache_without_source_url_works_for_default_url(self, monkeypatch, tmp_path):+ cache_dir = tmp_path / 'browser_use' / 'token_cost'+ cache_dir.mkdir(parents=True)+ cache_file = cache_dir / 'pricing_legacy.json'+ cache_file.write_text(+ CachedPricingData(timestamp=datetime.now(), data={'legacy-model': pricing_entry()}).model_dump_json(exclude_none=True)+ )++ monkeypatch.setenv('XDG_CACHE_HOME', str(tmp_path))+ monkeypatch.delenv('BROWSER_USE_MODEL_PRICING_URL', raising=False)++ with patch('browser_use.tokens.service.httpx.AsyncClient') as mock_client_class:+ token_cost = TokenCost(include_cost=True)+ pricing = await token_cost.get_model_pricing('legacy-model')++ assert pricing is not None+ assert pricing.model == 'legacy-model'+ mock_client_class.assert_not_called()++ @pytest.mark.asyncio+ async def test_custom_pricing_still_takes_precedence(self):+ token_cost = TokenCost(include_cost=False)+ token_cost._initialized = True+ token_cost._pricing_data = {+ 'bu-1-0': {+ 'input_cost_per_token': 999,+ 'output_cost_per_token': 999,+ }+ }++ pricing = await token_cost.get_model_pricing('bu-1-0')++ assert pricing is not None+ assert pricing.input_cost_per_token != 999+ assert pricing.output_cost_per_token != 999++ @pytest.mark.asyncio+ async def test_override_source_can_supply_missing_qwen_pricing(self, monkeypatch, tmp_path):+ override_url = 'https://pricing.example/qwen.json'+ monkeypatch.setenv('XDG_CACHE_HOME', str(tmp_path))++ qwen_model = 'openrouter/qwen/qwen3.5-flash-02-23'+ client = build_async_client({qwen_model: pricing_entry(0.000003, 0.000007)})++ with patch('browser_use.tokens.service.httpx.AsyncClient', return_value=client):+ token_cost = TokenCost(include_cost=True, pricing_url=override_url)+ cost = await token_cost.calculate_cost(+ qwen_model,+ ChatInvokeUsage(+ prompt_tokens=100,+ prompt_cached_tokens=None,+ prompt_cache_creation_tokens=None,+ prompt_image_tokens=None,+ completion_tokens=25,+ total_tokens=125,+ ),+ )++ assert cost is not None+ assert cost.total_cost > 0+ client.get.assert_awaited_once_with(override_url, timeout=30)+++class TestPricingUrlPlumbing:+ def test_agent_passes_pricing_url_to_token_cost(self):+ llm = create_mock_llm()++ with patch('browser_use.agent.service.TokenCost') as token_cost_cls:+ token_cost = MagicMock()+ token_cost_cls.return_value = token_cost++ Agent(task='Test task', llm=llm, pricing_url='https://pricing.example/agent.json')++ token_cost_cls.assert_called_once_with(include_cost=False, pricing_url='https://pricing.example/agent.json')++ def test_code_agent_passes_pricing_url_to_token_cost(self):+ class MockChatBrowserUse:+ model = 'mock-browser-use'+ _verified_api_keys = True+ provider = 'mock'+ name = 'mock-browser-use'+ model_name = 'mock-browser-use'++ async def ainvoke(self, messages, output_format=None, **kwargs):+ raise NotImplementedError++ @classmethod+ def __get_pydantic_core_schema__(cls, source_type, handler):+ return core_schema.any_schema()++ llm = MockChatBrowserUse()++ with patch('browser_use.code_use.service.TokenCost') as token_cost_cls:+ token_cost = MagicMock()+ token_cost_cls.return_value = token_cost++ CodeAgent(task='Test task', llm=llm, pricing_url='https://pricing.example/code.json')++ token_cost_cls.assert_called_once_with(include_cost=False, pricing_url='https://pricing.example/code.json')