# Adding Browsing Capabilities to AG2

## Introduction
Previously, in our [Cross-Framework LLM Tool Integration](https://docs.ag2.ai/0.9.10/docs/blog/2024/12/20/Tools-interoperability) guide, we combined tools from frameworks like **LangChain**, **CrewAI**, and **PydanticAI** to enhance AG2.

Now, we’re taking AG2 even further by integrating [`Browser Use`](https://github.com/browser-use/browser-use) and [`Crawl4AI`](https://github.com/unclecode/crawl4ai), enabling agents to navigate websites, extract dynamic content, and interact with web pages. This unlocks new possibilities for automated data collection, web automation, and more.

## `Browser Use` Integration

### Installation
[`Browser Use`](https://github.com/browser-use/browser-use) requires **Python 3.11 or higher**.

To get started with the `Browser Use` integration in AG2, follow these steps:

1. Install AG2 with the `browser-use` extra:

```
    pip install ag2[openai,browser-use]
    ```

If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:

```
    pip install -U autogen[openai,browser-use]
    ```

or

```
    pip install -U ag2[openai,browser-use]
    ```

as `autogen` and `ag2` are aliases for the same PyPI package.
2. Set up Playwright:

```
    # Installs Playwright and browsers for all OS
    playwright install
    # Additional command, mandatory for Linux only
    playwright install-deps
    ```

3. For running the code in Jupyter, use `nest_asyncio` to allow nested event loops.

```
    pip install nest_asyncio
    ```

You're all set! Now you can start using browsing features in AG2.

### Imports

```python
import os
import nest_asyncio

from autogen import AssistantAgent, UserProxyAgent, LLMConfig
from autogen.tools.experimental import BrowserUseTool

nest_asyncio.apply()
```

### Agent Configuration
Configure the agents for the interaction.

- `config_list` defines the LLM configurations, including the model and API key.
- [`UserProxyAgent`](https://docs.ag2.ai/0.9.10/docs/api-reference/autogen/UserProxyAgent) simulates user inputs without requiring actual human interaction (set to `NEVER`).
- [`AssistantAgent`](https://docs.ag2.ai/0.9.10/docs/api-reference/autogen/AssistantAgent) represents the AI agent, configured with the LLM settings.

[`Browser Use`](https://github.com/browser-use/browser-use) supports the following models: [Supported Models](https://docs.browser-use.com/customize/supported-models#supported-models)

We had great experience with `OpenAI`, `Anthropic`, and `Gemini`. However, `DeepSeek` and `Ollama` haven't performed as well.

```python
llm_config = LLMConfig(config_list={
    "api_type": "openai",
    "model": "gpt-5-nano",
    "api_key": os.environ["OPENAI_API_KEY"],
})

user_proxy = UserProxyAgent(name="user_proxy", human_input_mode="NEVER")
assistant = AssistantAgent(
    name="assistant",
    llm_config=llm_config,
)
```

### Web Browsing with Browser Use
The [`BrowserUseTool`](https://docs.ag2.ai/0.9.10/docs/api-reference/autogen/tools/experimental/BrowserUseTool) allows agents to interact with web pages—navigating, searching, and extracting information.

To see the agent's activity in real-time, set `headless` to `False` in the `browser_config`. If `True`, the browser runs in the background.

```python
browser_use_tool = BrowserUseTool(
    llm_config=llm_config,
    browser_config={"headless": False},
)

browser_use_tool.register_for_execution(user_proxy)
browser_use_tool.register_for_llm(assistant)
```

### Initiate Chat
Now, let’s run a task where the assistant searches Reddit for “AG2,” clicks the first post, and retrieves the first comment.

```python
result = user_proxy.initiate_chat(
    recipient=assistant,
    message="Go to Reddit, search for 'ag2' in the search bar, click on the first post and return the first comment.",
    max_turns=2,
)
```

### `Crawl4AI` Integration
The integration process follows a straightforward approach.

### Installation
To integrate `Crawl4AI` with AG2, follow these steps:

1. Install AG2 with the `crawl4ai` extra:

```
    pip install ag2[openai,crawl4ai]
    ```

If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:

```
    pip install -U autogen[openai,crawl4ai]
    ```

or

```
    pip install -U ag2[openai,crawl4ai]
    ```

as `autogen` and `ag2` are aliases for the same PyPI package.
2. Set up Playwright:

```
    # Installs Playwright and browsers for all OS
    playwright install
    # Additional command, mandatory for Linux only
    playwright install-deps
    ```

3. For running the code in Jupyter, use `nest_asyncio` to allow nested event loops.

```
    pip install nest_asyncio
    ```

Once installed, you're ready to start using the browsing features in AG2.

### Imports

```python
import os
import nest_asyncio
from pydantic import BaseModel

from autogen import AssistantAgent, UserProxyAgent, LLMConfig
from autogen.tools.experimental import Crawl4AITool

nest_asyncio.apply()
```

### Agent Configuration
Configure the agents for the interaction.

[`Crawl4AI`](https://github.com/unclecode/crawl4ai) is built on top of [LiteLLM](https://github.com/BerriAI/litellm) and supports the same models as LiteLLM.

We had great experience with `OpenAI`, `Anthropic`, `Gemini` and `Ollama`. However, as of this writing, `DeepSeek` is encountering some issues.

```python
llm_config = LLMConfig(config_list={
    "api_type": "openai",
    "model": "gpt-4o-mini",
    "api_key": os.environ["OPENAI_API_KEY"],
})

user_proxy = UserProxyAgent(name="user_proxy", human_input_mode="NEVER")

assistant = AssistantAgent(name="assistant", llm_config=llm_config)
```

### Web Browsing with `Crawl4AI`
[`Crawl4AITool`](https://docs.ag2.ai/0.9.10/docs/api-reference/autogen/tools/experimental/Crawl4AITool) offers three integration modes:
1. **Basic Web Scraping (No LLM):**  
   ```python
   crawlai_tool = Crawl4AITool()
   ```
2. **Web Scraping with LLM Processing:**  
   ```python
   crawlai_tool = Crawl4AITool(llm_config=llm_config)
   ```
3. **LLM Processing with Schema for Structured Data:**  
   ```python
   class Blog(BaseModel):
       title: str
       url: str

crawlai_tool = Crawl4AITool(llm_config=llm_config, extraction_model=Blog)
   ```

We'll proceed with the most advanced option: **LLM Processing with Structured Data Schema.**

After creating the tool instance, register the agents:

```python
crawlai_tool.register_for_execution(user_proxy)
crawlai_tool.register_for_llm(assistant)
```

### Initiate Chat
```python
message = "Extract all blog posts from https://docs.ag2.ai/docs/blog"
result = user_proxy.initiate_chat(
    recipient=assistant,
    message=message,
    max_turns=2,
)
```

## Conclusion
With `Browser Use` and `Crawl4AI`, AG2 gets a serious upgrade for web browsing and data extraction. `Browser Use` makes it easy to navigate and interact with web pages, while `Crawl4AI` helps scrape and structure data, with or without AI processing.

Whether you need quick web access, detailed data extraction, or AI-powered insights, these tools make it simple to integrate real-time web content into your projects. Now you're all set to build smarter, more automated agents that can browse, extract, and use web data with ease. 🚀
