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

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# OpenAI Instrumentation

> Instrument OpenAI API calls with the LangWatch Python SDK to capture traces, debug, and support AI agent testing workflows.

> [!TIP]
>
> **Quick setup?** Instead of following these steps manually, [copy a prompt](https://langwatch.ai/docs/skills/directory.md) into your coding agent and it will set this up for you automatically.

LangWatch integrates with OpenAI to automatically capture detailed information about your LLM calls.

## Installation

```bash pip theme={null}
pip install langwatch openai
```

```bash uv theme={null}
uv add langwatch openai
```

## Usage

> [!NOTE]
>
> The LangWatch API key is configured by default via the `LANGWATCH_API_KEY` environment variable.

Use `autotrack_openai_calls()` to automatically capture all OpenAI calls made with a specific client instance within a trace.

```python theme={null}
import langwatch
from openai import OpenAI

langwatch.setup()
client = OpenAI()


@langwatch.trace(name="OpenAI Chat Completion")
def get_openai_chat_response(user_prompt: str):
    langwatch.get_current_trace().autotrack_openai_calls(client)

    response = client.chat.completions.create(
        model="gpt-6-luna",
        messages=[{"role": "user", "content": user_prompt}],
    )
    completion = response.choices[0].message.content
    return completion


if __name__ == "__main__":
    user_query = "Tell me a joke"
    response = get_openai_chat_response(user_query)

    print(f"User: {user_query}")
    print(f"AI: {response}")
```

The `@langwatch.trace()` decorator creates a parent trace, and `autotrack_openai_calls()` enables automatic tracking of all calls made with the specified client instance for the duration of that trace.

## Check it worked

Run the example. Then open your project in LangWatch: a new trace named "OpenAI Chat Completion" appears in the traces list within a few seconds. Open it to see the OpenAI chat completion nested inside it, with its input and output.

If no trace appears, check that `LANGWATCH_API_KEY` is set in the environment the script runs in.

## Related

* [Capturing RAG](https://langwatch.ai/docs/integration/python/tutorials/capturing-rag.md) - Learn how to capture RAG data from retrievers and tools
* [Capturing Metadata and Attributes](https://langwatch.ai/docs/integration/python/tutorials/capturing-metadata.md) - Add custom metadata and attributes to your traces and spans
* [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails.md) - Log evaluations and implement guardrails in your OpenAI applications


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