> ## 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 Agents SDK Instrumentation

> Instrument OpenAI Agents with the LangWatch Python SDK to capture traces, run AI agent evaluations, and debug agent testing scenarios.

LangWatch integrates with OpenAI Agents through OpenInference instrumentation to monitor agent execution, LLM calls, and tool usage.

## Installation

```bash pip theme={null}
pip install langwatch openai-agents openinference-instrumentation-openai-agents
```

```bash uv theme={null}
uv add langwatch openai-agents openinference-instrumentation-openai-agents
```

## Usage

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

Use the OpenInference instrumentation for OpenAI Agents by passing `OpenAIAgentsInstrumentor` to `langwatch.setup()`.

```python theme={null}
import langwatch
from agents import Agent, Runner
from openinference.instrumentation.openai_agents import OpenAIAgentsInstrumentor
import os
import asyncio

langwatch.setup(instrumentors=[OpenAIAgentsInstrumentor()])

agent = Agent(name="ExampleAgent", instructions="You are a helpful assistant.")


@langwatch.trace(name="OpenAI Agent Run with OpenInference")
async def run_agent_with_openinference(prompt: str):
    result = await Runner.run(agent, prompt)
    return result.final_output


async def main():
    user_query = "Tell me a joke"
    response = await run_agent_with_openinference(user_query)
    print(f"User: {user_query}")
    print(f"AI: {response}")


if __name__ == "__main__":
    asyncio.run(main())
```

The `OpenAIAgentsInstrumentor` automatically captures agent activities, LLM calls, and tool usage. Use `@langwatch.trace()` to create a parent trace under which agent operations will be nested.

## Check it worked

Run the example. Then open your project in LangWatch: a new trace named "OpenAI Agent Run with OpenInference" appears in the traces list within a few seconds. Open it to see the agent run, its LLM calls and tool calls nested inside it.

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 Agents applications


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