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
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.pyindex 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.pyindex 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: floatstep_end_time: floattokens: int # Approximate tokens from message manager for this step+ step_number: int@propertydef duration_seconds(self) -> float:@@ -189,7 +190,6 @@ class AgentHistoryList(BaseModel):history: list[AgentHistory]- @propertydef 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_secondsreturn total- @propertydef total_tokens(self) -> int:"""Get total tokens used across all steps.@@ -211,6 +210,10 @@ class AgentHistoryList(BaseModel):total += h.metadata.tokensreturn 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()})'