How andersonamaral2/browser-use works
1. What this repository is
- browser-use is a Python library for building AI agents that operate a real browser.
- The core architecture is an agent loop: observe the page, compress the state, ask an LLM what to do, execute browser/actions, then repeat until the task is done.
2. Main runtime boundary
- The public entry point is browser_use.Agent, with Browser / BrowserContext and Controller as the main collaborators.
- Typical flow: user task + LLM + browser context → agent → DOM/page state → LLM decision → browser interaction or custom action → history/result.
3. Agent subsystem
- browser_use/agent/service.py is the orchestration layer for the decision loop.
- browser_use/agent/prompts.py contains the prompt templates that shape how page state and action options are presented to the model.
- browser_use/agent/message_manager/ suggests a separate component for assembling and truncating the conversation/state passed to the LLM.
- tests.py here likely covers the agent loop, planning, retries, and task completion behavior.
4. Browser subsystem
- browser_use/browser/browser.py and context.py wrap browser lifecycle and tab/context management.
- The README makes it clear the implementation is built around Playwright concepts: one browser can host many isolated contexts, which is the intended parallelization boundary.
- Browser tests cover low-level interaction behavior such as screenshots and clicks, so this layer is responsible for the actual page automation primitive set.
5. DOM extraction and page-state compression
- browser_use/dom/buildDomTree.js is the browser-side extraction helper that turns a live page into a structured DOM representation.
- browser_use/dom/service.py and history_tree_processor/ likely convert raw DOM/page data into a compact, LLM-friendly representation and track changes over time.
- This is a key architectural boundary: the agent does not reason over the full browser DOM directly, but over a reduced “page state” snapshot.
- DOM tests focus on extraction and processing correctness, which is critical because the LLM’s actions depend on this representation.
6. Action execution and extensibility
- browser_use/controller/service.py and registry/ implement the action registry that maps LLM-selected actions to executable Python functions.
- The README shows both synchronous and asynchronous custom actions, plus optional typed parameters via Pydantic models and optional browser injection.
- This is the main extension point for integrating side effects such as saving data, asking a human, or calling external systems.
7. Serialization / data contract layers
- The repeated views.py files alongside service.py files indicate a service/view split: services contain behavior, views define the structured data exchanged between layers.
- In architecture terms, this helps separate internal stateful logic from serializable models used by prompts, histories, and tests.
- This pattern appears across agent, browser, controller, DOM, and telemetry, so it is a consistent library design choice.
8. Telemetry and operational concerns
- browser_use/telemetry/ is a separate subsystem for anonymous usage reporting.
- The README says this uses PostHog and can be disabled with ANONYMIZED_TELEMETRY=false.
- logging_config.py centralizes logging behavior, which matters for debugging agent traces and browser interactions.
9. Public API and examples
- browser_use/__init__.py exposes the package-level API.
- The examples/ folder demonstrates the supported usage patterns: file upload, multi-tab workflows, parallel agents, custom output handling, self-correction, vision-based tasks, and trace saving.
- These examples are not just demos; they document the intended integration boundaries for consumers of the library.
10. Testing and validation strategy
- There are package-local tests under browser_use/.../tests and top-level integration-style tests in tests/.
- The top-level suite covers broader behavior such as core functionality, self-registered actions, dropdowns, attaching to Chrome, full-screen behavior, vision, stress, and Mind2Web evaluation data.
- This suggests the repo is validated at both unit and workflow levels, with some tests depending on real browser automation.
11. Packaging and delivery
- The project is a Python 3.11+ package defined by pyproject.toml, with a GitHub Actions publish workflow in .github/workflows/publish.yml.
- The presence of .env.example and README instructions indicates the library expects external LLM API credentials at runtime.
- playwright install is an operational dependency when running browser automation locally or in CI.
12. Overall architecture diagram perspective
- Draw the system as three major planes:
- Control plane: Agent, prompts, message manager, controller, telemetry.
- Execution plane: Browser, browser context, Playwright interactions.
- Perception plane: DOM extraction, history processing, page-state serialization.
- The LLM sits between perception and control, while the controller bridges model decisions to concrete browser or custom application actions.
Main entry points: agent/service.py Agent loop · controller/service.py Controller · dom/service.py DOM service · browser.py Browser
Components 17
Control plane
Agent orchestration, prompts, action dispatch, and telemetry.
- Agent loop · Main decision loop that observes state, calls the LLM, and applies actions.
browser_use/agent/service.py - Prompts · LLM prompt construction and task framing.
browser_use/agent/prompts.py - Message manager · Assembles, truncates, and formats conversation and page state for the model.
browser_use/agent/message_manager/service.py - Controller · Resolves model-selected actions into executable Python behavior.
browser_use/controller/service.py - Action registry · Maps action names and typed parameters to registered handlers.
browser_use/controller/registry/service.py - Agent views · Serializable agent-layer models used for state exchange.
browser_use/agent/views.py - Controller views · Serializable action and registry models for execution contracts.
browser_use/controller/views.py
Perception plane
DOM extraction, state compression, and page-history modeling.
- DOM service · Turns live page information into a reduced model-friendly snapshot.
browser_use/dom/service.py - DOM extractor · JavaScript helper that builds structured DOM representations in-page.
browser_use/dom/buildDomTree.js - History tree · Tracks and compresses DOM/page-state changes over time.
browser_use/dom/history_tree_processor/service.py - DOM views · Structured DOM and page-state models exchanged between perception and control.
browser_use/dom/views.py
Execution plane
Browser lifecycle, contexts, and page interaction primitives.
- Browser · Browser lifecycle and top-level automation container.
browser_use/browser/browser.py - Context · Isolated browser context and tab management boundary.
browser_use/browser/context.py - Browser views · Serializable browser-layer models shared across services and tests.
browser_use/browser/views.py
Public API
Package entrypoints and user-facing examples.
- Package API · Package-level exports and library surface.
browser_use/__init__.py
Connections 23
- Package API → Agent loop: exposes · Package entrypoint exports the main agent surface.
- Package API → Browser: exposes · Package entrypoint exposes browser primitives.
- Package API → Controller: exposes · Package entrypoint exposes controller integration.
- Agent loop → Prompts: uses · Agent loop formats tasks and page state for the LLM.
- Agent loop → Message manager: assembles · Agent loop delegates conversation and context construction.
- Agent loop → Controller: executes · Agent sends chosen actions to the controller.
- Agent loop → DOM service: observes · Agent consumes reduced page-state snapshots.
- Agent loop → Context: drives · Agent interacts through the active browser context.
- Message manager → Agent views: serializes · Message assembly uses structured agent-side views.
- DOM service → DOM extractor: extracts · DOM service relies on the browser-side DOM builder.
- DOM service → History tree: compresses · DOM service condenses state through history processing.
- DOM service → DOM views: produces · Perception layer emits structured DOM/page-state views.
- History tree → DOM views: updates · History processing feeds the compact page-state model.
- Controller → Action registry: resolves · Controller maps action requests to registered handlers.
- Controller → Context: acts on · Controller invokes browser actions through the current context.
- Controller → Controller views: uses · Controller exchanges structured action models.
- Browser → Context: hosts · Browser wrapper creates and manages contexts.
- Browser → Browser views: serializes · Browser layer exposes serializable views.
- Context → DOM service: provides page · Context supplies live page data for perception.
- Telemetry → Agent loop: reports · Operational telemetry observes agent usage.
- Telemetry → Controller: reports · Operational telemetry can observe action execution.
- Logging → Agent loop: configures · Central logging supports agent traces.
- Logging → Browser: configures · Central logging supports browser interaction debugging.