Overview
TheAgent 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.
Architecture
The agent operates in a multi-step execution loop:Key Components
Message Manager
Manages conversation history and context sent to the LLM
Tools Registry
Available browser actions the agent can perform
State Manager
Tracks execution state, failures, and planning
Token Cost Service
Monitors and calculates API usage costs
Creating an Agent
Basic Example
With Custom Browser
Agent Lifecycle
Initialization (__init__)
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.
Execution Loop
Thestep() 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
Agent State Management
The agent maintains state across steps throughAgentState (line 250 in agent/views.py):
State Persistence
You can save and restore agent state:Planning System
Agents can maintain multi-step plans whenenable_planning=True (default):
The planning system helps agents break down complex tasks into smaller steps and track progress.
- 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
Loop Detection
The agent includes sophisticated loop detection (line 156 inagent/views.py):
- 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
“Heads up: you have repeated a similar action 8 times in the last 20 actions. Are you still making progress with each attempt?”
Advanced Features
Thinking Mode
Enable internal reasoning for better decision making:- Evaluation of previous action’s success
- Current memory/context
- Next goal and reasoning
Flash Mode
Optimize for speed by disabling evaluation and planning:Vision Control
Configure how the agent processes visual information:Error Handling
The agent includes robust error handling:Failure Recovery
When errors occur:- Retry with context: Error details added to next LLM call
- Consecutive failure tracking: Monitors repeated failures
- Fallback LLM: Switches to backup model if configured
- Final response: Attempts structured output even after max failures
Agent Hooks
Implement custom logic at key points:Performance Optimization
Message Compaction
Reduce prompt size by summarizing older history:Timeout Configuration
Control execution timeouts:- Gemini 3 Pro: 90 seconds
- Claude/O3/DeepSeek: 90 seconds
- Groq: 30 seconds (fast inference)
- Default: 75 seconds
Real-World Example
Next Steps
Browser Configuration
Learn how to configure browser sessions
Tools System
Explore available actions and custom tools
LLM Providers
Configure different language models
Agent Parameters
Complete parameter reference