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

# OpenAI Agents SDK

> AgentOps and OpenAI Agents SDK integration for powerful multi-agent workflow monitoring.

## Video Tutorial

<iframe width="560" height="315" src="https://www.youtube.com/embed/9FH-BYqYYrQ" title="AgentOps + OpenAI Agents SDK Integration Tutorial" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen />

[OpenAI Agents Python](https://github.com/openai/openai-agents-python) is a lightweight yet powerful SDK for building multi-agent workflows in Python. AgentOps seamlessly integrates to provide observability into these workflows.

* [OpenAI Agents Python documentation](https://openai.github.io/openai-agents-python/)
* [TypeScript guide](/v2/integrations/openai_agents_js)

## Core Concepts

* **Agents**: LLMs configured with instructions, tools, guardrails, and handoffs
* **Handoffs**: Allow agents to transfer control to other agents for specific tasks
* **Guardrails**: Configurable safety checks for input and output validation
* **Tracing**: Built-in tracking of agent runs, allowing you to view, debug and optimize your workflows

## Python

### Installation

Install AgentOps, the OpenAI Agents SDK, and `python-dotenv` for managing API keys:

<CodeGroup>
  ```bash pip  theme={null}
  pip install agentops openai-agents python-dotenv
  ```

  ```bash poetry theme={null}
  poetry add agentops openai-agents python-dotenv
  ```

  ```bash uv theme={null}
  uv pip install agentops openai-agents python-dotenv
  ```
</CodeGroup>

### Setting Up API Keys

Before using the OpenAI Agents SDK with AgentOps, you need to set up your API keys:

* **OPENAI\_API\_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys)
* **AGENTOPS\_API\_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)

You can set these as environment variables or in a `.env` file.

<CodeGroup>
  ```bash Export to CLI theme={null}
  export OPENAI_API_KEY="your_openai_api_key_here"
  export AGENTOPS_API_KEY="your_agentops_api_key_here"
  ```

  ```txt Set in .env file theme={null}
  OPENAI_API_KEY="your_openai_api_key_here"
  AGENTOPS_API_KEY="your_agentops_api_key_here"
  ```
</CodeGroup>

Then load them in your Python code:

```python theme={null}
from dotenv import load_dotenv
import os

load_dotenv()

AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
```

### Usage

AgentOps will automatically instrument the OpenAI Agents SDK after being initialized. You can then create agents, run them, and track their interactions.

```python theme={null}
import agentops
from agents import Agent, Runner

# Initialize AgentOps
agentops.init()

# Create an agent with instructions
agent = Agent(name="Assistant", instructions="You are a helpful assistant")

result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)
```

## Examples

<CodeGroup>
  ```python Handoffs theme={null}
  from agents import Agent, Runner
  import asyncio
  import agentops
  import os

  agentops.init()

  spanish_agent = Agent(
      name="Spanish agent",
      instructions="You only speak Spanish.",
  )

  english_agent = Agent(
      name="English agent",
      instructions="You only speak English",
  )

  triage_agent = Agent(
      name="Triage agent",
      instructions="Handoff to the appropriate agent based on the language of the request.",
      handoffs=[spanish_agent, english_agent],
  )


  async def main():
      result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
      print(result.final_output)
      # Expected Output: ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?


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

  ```python Function Calling theme={null}
  import asyncio
  from agents import Agent, Runner, function_tool
  import agentops
  import os

  agentops.init()

  @function_tool
  def get_weather(city: str) -> str:
      return f"The weather in {city} is sunny."


  agent = Agent(
      name="Weather Agent", 
      instructions="You are a helpful agent that can get weather information.",
      tools=[get_weather],
  )


  async def main():
      result = await Runner.run(agent, input="What's the weather in Tokyo?")
      print(result.final_output)
      # Expected Output: The weather in Tokyo is sunny.


  if __name__ == "__main__":
      asyncio.run(main())
  ```
</CodeGroup>

## More Examples

<CardGroup cols={2}>
  <Card title="Customer Service Agent" icon="notebook" href="/v2/examples/openai_agents">
    Demonstrates a customer service workflow
  </Card>

  <Card title="Agent Patterns" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/openai_agents/agent_patterns.ipynb" newTab={true}>
    Illustrates various agent interaction patterns.
  </Card>

  <Card title="Agents with Tools" icon="notebook" href="https://github.com/AgentOps-AI/agentops/blob/main/examples/openai_agents/agents_tools.ipynb" newTab={true}>
    Showcases agents utilizing different tools.
  </Card>
</CardGroup>

<script type="module" src="/scripts/github_stars.js" />

<script type="module" src="/scripts/scroll-img-fadein-animation.js" />

<script type="module" src="/scripts/button_heartbeat_animation.js" />

<script type="css" src="/styles/styles.css" />

<script type="module" src="/scripts/adjust_api_dynamically.js" />
