The Agent class is the core orchestrator of Browser Use. It combines an LLM with browser automation tools to autonomously complete web tasks. The agent follows a continuous loop of observing the browser state, reasoning about the next action, executing it, and evaluating the result.
from browser_use import Agent, ChatBrowserUseagent = Agent( task="Find the top post on Hacker News", llm=ChatBrowserUse(),)# Run the agenthistory = await agent.run(max_steps=100)
When you create an agent (line 134 in agent/service.py):
LLM Setup: Validates and configures the language model
Browser Session: Creates or connects to a browser instance
Tools Registration: Sets up available actions
State Initialization: Prepares execution state tracking
Message Manager: Configures prompt management
File System: Sets up temporary workspace
The agent automatically detects the optimal configuration based on your LLM choice. For example, Claude Sonnet models automatically enable coordinate clicking and optimize screenshot sizes.
The step() method (line 1019 in agent/service.py) executes one iteration:
Phase 1: Context Preparation
Phase 2: LLM Decision
Phase 3: Action Execution
Phase 4: Post-Processing
# Get current browser statebrowser_state = await browser.get_browser_state_summary( include_screenshot=True, include_recent_events=True)# Check for new downloadsawait agent._check_and_update_downloads()# Update available actions for current pageawait agent._update_action_models_for_page(url)
# Execute actions from model outputfor action in model_output.actions: result = await tools.execute_action(action) if result.error: agent.state.consecutive_failures += 1 else: agent.state.consecutive_failures = 0
# Update plan stateagent._update_plan_from_model_output(model_output)# Record for loop detectionagent._update_loop_detector_actions()# Save to historyhistory.add_step(browser_state, model_output, result)
Agents can maintain multi-step plans when enable_planning=True (default):
The planning system helps agents break down complex tasks into smaller steps and track progress.
agent = Agent( task="Research competitors and create a comparison table", llm=llm, enable_planning=True, planning_replan_on_stall=3, # Replan after 3 consecutive failures planning_exploration_limit=5, # Nudge to create plan after 5 steps)
Planning Features:
Automatic plan generation: Agent creates step-by-step plans
Progress tracking: Monitors completion of each plan item
Dynamic replanning: Adjusts plan when stuck or failing
Exploration nudges: Encourages planning after wandering
The agent includes sophisticated loop detection (line 156 in agent/views.py):
class ActionLoopDetector: window_size: int = 20 # Rolling window of recent actions recent_action_hashes: list[str] # Normalized action hashes recent_page_fingerprints: list[PageFingerprint] max_repetition_count: int consecutive_stagnant_pages: int
Detection Triggers:
Action repetition: Same action repeated 5+ times
Page stagnation: Page unchanged for 5+ consecutive actions
Escalating nudges: Gentle warnings at 5, 8, and 12 repetitions
Example nudge message:
“Heads up: you have repeated a similar action 8 times in the last 20 actions. Are you still making progress with each attempt?”
agent = Agent( task="Task that might fail", llm=llm, max_failures=5, # Retry limit before giving up final_response_after_failure=True, # Attempt final output after max failures fallback_llm=ChatOpenAI(model='gpt-4.1-mini'), # Fallback model if primary fails)