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

# Browser Use

> Capture Browser Use 1.0 agent runs through Pydantic AI's OpenTelemetry spans

## Choose a setup method

| Method | Use it when |
| - | - |
| **Agnost AI skill** | You want your coding agent to inspect the app, make the change, and verify it. |
| **Agnost AI SDK** | You want explicit control over the interaction boundaries and fields. |
| **OpenTelemetry** | The framework already emits useful spans or you operate an OTel pipeline. |

Start with one method for a call path. Combining SDK tracking with framework
OpenTelemetry on the same call can create duplicate interactions.

## Recommended: Agnost AI skill

> [!NOTE]
>
> **Recommended.** Choose this path when you want your coding agent to inspect the
> project, select a supported transport, make the smallest instrumentation change,
> send a test event, and verify that it reached Agnost AI.

Install the skill once in your coding-agent environment:

```bash theme={null}
npx skills add AgnostAI/skills --skill agnost-ai
```

Run the framework-specific prompt below from the application root. Review the
resulting diff before deploying it.

```text theme={null}
Use the agnost-ai skill to add Agnost AI analytics to this Browser Use application.
Org ID: your-org-id
Instrument the real Agent.run() path and verify one fresh interaction.
```

## Manual setup: Agnost AI SDK

```bash theme={null}
pip install agnost
```

```python theme={null}
import agnost
from browser_use import Agent, Browser, ChatAnthropic

agnost.init("your-org-id")


async def run_task(task: str) -> str:
    interaction = agnost.begin(
        user_id="u-42",
        agent_name="browser-use-agent",
        input=task,
        conversation_id="sess-123",  # groups turns into one conversation
    )
    if interaction is None:
        raise RuntimeError("Agnost is not initialized. Call agnost.init() first.")
    try:
        agent = Agent(task, llm=ChatAnthropic("claude-haiku-4-5"), browser=Browser("chromium"))
        result = await agent.run()
        interaction.end(output=result.final_result() or result.error or "", success=result.is_done())
        return result.final_result() or ""
    except Exception as exc:
        interaction.end(output=str(exc), success=False)
        raise
```

`agent.run()` does not raise for model or provider failures. It returns a result with
`status="error"`, so `result.is_done()` is what marks the interaction as failed.

Call `agnost.shutdown()` once when the application exits, not after each request.
Shutting down per request disables telemetry for later agent runs in the same process.

## Manual setup: OpenTelemetry

Browser Use 1.0 runs on Pydantic AI, and its documented tracing route is Pydantic AI's
OpenTelemetry instrumentation. Each run emits an `invoke_agent` span, a `chat <model>` span
per model request, and an `execute_tool python` span for every Python cell the agent runs.
These spans use the OTel GenAI conventions, so Agnost AI ingests them as they are.

### Install

```bash theme={null}
pip install browser-use opentelemetry-sdk opentelemetry-exporter-otlp-proto-http \
            opentelemetry-processor-baggage
```

### Setup

```python theme={null}
import asyncio

from opentelemetry import baggage, context
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.processor.baggage import ALLOW_ALL_BAGGAGE_KEYS, BaggageSpanProcessor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from pydantic_ai import Agent as PydanticAgent
from pydantic_ai.models.instrumented import InstrumentationSettings

from browser_use import Agent, Browser, ChatAnthropic

provider = TracerProvider()
provider.add_span_processor(BaggageSpanProcessor(ALLOW_ALL_BAGGAGE_KEYS))
provider.add_span_processor(
    BatchSpanProcessor(
        OTLPSpanExporter(
            endpoint="https://otel.agnost.ai/v1/traces",
            headers={"X-Agnost-Org-ID": "<your-org-id>"},
        )
    )
)
PydanticAgent.instrument_all(InstrumentationSettings(tracer_provider=provider))


async def run_task(task: str, session_id: str, user_id: str) -> str | None:
    ctx = baggage.set_baggage("agnost.session_id", session_id)
    ctx = baggage.set_baggage("agnost.user_id", user_id, context=ctx)
    token = context.attach(ctx)
    try:
        agent = Agent(task, llm=ChatAnthropic("claude-haiku-4-5"), browser=Browser("chromium"))
        result = await agent.run()
        return result.final_result()
    finally:
        context.detach(token)


print(asyncio.run(run_task("What is the title of https://example.com?", "sess-123", "u-42")))
provider.shutdown()
```

Pydantic AI writes a fresh `gen_ai.conversation.id` on every run, and Browser Use does not
let you pass your own. `BaggageSpanProcessor` copies the `agnost.session_id` and
`agnost.user_id` baggage onto every span started inside `run_task`, and Agnost AI reads
`agnost.session_id` before `gen_ai.conversation.id`. That is how runs for the same user
session land in one conversation.

`instrument_all()` must run before the first `Agent` is created. Keep the default
`Browser()` (Browser Use Cloud) if you use it; `Browser("chromium")` above runs a local browser.

### What appears in Agnost AI

* **Conversations** grouped by `agnost.session_id`.
* **User-level analytics** grouped by `agnost.user_id`.
* **Events** for the agent run (`agent`), each model request (`chat`), and each tool call
  (`python` for the cells the agent writes, plus Pydantic AI's output tool).
* The task as input and the final answer as output on the agent event. The `python` tool
  events carry the code the model wrote and what the cell printed.

### Verify

Run one task, then open **Events** in Agnost AI. Confirm the conversation shows your
session ID and user ID, an `agent` event with the task and the final answer, and a `python`
tool event for each cell.

### Troubleshooting

* No events: confirm `instrument_all()` runs before any `Agent` is created and that the
  exporter has `X-Agnost-Org-ID`. Call `provider.shutdown()` (or `force_flush()`) before a
  short script exits.
* Every run is its own conversation: the baggage was not attached around `agent.run()`.
  Attach it before the run starts, in the same task, as shown above.
* One extra `chat` event per run: that is Browser Use's judge, which grades the run with the
  same model. Pass `Agent(..., judge=False)` to turn it off.
* Spans carry the task, the code the model writes, and page text it reads. Pass
  `InstrumentationSettings(tracer_provider=provider, include_content=False)` to keep message
  content out of the spans.
* `ANONYMIZED_TELEMETRY` controls Browser Use's own usage telemetry. It does not affect
  these traces.
* Browser Use 0.x hooks (`on_step_start`, `on_step_end`) and the Laminar and OpenLIT
  integrations are gone in 1.0. Use the Pydantic AI spans above.

### References

* [Browser Use observability](https://docs.browser-use.com/open-source/observability)
* [Pydantic AI OpenTelemetry without Logfire](https://pydantic.dev/docs/ai/integrations/logfire/)
* [OpenTelemetry baggage span processor](https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/processor/opentelemetry-processor-baggage)

## Next steps

* [Conversations](https://docs.agnost.ai/using-conversations.md): review the complete Browser Use run.
* [Events](https://docs.agnost.ai/using-events.md): inspect model requests and the Python cells the agent ran.
* [Patterns](https://docs.agnost.ai/using-patterns.md): organize production conversations by what users wanted.


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