Skip to content

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

Enhance agent history tracking

Magnus Müllere1b51196a072
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index cf9d813f2..a78ceb57f 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -581,10 +581,13 @@ class Agent(Generic[Context]):
self.telemetry.capture(
AgentEndTelemetryEvent(
agent_id=self.state.agent_id,
- success=self.state.history.is_done(),
+ is_done=self.state.history.is_done(),
+ success=self.state.history.is_successful(),
steps=self.state.n_steps,
max_steps_reached=self.state.n_steps >= max_steps,
errors=self.state.history.errors(),
+ total_input_tokens=self.state.history.total_input_tokens(),
+ total_duration_seconds=self.state.history.total_duration_seconds(),
)
)
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index af35710ed..17a6f2493 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -92,7 +92,7 @@ class ActionResult(BaseModel):
"""Result of executing an action"""
is_done: Optional[bool] = False
- success: Optional[bool] = True # Default to True for backward compatibility
+ success: Optional[bool] = None
extracted_content: Optional[str] = None
error: Optional[str] = None
include_in_memory: bool = False # whether to include in past messages as context or not
@@ -289,12 +289,13 @@ class AgentHistoryList(BaseModel):
return last_result.is_done is True
return False
- def is_successful(self) -> bool:
- """Check if the agent completed successfully"""
+ def is_successful(self) -> bool | None:
+ """Check if the agent completed successfully - the agent decides in the last step if it was successful or not. None if not done yet."""
if self.history and len(self.history[-1].result) > 0:
last_result = self.history[-1].result[-1]
- return last_result.is_done is True and last_result.success is True
- return False
+ if last_result.is_done is True:
+ return last_result.success
+ return None
def has_errors(self) -> bool:
"""Check if the agent has any non-None errors"""
diff --git a/browser_use/telemetry/views.py b/browser_use/telemetry/views.py
index 588b2e399..fdba27303 100644
--- a/browser_use/telemetry/views.py
+++ b/browser_use/telemetry/views.py
@@ -54,6 +54,10 @@ class AgentEndTelemetryEvent(BaseTelemetryEvent):
agent_id: str
steps: int
max_steps_reached: bool
- success: bool
+ is_done: bool
+ success: bool | None
+ total_input_tokens: int
+ total_duration_seconds: float
+
errors: Sequence[str | None]
name: str = 'agent_end'