Home
Relevant source files
The following files were used as context for generating this wiki page:browser-use is an asynchronous Python library (version >= 3.11) that provides AI-driven browser automation capabilities. It leverages Large Language Models (LLMs) and the Chrome DevTools Protocol (CDP) to enable AI agents to autonomously navigate web pages, interact with elements, and execute complex tasks. The system is designed for a variety of use cases, including web scraping, automated testing, research, and form filling.
Sources: CLAUDE.md:3-5, pyproject.toml:6
The project can be utilized as a Python library within scripts, through a comprehensive command-line interface (CLI), or as a scalable cloud service. It features an event-driven architecture, a flexible tools system for defining agent capabilities, and integration with multiple LLM providers.
Sources: README.md:28-44, CLAUDE.md:7-8
High-Level Architecture
The library is built on an event-driven architecture, orchestrating several core components to manage browser automation tasks. The BrowserSession uses a bubus event bus to coordinate various watchdog services that handle different aspects of browser interaction.
Sources: CLAUDE.md:7-8, 17-18
This diagram illustrates the main components and their interactions.
Sources: CLAUDE.md:10-16
Core Components
- Agent (
browser_use/agent/service.py): The central orchestrator that receives tasks, manages the browser, and executes the LLM-driven action loop. - BrowserSession (
browser_use/browser/session.py): Manages the browser lifecycle, CDP connections viacdp-use, and coordinates watchdog services through an event bus. - Tools (
browser_use/tools/service.py): An action registry that translates LLM decisions into concrete browser operations like clicking, typing, and scrolling. - DomService (
browser_use/dom/service.py): Extracts and processes DOM content, handles element highlighting, and generates the accessibility tree. - LLM Integration (
browser_use/llm/): An abstraction layer that supports various LLM providers, including OpenAI, Anthropic, and Google.
Sources: CLAUDE.md:10-16
Event-Driven Browser Management
The BrowserSession utilizes a bubus event bus to coordinate several watchdog services, each responsible for a specific domain of browser management:
- DOMWatchdog: Processes DOM snapshots, screenshots, and element highlighting.
- DownloadsWatchdog: Manages file downloads.
- PopupsWatchdog: Handles JavaScript dialogs.
- SecurityWatchdog: Enforces domain restrictions.
- AboutBlankWatchdog: Manages empty page redirects.
Sources: CLAUDE.md:17-25, browser_use/browser/watchdogs/dom_watchdog.py
Getting Started
Installation
The project requires Python 3.11+ and is managed using uv.
- Create a virtual environment:
uv init - Install the package:
uv add browser-use uv sync - Install the Chromium browser:
uvx browser-use install
Sources: README.md:30-40, pyproject.toml:6
Configuration
An API key is required for the LLM provider. The library can be configured by creating a .env file in the project root.
# .env
BROWSER_USE_API_KEY=your-key
New users can get free credits for the ChatBrowserUse model from the Browser Use Cloud.
Sources: README.md:35-38
Quickstart Example
The following is a minimal example to run an agent.
from browser_use import Agent, Browser, ChatBrowserUse
import asyncio
async def example():
browser = Browser(
# use_cloud=True, # Uncomment to use a stealth browser on Browser Use Cloud
)
llm = ChatBrowserUse()
agent = Agent(
task="Find the number of stars of the browser-use repo",
llm=llm,
browser=browser,
)
history = await agent.run()
return history
if __name__ == "__main__":
history = asyncio.run(example())
Sources: README.md:42-61
Command-Line Interface (CLI)
The project includes a powerful CLI, browser-use, for running agents and managing the environment. It is built using click and textual.
Sources: browser_use/cli.py:163-165
Modes of Operation
- Interactive TUI Mode: The default mode, launched by running
uvx browser-use. It provides a Textual-based terminal user interface for entering tasks and viewing logs. - One-Shot Prompt Mode: Executes a single task non-interactively using the
--prompt(or-p) flag. - MCP Server Mode: Runs as a Model-Context-Protocol server via the
--mcpflag, allowing integration with clients like Claude Desktop. - Template Generation (
init): Theinitcommand generates starter templates. It can be run interactively or by specifying a template name. Templates are fetched at runtime from a GitHub repository. - Browser Installation (
install): Theinstallcommand downloads and sets up the required Chromium browser and its dependencies. - Authentication (
auth): Theauthcommand authenticates the user with Browser Use Cloud to sync runs.
Sources: browser_use/cli.py:1169-1268, browser_use/init_cmd.py:221-231
Key CLI Options
| Option | Description | Source File |
|---|---|---|
--prompt <str> | Run a single task without the TUI. | browser_use/cli.py:1180 |
--mcp | Run as an MCP server. | browser_use/cli.py:1181 |
--model <str> | Specify the LLM to use (e.g., gpt-4o-mini). | browser_use/cli.py:1170 |
--headless | Run the browser in headless mode. | browser_use/cli.py:1172 |
--user-data-dir <path> | Path to a Chrome user data directory for persistent sessions. | browser_use/cli.py:1175 |
--cdp-url <url> | Connect to an existing Chrome instance via its CDP URL. | browser_use/cli.py:1177 |
--proxy-url <url> | Use a proxy server for browser traffic. | browser_use/cli.py:1178 |
Sources: browser_use/cli.py:1168-1182
Agent Operation
The AI agent operates in an iterative loop, receiving state information, reasoning about it, and selecting actions to perform.
This diagram shows the agent's decision-making loop.
Agent Input
At each step, the agent receives a comprehensive state snapshot:
<agent_history>: A log of previous actions and their results.<agent_state>: The user request, file system summary, and step info.<browser_state>: The current URL, open tabs, and a list of indexed interactive elements.<browser_vision>: A screenshot of the page with bounding boxes around interactive elements.<read_state>: One-time data from a previousextractorread_fileaction.
Sources: browser_use/agent/system_prompts/system_prompt.md:12-20
Agent Output
The agent must respond with a JSON object containing its reasoning and the next action(s). The structure varies slightly based on the model, but generally includes:
evaluation_previous_goal: An assessment of the last action's success or failure.memory: A brief summary of progress to be carried forward.next_goal: The immediate objective for the next step.action: A list of one or more actions to execute.
For some models, a thinking block is used for explicit reasoning.
Sources: browser_use/agent/system_prompts/system_prompt.md:248-256, browser_use/agent/system_prompts/system_prompt_no_thinking.md:231-239
Agent Tools
The Tools service (browser_use/tools/service.py) defines the set of actions an agent can perform. Custom tools can also be registered.
| Action | Description |
|---|---|
navigate | Navigates to a specified URL. |
click | Clicks on an element by its index or coordinates. |
input | Types text into an input field. |
scroll | Scrolls the page up or down. |
extract | Uses an LLM to extract structured data from the page content. |
done | Terminates the task and reports the outcome. |
write_file | Writes content to the agent's local file system. |
read_file | Reads content from the agent's local file system. |
Sources: browser_use/tools/service.py:102, 180, 267, 497, 577, 700, 742, 762
Code-Use Mode
An alternative mode allows the agent to write and execute Python code in a notebook-like environment to control the browser. This provides greater flexibility for complex logic, data manipulation, and looping. The agent has access to functions like await navigate(...), await click(...), and await evaluate(...) to execute JavaScript.
Sources: browser_use/code_use/system_prompt.md:3-6, 58-76
File System
The agent has access to a persistent file system, managed by the FileSystem class, for long-running tasks.
- It is initialized with a
todo.mdfile, which the agent uses as a checklist for subtasks. - The agent can create, read, and write files (
.txt,.md,.json,.csv,.pdf). - Downloaded files (e.g., PDFs) are automatically saved and their paths are made available to the agent.
Sources: browser_use/agent/system_prompts/system_prompt.md:112-122, browser_use/filesystem/file_system.py:16-19, 120-123
Cloud Integration
For enhanced scalability, performance, and stealth, browser-use offers a cloud-based service.
- Cloud Browsers: Users can run agents on stealth browsers hosted in the cloud by setting
use_cloud=Truein theBrowserconstructor. This helps avoid bot detection and CAPTCHAs. - Cloud API: A REST API is available for running tasks, offering features like proxy usage, structured output, and a dedicated Search API.
- Run Syncing: The
browser-use authCLI command allows users to authenticate and sync their local runs to the Browser Use Cloud for monitoring and review.
Sources: README.md:24, 212-218, examples/cloud/README.md:3-5, 29-33, browser_use/cli.py:1257-1259