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

# AG2

> Trace AG2 0.14 agent chats and tool execution with OpenInference in Arize AX.

[AG2](https://github.com/ag2ai/ag2) is an agent framework built on the
`autogen` API. The
[`AG2Instrumentor`](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-ag2)
captures ConversableAgent chats, replies, and tool execution as OpenInference
AGENT and TOOL spans in Arize AX.

## Prerequisites

* Python 3.10+
* An Arize AX account ([sign up](https://arize.com/sign-up/))

This guide uses AG2's offline multi-agent and tool-call pattern, so it does not
need an LLM provider key.

## Launch Arize AX

1. Sign in to your [Arize AX account](https://app.arize.com/).
2. From **Space Settings**, copy your **Space ID** and **API Key**. You will set them as `ARIZE_SPACE_ID` and `ARIZE_API_KEY` below.

## Install

```bash theme={null}
pip install arize-otel openinference-instrumentation-ag2 "ag2<1.0"
```

## Configure credentials

```bash theme={null}
export ARIZE_SPACE_ID="<your-space-id>"
export ARIZE_API_KEY="<your-api-key>"
export ARIZE_PROJECT_NAME="ag2-tracing-example"
```

## Setup tracing

```python theme={null}
# instrumentation.py
import os

from arize.otel import register
from openinference.instrumentation.ag2 import AG2Instrumentor

tracer_provider = register(
    space_id=os.environ["ARIZE_SPACE_ID"],
    api_key=os.environ["ARIZE_API_KEY"],
    project_name=os.environ["ARIZE_PROJECT_NAME"],
)

AG2Instrumentor().instrument(tracer_provider=tracer_provider)
print("Arize AX tracing initialized for AG2.")
```

AG2 instrumentation supports the AG2 0.14.x `autogen` API. AG2 v1 uses a
different middleware API and is not yet supported.

## Run AG2

```python theme={null}
# example.py
import json
from typing import Any

from instrumentation import tracer_provider
from autogen import ConversableAgent


def get_weather(city: str) -> str:
    return f"It is 72F and sunny in {city}."


def reply_with_weather(
    agent: ConversableAgent,
    messages: list[dict[str, Any]] | None = None,
    sender: Any = None,
    config: Any = None,
) -> tuple[bool, str]:
    _, result = agent.execute_function(
        {"name": "get_weather", "arguments": json.dumps({"city": "Portland"})},
        call_id="call-1",
    )
    return True, str(result["content"])


weather_agent = ConversableAgent(
    "weather_agent",
    llm_config=False,
    human_input_mode="NEVER",
)
weather_agent.register_function({"get_weather": get_weather})
weather_agent.register_reply(
    [ConversableAgent, None],
    reply_with_weather,
    position=0,
)

user_proxy = ConversableAgent(
    "user_proxy",
    llm_config=False,
    human_input_mode="NEVER",
)
chat = user_proxy.initiate_chat(
    weather_agent,
    message="What is the weather in Portland?",
    max_turns=1,
    silent=True,
)
print("weather_agent:", chat.chat_history[-1]["content"])
```

### Expected output

```text wrap theme={null}
Arize AX tracing initialized for AG2.
weather_agent: It is 72F and sunny in Portland.
```

## Verify in Arize AX

1. Open your Arize AX space and select project **`ag2-tracing-example`**.
2. You should see a new trace within \~30 seconds with AGENT spans for the chat and reply, plus a TOOL span named `get_weather`.
3. If no traces appear, see [Troubleshooting](#troubleshooting).

### Check from the skill, CLI, or SDK

Confirm spans are actually reaching your Arize AX project. Use whichever fits your workflow — the skill and CLI work for any framework; the SDK check is shown for each language.

<Tabs>
  <Tab title="Arize skill (agent)">
    Install the [Arize Skills](https://github.com/Arize-ai/arize-skills) plugin and let your coding agent check for you:

    ```bash theme={null}
    npx skills add Arize-ai/arize-skills
    ```

    Then prompt your agent:

    > Use the `arize-trace` skill to export and analyze recent traces from my project. Confirm spans are arriving, and summarize any errors or latency issues.
  </Tab>

  <Tab title="AX CLI">
    Export recent spans for your project — any rows mean traces are landing:

    ```bash theme={null}
    ax spans export "$ARIZE_PROJECT_NAME" --space "$ARIZE_SPACE_ID" \
      --limit 5 --stdout | jq 'length'
    ```

    A non-zero count confirms spans reached Arize AX. Run `ax auth login` first if you have not authenticated. See the [`ax spans` reference](/docs/api-clients/cli/spans).
  </Tab>

  <Tab title="SDK">
    Query the project's spans and check that at least one came back.

    <CodeGroup>
      ```python Python theme={null}
      import os
      from arize import ArizeClient

      client = ArizeClient(api_key=os.environ["ARIZE_API_KEY"])
      resp = client.spans.list(
          project=os.environ["ARIZE_PROJECT_NAME"],
          space=os.environ["ARIZE_SPACE_ID"],
          limit=5,
      )
      count = len(resp.spans)
      print(
          f"{count} span(s) found" if count else "No spans yet — recheck setup"
      )
      ```

      ```typescript TypeScript theme={null}
      // Reads ARIZE_API_KEY from the environment.
      import { listSpans } from "@arizeai/ax-client";

      const { data: spans } = await listSpans({
        project: process.env.ARIZE_PROJECT_NAME!,
        space: process.env.ARIZE_SPACE_ID!,
        limit: 5,
      });
      const count = spans.length;
      console.log(
        count ? `${count} span(s) found` : "No spans yet — recheck setup",
      );
      ```

      ```go Go theme={null}
      client, err := arize.NewClient(
          arize.Config{APIKey: os.Getenv("ARIZE_API_KEY")},
      )
      if err != nil {
          log.Fatal(err)
      }
      resp, err := client.Spans.List(ctx, spans.ListRequest{
          Project: os.Getenv("ARIZE_PROJECT_NAME"),
          Space:   os.Getenv("ARIZE_SPACE_ID"),
          Limit:   5,
      })
      if err != nil {
          log.Fatal(err)
      }
      fmt.Printf("%d span(s) found\n", len(resp.Spans))
      ```
    </CodeGroup>

    SDK span references: [Python](/docs/api-clients/python/version-8/client-resources/spans) · [TypeScript](/docs/api-clients/typescript/version-1/client-resources/spans) · [Go](/docs/api-clients/go/version-2/client-resources/spans).
  </Tab>
</Tabs>

## Troubleshooting

* **No traces in Arize AX.** Call `AG2Instrumentor().instrument(...)` before starting a chat.
* **Import or instrumentation error.** Install an AG2 0.14.x release; AG2 v1 is not supported by this instrumentor.
* **The example asks for an LLM key.** Keep both agents' `llm_config=False`; this offline example runs a local tool instead of calling a model.

## Resources

<CardGroup>
  <Card icon="terminal" href="https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-ag2" title="OpenInference AG2 Instrumentor" horizontal />

  <Card icon="github" href="https://github.com/ag2ai/ag2" title="AG2 repository" horizontal />
</CardGroup>
