Skip to content

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

fix max steps bug

mertunsall54bf853e8baa
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index ebf6f89e0..1ff8e059c 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1869,11 +1869,15 @@ class Agent(Generic[Context, AgentStructuredOutput]):
except Exception as e:
raise e
- self.logger.debug(f'🔄 Starting main execution loop with max {max_steps} steps...')
- for step in range(max_steps):
+ self.logger.debug(
+ f'🔄 Starting main execution loop with max {max_steps} steps (currently at step {self.state.n_steps})...'
+ )
+ while self.state.n_steps <= max_steps:
+ current_step = self.state.n_steps - 1 # Convert to 0-indexed for step_info
+
# Use the consolidated pause state management
if self.state.paused:
- self.logger.debug(f'⏸️ Step {step}: Agent paused, waiting to resume...')
+ self.logger.debug(f'⏸️ Step {self.state.n_steps}: Agent paused, waiting to resume...')
await self._external_pause_event.wait()
signal_handler.reset()
@@ -1891,8 +1895,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):
agent_run_error = 'Agent stopped programmatically'
break
- step_info = AgentStepInfo(step_number=step, max_steps=max_steps)
- is_done = await self._execute_step(step, max_steps, step_info, on_step_start, on_step_end)
+ step_info = AgentStepInfo(step_number=current_step, max_steps=max_steps)
+ is_done = await self._execute_step(current_step, max_steps, step_info, on_step_start, on_step_end)
if is_done:
# Agent has marked the task as done