> ## Documentation Index
> Fetch the complete documentation index at: https://docs.microsandbox.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Run a browser agent

> Run an AI browser agent with isolated Chromium inside a microVM

[Browser Use](https://github.com/browser-use/browser-use) turns a task into browser actions. This guide runs both the agent and Chromium inside one isolated microsandbox.

> [!NOTE]
>
> The browser image is large. Its first pull and Python package install can take a few minutes. Use a [snapshot](https://docs.microsandbox.dev/how-to/sandboxing/warm-workers.md) to avoid repeating that setup.

## Run Browser Use

**1. Create the agent**

Save this as `browser-use-task.py`:

```python Python theme={null}
import asyncio
import os

from browser_use import Agent, BrowserProfile, BrowserSession, ChatBrowserUse


async def main() -> None:
    browser = BrowserSession(
        browser_profile=BrowserProfile(
            headless=True,
            executable_path=os.environ["CHROME_PATH"],
            args=["--no-sandbox", "--disable-dev-shm-usage"],
            enable_default_extensions=False,
            user_data_dir=None,
        )
    )
    agent = Agent(
        task="Return the title of https://example.com",
        llm=ChatBrowserUse(model="bu-2-0"),
        browser_session=browser,
    )
    history = await agent.run(max_steps=3)
    print(history.final_result())
    await browser.kill()


asyncio.run(main())
```

**2. Start the sandbox**

Save [msb-trust.sh](https://github.com/superradcompany/microsandbox/blob/main/scripts/msb-trust/msb-trust.sh)
beside `browser-use-task.py`. Export a [Browser Use API key](https://cloud.browser-use.com/new-api-key), then run the task. The secret stays on the host and can only be substituted into requests to Browser Use's model endpoint.

```sh macOS & Linux theme={null}
export BROWSER_USE_API_KEY="your-key"

msb run --name browser-use-task --replace \
  --cpus 2 --memory 4G --root-disk 10G \
  --secret "BROWSER_USE_API_KEY@llm.api.browser-use.com" \
  --copy-file ./browser-use-task.py:/root/browser-use-task.py \
  --copy-file ./msb-trust.sh:/root/msb-trust.sh \
  mcr.microsoft.com/playwright/python:v1.61.0-noble -- sh -lc '
    apt-get update -qq && apt-get install -y -qq libnss3-tools &&
    sh /root/msb-trust.sh chrome &&
    pip install --disable-pip-version-check browser-use==0.13.10 &&
    CHROME_PATH="$(find /ms-playwright -path "*/chrome-linux/chrome" -type f | head -n 1)" \
      python /root/browser-use-task.py
  '
```

```powershell Windows theme={null}
$env:BROWSER_USE_API_KEY = "your-key"

msb run --name browser-use-task --replace `
  --cpus 2 --memory 4G --root-disk 10G `
  --secret "BROWSER_USE_API_KEY@llm.api.browser-use.com" `
  --copy-file ./browser-use-task.py:/root/browser-use-task.py `
  --copy-file ./msb-trust.sh:/root/msb-trust.sh `
  mcr.microsoft.com/playwright/python:v1.61.0-noble -- sh -lc '
    apt-get update -qq && apt-get install -y -qq libnss3-tools &&
    sh /root/msb-trust.sh chrome &&
    pip install --disable-pip-version-check browser-use==0.13.10 &&
    CHROME_PATH="$(find /ms-playwright -path "*/chrome-linux/chrome" -type f | head -n 1)" \
      python /root/browser-use-task.py
  '
```

The agent prints its final result and exits. The Browser Use browser session uses the Chromium already included in the image, so it does not download a second browser.

The script configures Chromium's certificate trust for TLS interception used by
`--secret`. See [Trusting certificates in applications](https://docs.microsandbox.dev/networking/tls.md#application-trust).

**3. Clean up**

Remove the stopped sandbox and unset the API key:

```sh macOS & Linux theme={null}
msb rm -f browser-use-task
unset BROWSER_USE_API_KEY
```

```powershell Windows theme={null}
msb rm -f browser-use-task
Remove-Item Env:BROWSER_USE_API_KEY
```

## Reference

* [Browser Use Python quickstart](https://docs.browser-use.com/open-source/introduction)
* [Microsandbox secrets](https://docs.microsandbox.dev/sandboxes/secrets.md)
* [Microsandbox snapshots](https://docs.microsandbox.dev/how-to/sandboxing/warm-workers.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.