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

# Trace OpenAI SDK with BotGauge

> Trace OpenAI SDK calls with BotGauge in code or with zero-code launch. Verified versions, install line, and a tested chat completions sample.

Records OpenAI SDK LLM calls with model, input and output, and token counts. If your framework calls the OpenAI SDK underneath, this extra traces those LLM calls without the framework's own steps.

| | |
| - | - |
| Extra | `[openai]` |
| Verified framework versions | openai 2.45.0, 2.54.0, 3.33.0, and 3.35.1 |
| Instrumentor installed by the extra | `openinference-instrumentation-openai 0.1.65` |
| Zero-code run | Traced |

Also verified: openai 2.45.0 with instrumentor 0.1.61. The extra no longer installs that older instrumentor. See [supported frameworks](https://www.botgauge.com/docs/integrations/overview.md) for how verification works.

## In code

Call `tracing.init()`, attach the framework instrumentor to the provider it returns, and wrap the work in `tracing.turn()` and `tracing.agent()`.

**1. Install**

```bash terminal theme={null}
pip install "botgauge[openai]" openai==3.35.1
```

**2. Add the code**

```python app.py theme={null}
import os

from botgauge import tracing
from openai import OpenAI
from openinference.instrumentation.openai import OpenAIInstrumentor

provider = tracing.init()
OpenAIInstrumentor().instrument(tracer_provider=provider)

client = OpenAI()

with tracing.turn(session_id="ses_openai"), tracing.agent("openai-agent"):
    reply = client.chat.completions.create(
        model=os.environ["OPENAI_MODEL"],
        messages=[{"role": "user", "content": "In one sentence, what is OpenTelemetry?"}],
    )
print(reply.choices[0].message.content)

tracing.shutdown()
```

**3. Run**

```bash terminal theme={null}
python app.py
```

Needs `BOTGAUGE_API_KEY`, `OPENAI_API_KEY`, `OPENAI_MODEL`. Set `OPENAI_BASE_URL` too if your LLM isn't api.openai.com.

## Zero-code

Leave your app unchanged and start it with `botgauge instrument`. The sample still wraps the work in `tracing.turn()` and `tracing.agent()` so spans carry session and agent ids.

**1. Install**

```bash terminal theme={null}
pip install "botgauge[openai]" openai==3.35.1
```

**2. Add the code**

```python app.py theme={null}
import os

from botgauge import tracing
from openai import OpenAI

client = OpenAI()

with tracing.turn(session_id="ses_openai"), tracing.agent("openai-agent"):
    reply = client.chat.completions.create(
        model=os.environ["OPENAI_MODEL"],
        messages=[{"role": "user", "content": "In one sentence, what is OpenTelemetry?"}],
    )
print(reply.choices[0].message.content)
```

**3. Run**

```bash terminal theme={null}
botgauge instrument python app.py
```

Needs `BOTGAUGE_API_KEY`, `OPENAI_API_KEY`, `OPENAI_MODEL`. Set `OPENAI_BASE_URL` too if your LLM isn't api.openai.com.

View OpenAI SDK spans liveWatch your OpenAI chat completion calls with token counts appear in [BotGauge](https://cloud.botgauge.com).

[Try BotGauge →](https://cloud.botgauge.com)

## Related

See [versions not verified](https://www.botgauge.com/docs/integrations/versions-not-verified.md) if you run a different version, [capture and redaction](https://www.botgauge.com/docs/tracing/capture-and-redaction.md) to control exported content, and [troubleshooting](https://www.botgauge.com/docs/tracing/troubleshooting.md) if spans are missing.


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