Skip to main content

Overview

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.

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):
  1. LLM Setup: Validates and configures the language model
  2. Browser Session: Creates or connects to a browser instance
  3. Tools Registration: Sets up available actions
  4. State Initialization: Prepares execution state tracking
  5. Message Manager: Configures prompt management
  6. 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

The step() method (line 1019 in agent/service.py) executes one iteration:

Agent State Management

The agent maintains state across steps through AgentState (line 250 in agent/views.py):

State Persistence

You can save and restore agent state:

Planning System

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.
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

Loop Detection

The agent includes sophisticated loop detection (line 156 in agent/views.py):
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?”

Advanced Features

Thinking Mode

Enable internal reasoning for better decision making:
When enabled, each step includes the agent’s thought process:
  • Evaluation of previous action’s success
  • Current memory/context
  • Next goal and reasoning

Flash Mode

Optimize for speed by disabling evaluation and planning:
Flash mode disables use_thinking, planning, and evaluation. Only use for simple, straightforward tasks.

Vision Control

Configure how the agent processes visual information:

Error Handling

The agent includes robust error handling:

Failure Recovery

When errors occur:
  1. Retry with context: Error details added to next LLM call
  2. Consecutive failure tracking: Monitors repeated failures
  3. Fallback LLM: Switches to backup model if configured
  4. 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:
Auto-detected timeouts by model:
  • 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