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

# OpenAI Agents SDK integration

> Connect OpenAI Agents SDK to Orq.ai's AI Gateway for complete observability, built-in reliability, and access to 300+ LLMs across 20+ providers.

## AI Gateway

### Overview

OpenAI Agents SDK enables powerful AI-driven automation through structured conversations and tool calling. Connecting the Agents SDK to Orq.ai's AI Gateway transforms experimental agents into production-ready systems with enterprise-grade capabilities.

### Key Benefits

Orq.ai's AI Gateway enhances OpenAI Agents with:

<CardGroup cols={2}>
  <Card title="Complete Observability" icon="chart-line">
    Track every agent step, tool use, and interaction with detailed traces and analytics
  </Card>

  <Card title="Built-in Reliability" icon="shield-check">
    Automatic fallbacks, retries, and load balancing for production resilience
  </Card>

  <Card title="Cost Optimization" icon="chart-pie">
    Real-time cost tracking and spend management across all AI operations
  </Card>

  <Card title="Multi-Provider Access" icon="cubes">
    Access 300+ LLMs and 20+ providers through a single, unified integration
  </Card>
</CardGroup>

### Prerequisites

Before integrating OpenAI Agents SDK with Orq.ai, ensure the following are in place:

* An Orq.ai account and [API Key](/docs/ai-gateway/configuration/api-keys)
* Python 3.9 or higher
* OpenAI Agents SDK installed

<Info>
  To set up an API key, see [API keys & Endpoints](/docs/ai-gateway/configuration/api-keys).
</Info>

### Installation

Install the OpenAI Agents SDK:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install openai-agents openai
```

### Configuration

Configure OpenAI Agents SDK to use Orq.ai's AI Gateway by setting a custom AsyncOpenAI client:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from agents import set_default_openai_client, set_tracing_disabled
import os

# Configure OpenAI client with Orq.ai AI Gateway
client = AsyncOpenAI(
    api_key=os.getenv("ORQ_API_KEY"),
    base_url="https://api.orq.ai/v3/router"
)

# Disable the SDK's built-in OpenAI tracing exporter for gateway-only use
set_tracing_disabled(True)

# Set as default client for all agents
set_default_openai_client(client)
```

> **base\_url**: `https://api.orq.ai/v3/router`

### Built-in OpenAI Tracing

<Warning>
  The OpenAI Agents SDK includes built-in tracing that exports to OpenAI's hosted Traces dashboard by default. When you use Orq.ai's AI Gateway with `ORQ_API_KEY` and do not configure OpenAI tracing credentials, the SDK can print `No API key provided for OpenAI tracing exporter. Exports will be skipped`.
</Warning>

Do not set `OPENAI_API_KEY` only to silence this warning. That sends SDK traces to OpenAI's dashboard instead of Orq.ai. Disable the SDK's built-in OpenAI tracing for gateway-only use when agent traces in Orq.ai are not needed.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
export OPENAI_AGENTS_DISABLE_TRACING=1
```

Or disable it in code:

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import { setTracingDisabled } from "@openai/agents";

  setTracingDisabled(true);
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from agents import set_tracing_disabled

  set_tracing_disabled(True)
  ```
</CodeGroup>

### Basic Agent Example

Here's a complete example of creating and running an OpenAI agent through Orq.ai:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from agents import Agent, Runner, set_default_openai_client, set_tracing_disabled
import os

# Configure client with Orq.ai AI Gateway
client = AsyncOpenAI(
    api_key=os.getenv("ORQ_API_KEY"),
    base_url="https://api.orq.ai/v3/router"
)
set_tracing_disabled(True)
set_default_openai_client(client)

# Create agent
agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant that explains complex concepts simply."
)

# Run the agent
result = Runner.run_sync(agent, "Explain quantum computing in simple terms")
print(result.final_output)
```

### Agent with Tools

OpenAI Agents can use tools while routing through Orq.ai:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from agents import Agent, Runner, set_default_openai_client, set_tracing_disabled, function_tool
import os

# Configure client
client = AsyncOpenAI(
    api_key=os.getenv("ORQ_API_KEY"),
    base_url="https://api.orq.ai/v3/router"
)
set_tracing_disabled(True)
set_default_openai_client(client)

# Define a tool using the @function_tool decorator
@function_tool
def get_weather(location: str) -> str:
    """Get the current weather for a location."""
    return f"The weather in {location} is sunny and 72°F"

# Create agent with tools
agent = Agent(
    name="Weather Assistant",
    instructions="You are a weather assistant. Use the get_weather function to provide weather information.",
    tools=[get_weather]
)

# Run agent with tool access
result = Runner.run_sync(agent, "What's the weather in San Francisco?")
print(result.final_output)
```

### Model Selection

With Orq.ai, any supported model from 20+ providers can be used:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from agents import Agent, Runner, set_default_openai_client, set_tracing_disabled
import os

# Configure client
client = AsyncOpenAI(
    api_key=os.getenv("ORQ_API_KEY"),
    base_url="https://api.orq.ai/v3/router"
)
set_tracing_disabled(True)
set_default_openai_client(client)

# Use Claude
claude_agent = Agent(
    name="Claude Assistant",
    model="anthropic/claude-sonnet-4-6",
    instructions="You are a helpful assistant."
)

# Use Gemini
gemini_agent = Agent(
    name="Gemini Assistant",
    model="google-ai/gemini-2.5-flash",
    instructions="You are a helpful assistant."
)

# Use any other model
groq_agent = Agent(
    name="Groq Assistant",
    model="groq/llama-3.3-70b-versatile",
    instructions="You are a helpful assistant."
)

# Run with different models
result = Runner.run_sync(claude_agent, "Explain machine learning")
print(result.final_output)
```
