Skip to content

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

Add step number to metadata and enhance AgentHistoryList with token usage methods

Magnus Müllerb44afd51071e
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 04241aaf9..e843f1e6a 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -380,6 +380,7 @@ class Agent(Generic[Context]):
if state:
metadata = StepMetadata(
+ step_number=self.state.n_steps,
step_start_time=step_start_time,
step_end_time=step_end_time,
tokens=self._message_manager.state.history.current_tokens,
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index f82408840..2ef3ff5e3 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -99,6 +99,7 @@ class StepMetadata(BaseModel):
step_start_time: float
step_end_time: float
tokens: int # Approximate tokens from message manager for this step
+ step_number: int
@property
def duration_seconds(self) -> float:
@@ -189,7 +190,6 @@ class AgentHistoryList(BaseModel):
history: list[AgentHistory]
- @property
def total_duration_seconds(self) -> float:
"""Get total duration of all steps in seconds"""
total = 0.0
@@ -198,7 +198,6 @@ class AgentHistoryList(BaseModel):
total += h.metadata.duration_seconds
return total
- @property
def total_tokens(self) -> int:
"""
Get total tokens used across all steps.
@@ -211,6 +210,10 @@ class AgentHistoryList(BaseModel):
total += h.metadata.tokens
return total
+ def token_usage(self) -> list[int]:
+ """Get token usage for each step"""
+ return [h.metadata.tokens for h in self.history if h.metadata]
+
def __str__(self) -> str:
"""Representation of the AgentHistoryList object"""
return f'AgentHistoryList(all_results={self.action_results()}, all_model_outputs={self.model_actions()})'