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

# xAI Grok Tracing

> Trace xAI Grok calls in Phoenix with OpenInference auto-instrumentation for the OpenAI Python SDK, since xAI provides an OpenAI-compatible API endpoint.

[xAI](https://x.ai/api) provides Grok models through an API that is compatible with the OpenAI SDK. Set the OpenAI client's `base_url` to `https://api.x.ai/v1` and use [`openinference-instrumentation-openai`](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai) to send Grok traces to Phoenix.

> [!NOTE]
>
> This page instruments OpenAI SDK calls to xAI's API. It is not a separate xAI-specific OpenInference instrumentor.

## Install

```bash theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
pip install arize-phoenix-otel openinference-instrumentation-openai openai
```

## Setup

Add your xAI API key as an environment variable:

```bash theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export XAI_API_KEY="<your-xai-api-key>"
```

Connect your application to Phoenix with the `register` function:

from phoenix.otel import register

&#x20; \# configure the Phoenix tracer
&#x20; tracer\_provider = register(
&#x20; project\_name="my-llm-app", # Default is 'default'
&#x20; auto\_instrument=True # Auto-instrument your app based on installed OI dependencies
&#x20; )

## Run xAI Grok

```python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.x.ai/v1",
    api_key=os.environ["XAI_API_KEY"],
)

response = client.chat.completions.create(
    model="grok-4.6",
    messages=[
        {
            "role": "user",
            "content": "What can traces reveal about a failed AI agent run?",
        },
    ],
)

print(response.choices[0].message.content)
```

## Observe

Now that you have tracing set up, all invocations of the OpenAI client pointed at xAI Grok will be streamed to your running Phoenix for observability and evaluation. Each call appears as a `ChatCompletion` LLM span with the prompt, response, model name, token usage, and latency.

## Common Issues

* **Spans missing**: `register(..., auto_instrument=True)` must run before your application sends requests, and `openinference-instrumentation-openai` must be installed in the same environment
* **`401` from xAI**: Use your xAI API key, not an OpenAI key
* **Model not found**: Replace `grok-4.6` with a Grok model available in your xAI account
* **Base URL**: Ensure you're using `https://api.x.ai/v1` as the base URL

## Resources

> **[xAI Quickstart](https://docs.x.ai/developers/quickstart)**
>
> xAI API docs

> **[OpenInference OpenAI Instrumentation](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai)**
>
> OpenAI instrumentation package
