# andersonamaral2/browser-use architecture

> Architecture diagram of the GitHub repository andersonamaral2/browser-use, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/andersonamaral2/browser-use
- Repository: https://github.com/andersonamaral2/browser-use

## Overview

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: [`browser_use/agent/service.py`](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/service.py) (Agent loop), [`browser_use/controller/service.py`](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/controller/service.py) (Controller), [`browser_use/dom/service.py`](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/service.py) (DOM service), [`browser_use/browser/browser.py`](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/browser/browser.py) (Browser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_control["Control plane"]
  node_agent_service["Agent loop<br/>orchestrator<br/>[service.py]"]
  node_agent_prompts["Prompts<br/>prompt templates<br/>[prompts.py]"]
  node_message_mgr["Message manager<br/>context builder<br/>[service.py]"]
  node_controller_service["Controller<br/>action dispatcher<br/>[service.py]"]
  node_controller_registry["Action registry<br/>[service.py]"]
  node_agent_views["Agent views<br/>data models<br/>[views.py]"]
  node_controller_views["Controller views<br/>data models<br/>[views.py]"]
end

subgraph group_perception["Perception plane"]
  node_dom_service["DOM service<br/>page state<br/>[service.py]"]
  node_build_dom_tree["DOM extractor<br/>browser-side extractor<br/>[buildDomTree.js]"]
  node_history_processor["History tree<br/>state compressor<br/>[service.py]"]
  node_dom_views["DOM views<br/>data models<br/>[views.py]"]
end

subgraph group_execution["Execution plane"]
  node_browser_service["Browser<br/>[browser.py]"]
  node_browser_context["Context<br/>[context.py]"]
  node_browser_views["Browser views<br/>data models<br/>[views.py]"]
end

subgraph group_public["Public API"]
  node_pkg_api["Package API<br/>public api<br/>[__init__.py]"]
end

subgraph group_support["Support"]
  node_telemetry["Telemetry<br/>usage reporting<br/>[service.py]"]
  node_logging_config["Logging<br/>[logging_config.py]"]
end

node_pkg_api -->|"exposes"| node_agent_service
node_pkg_api -->|"exposes"| node_browser_service
node_pkg_api -->|"exposes"| node_controller_service
node_agent_service -->|"uses"| node_agent_prompts
node_agent_service -->|"assembles"| node_message_mgr
node_agent_service -->|"executes"| node_controller_service
node_agent_service -->|"observes"| node_dom_service
node_agent_service -->|"drives"| node_browser_context
node_message_mgr -->|"serializes"| node_agent_views
node_dom_service -->|"extracts"| node_build_dom_tree
node_dom_service -->|"compresses"| node_history_processor
node_dom_service -->|"produces"| node_dom_views
node_history_processor -->|"updates"| node_dom_views
node_controller_service -->|"resolves"| node_controller_registry
node_controller_service -->|"acts on"| node_browser_context
node_controller_service -->|"uses"| node_controller_views
node_browser_service -->|"hosts"| node_browser_context
node_browser_service -->|"serializes"| node_browser_views
node_browser_context -->|"provides page"| node_dom_service
node_telemetry -.->|"reports"| node_agent_service
node_telemetry -.->|"reports"| node_controller_service
node_logging_config -.->|"configures"| node_agent_service
node_logging_config -.->|"configures"| node_browser_service

click node_pkg_api "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/__init__.py"
click node_agent_service "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/service.py"
click node_agent_prompts "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/prompts.py"
click node_message_mgr "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/message_manager/service.py"
click node_controller_service "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/controller/service.py"
click node_controller_registry "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/controller/registry/service.py"
click node_telemetry "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/telemetry/service.py"
click node_browser_service "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/browser/browser.py"
click node_browser_context "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/browser/context.py"
click node_dom_service "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/service.py"
click node_build_dom_tree "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/buildDomTree.js"
click node_history_processor "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/history_tree_processor/service.py"
click node_browser_views "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/browser/views.py"
click node_agent_views "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/views.py"
click node_controller_views "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/controller/views.py"
click node_dom_views "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/views.py"
click node_logging_config "https://github.com/andersonamaral2/browser-use/blob/main/browser_use/logging_config.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_agent_service,node_agent_prompts,node_message_mgr,node_controller_service,node_controller_registry,node_agent_views,node_controller_views toneBlue
class node_dom_service,node_build_dom_tree,node_history_processor,node_dom_views toneAmber
class node_browser_service,node_browser_context,node_browser_views toneMint
class node_pkg_api toneRose
class node_telemetry,node_logging_config toneIndigo
```

## Components

### Control plane

Agent orchestration, prompts, action dispatch, and telemetry.

- [Agent loop](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/service.py): orchestrator · Main decision loop that observes state, calls the LLM, and applies actions. · `browser_use/agent/service.py`
- [Prompts](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/prompts.py): prompt templates · LLM prompt construction and task framing. · `browser_use/agent/prompts.py`
- [Message manager](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/message_manager/service.py): context builder · Assembles, truncates, and formats conversation and page state for the model. · `browser_use/agent/message_manager/service.py`
- [Controller](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/controller/service.py): action dispatcher · Resolves model-selected actions into executable Python behavior. · `browser_use/controller/service.py`
- [Action registry](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/controller/registry/service.py): action registry · Maps action names and typed parameters to registered handlers. · `browser_use/controller/registry/service.py`
- [Agent views](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/agent/views.py): data models · Serializable agent-layer models used for state exchange. · `browser_use/agent/views.py`
- [Controller views](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/controller/views.py): data models · 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](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/service.py): page state · Turns live page information into a reduced model-friendly snapshot. · `browser_use/dom/service.py`
- [DOM extractor](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/buildDomTree.js): browser-side extractor · JavaScript helper that builds structured DOM representations in-page. · `browser_use/dom/buildDomTree.js`
- [History tree](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/history_tree_processor/service.py): state compressor · Tracks and compresses DOM/page-state changes over time. · `browser_use/dom/history_tree_processor/service.py`
- [DOM views](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/dom/views.py): data models · 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](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/browser/browser.py): browser wrapper · Browser lifecycle and top-level automation container. · `browser_use/browser/browser.py`
- [Context](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/browser/context.py): tab context · Isolated browser context and tab management boundary. · `browser_use/browser/context.py`
- [Browser views](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/browser/views.py): data models · Serializable browser-layer models shared across services and tests. · `browser_use/browser/views.py`

### Public API

Package entrypoints and user-facing examples.

- [Package API](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/__init__.py): public api · Package-level exports and library surface. · `browser_use/__init__.py`

### Support

Logging, packaging, and test coverage boundaries.

- [Telemetry](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/telemetry/service.py): usage reporting · Anonymous operational reporting and client-side telemetry hooks. · `browser_use/telemetry/service.py`
- [Logging](https://github.com/andersonamaral2/browser-use/blob/main/browser_use/logging_config.py): logging setup · Centralized logging configuration for traces and debugging. · `browser_use/logging_config.py`

## Connections

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