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-greathouse61b2ee6c5bae
diff --git a/browser_use/tokens/service.py b/browser_use/tokens/service.pyindex 3c7e9e597..280e19876 100644--- a/browser_use/tokens/service.py+++ b/browser_use/tokens/service.py@@ -96,9 +96,10 @@ class TokenCost:# Check each file until we find a valid onefor cache_file in cache_files:- if await self._is_cache_valid(cache_file):+ is_valid, should_delete = await self._get_cache_status(cache_file)+ if is_valid:return cache_file- else:+ if should_delete:# Clean up old cache filestry:os.remove(cache_file)@@ -109,22 +110,23 @@ class TokenCost:except Exception:return None- async def _is_cache_valid(self, cache_file: Path) -> bool:- """Check if a specific cache file is valid and not expired"""+ async def _get_cache_status(self, cache_file: Path) -> tuple[bool, bool]:+ """Return whether a cache file is usable and whether it should be deleted."""try:if not cache_file.exists():- return False+ return False, False# Read the cached datacached = CachedPricingData.model_validate_json(await anyio.Path(cache_file).read_text())# Check if cache is still validif datetime.now() - cached.timestamp >= self.CACHE_DURATION:- return False+ return False, True- return self._cache_source_matches(cached)+ # Keep caches from other sources so different pricing URLs don't delete each other.+ return self._cache_source_matches(cached), Falseexcept Exception:- return False+ return False, Truedef _cache_source_matches(self, cached: CachedPricingData) -> bool:"""Only use cached pricing files from the same source URL."""diff --git a/tests/ci/test_token_cost_pricing_url.py b/tests/ci/test_token_cost_pricing_url.pyindex 68e5c2f3f..9c70656da 100644--- a/tests/ci/test_token_cost_pricing_url.py+++ b/tests/ci/test_token_cost_pricing_url.py@@ -83,7 +83,7 @@ class TestTokenCostPricingUrl: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 = cache_dir / 'pricing_wrong_source.json'cache_file.write_text(CachedPricingData(timestamp=datetime.now(),@@ -101,6 +101,7 @@ class TestTokenCostPricingUrl:assert pricing is not Noneassert pricing.model == 'right-model'+ assert cache_file.exists()client.get.assert_awaited_once_with(override_url, timeout=30)@pytest.mark.asyncio