Skip to content

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

better logs for tokens

Gregor Žuničb4c900d3e5a3
diff --git a/browser_use/tokens/service.py b/browser_use/tokens/service.py
index e281c059e..9fb263338 100644
--- a/browser_use/tokens/service.py
+++ b/browser_use/tokens/service.py
@@ -264,33 +264,34 @@ class TokenCost:
parts = []
- if cost:
+ # Always show token breakdown if we have cache information, regardless of cost tracking
+ if usage.prompt_cached_tokens or usage.prompt_cache_creation_tokens:
# Calculate actual new tokens (non-cached)
new_tokens = usage.prompt_tokens - (usage.prompt_cached_tokens or 0)
if new_tokens > 0:
new_tokens_fmt = self._format_tokens(new_tokens)
- if self.include_cost and cost.new_prompt_cost > 0:
+ if self.include_cost and cost and cost.new_prompt_cost > 0:
parts.append(f'🆕 {C_YELLOW}{new_tokens_fmt} (${cost.new_prompt_cost:.4f}){C_RESET}')
else:
parts.append(f'🆕 {C_YELLOW}{new_tokens_fmt}{C_RESET}')
if usage.prompt_cached_tokens:
cached_tokens_fmt = self._format_tokens(usage.prompt_cached_tokens)
- if self.include_cost and cost.prompt_read_cached_cost:
+ if self.include_cost and cost and cost.prompt_read_cached_cost:
parts.append(f'💾 {C_BLUE}{cached_tokens_fmt} (${cost.prompt_read_cached_cost:.4f}){C_RESET}')
else:
parts.append(f'💾 {C_BLUE}{cached_tokens_fmt}{C_RESET}')
if usage.prompt_cache_creation_tokens:
creation_tokens_fmt = self._format_tokens(usage.prompt_cache_creation_tokens)
- if self.include_cost and cost.prompt_cache_creation_cost:
+ if self.include_cost and cost and cost.prompt_cache_creation_cost:
parts.append(f'🔧 {C_BLUE}{creation_tokens_fmt} (${cost.prompt_cache_creation_cost:.4f}){C_RESET}')
else:
parts.append(f'🔧 {C_BLUE}{creation_tokens_fmt}{C_RESET}')
if not parts:
- # Fallback to simple display
+ # Fallback to simple display when no cache information available
total_tokens_fmt = self._format_tokens(usage.prompt_tokens)
parts.append(f'📥 {C_YELLOW}{total_tokens_fmt}{C_RESET}')
diff --git a/examples/models/gpt-4.1.py b/examples/models/gpt-4.1.py
index b5ffc9bb6..a2ecc84dd 100644
--- a/examples/models/gpt-4.1.py
+++ b/examples/models/gpt-4.1.py
@@ -5,10 +5,6 @@ Simple try of the agent.
"""
import asyncio
-import os
-import sys
-
-sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from dotenv import load_dotenv
from lmnr import Laminar
diff --git a/pyproject.toml b/pyproject.toml
index 640b4c3b9..7d4336f34 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -182,6 +182,6 @@ dev-dependencies = [
"ty>=0.0.1a1",
"pytest-xdist>=3.7.0",
"pillow>=11.2.1",
- "lmnr>=0.6.13",
+ "lmnr[all]>=0.6.13",
# "pytest-playwright-asyncio>=0.7.0", # not actually needed I think
]