> ## 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 SDK drop-in integration

> Use the AI Gateway with OpenAI SDK. Replace OpenAI base URL with Orq.ai for routing, caching, monitoring, and multi-provider fallback capabilities.

## AI Gateway

### Overview

The OpenAI SDK provides powerful tools for building AI applications with GPT models. Connecting the SDK to Orq.ai's **AI Gateway** transforms any OpenAI integration into a production-ready system with enterprise-grade capabilities, complete observability, and access to 300+ models across 20+ providers.

### Key Benefits

Orq.ai's AI Gateway enhances the OpenAI SDK with:

<CardGroup cols={2}>
  <Card title="Complete Observability" icon="chart-line">
    Track every API call, token usage, and model 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 the OpenAI 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.8+ or Node.js 18+ with TypeScript support
* OpenAI SDK installed

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

<Info>
  To use libraries with private models, see [Onboarding Private Models](/docs/ai-gateway/using-the-router#onboarding-private-models).
</Info>

### Installation

<CodeGroup>
  ```bash TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  npm install openai
  ```

  ```bash Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  pip install openai
  ```
</CodeGroup>

### Configuration

Set the **Base URL** to the [AI Gateway](/docs/ai-gateway/using-the-router) to route calls through the API without changing any other part of the code.

The **Orq.ai AI Gateway** provides Platform [Traces](/docs/ai-gateway/traces), keeping full compatibility and a unified API with all models while using the OpenAI SDK.

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

### Text Generation

Basic text generation with the OpenAI SDK through Orq.ai:

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

  const openai = new OpenAI({
    baseURL: "https://api.orq.ai/v3/router",
    apiKey: process.env.ORQ_API_KEY,
  });

  const response = await openai.responses.create({
    model: "openai/gpt-4o",
    instructions: "You are a helpful assistant.",
    input: "Hello!",
  });

  console.log(response.output_text);
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from openai import OpenAI
  import os

  client = OpenAI(
      base_url="https://api.orq.ai/v3/router",
      api_key=os.environ.get("ORQ_API_KEY"),
  )

  response = client.responses.create(
      model="openai/gpt-4o",
      instructions="You are a helpful assistant.",
      input="Hello!",
  )

  print(response.output_text)
  ```

  ```typescript TypeScript (Chat Completions) theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import OpenAI from "openai";

  const openai = new OpenAI({
    baseURL: "https://api.orq.ai/v3/router",
    apiKey: process.env.ORQ_API_KEY,
  });

  const completion = await openai.chat.completions.create({
    model: "openai/gpt-4o",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: "Hello!" },
    ],
  });

  console.log(completion.choices[0].message.content);
  ```

  ```python Python (Chat Completions) theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from openai import OpenAI
  import os

  client = OpenAI(
      base_url="https://api.orq.ai/v3/router",
      api_key=os.environ.get("ORQ_API_KEY"),
  )

  completion = client.chat.completions.create(
      model="openai/gpt-4o",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"},
      ],
  )

  print(completion.choices[0].message.content)
  ```
</CodeGroup>

### Streaming Responses

Stream responses for real-time output:

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

  const openai = new OpenAI({
    baseURL: "https://api.orq.ai/v3/router",
    apiKey: process.env.ORQ_API_KEY,
  });

  const stream = await openai.responses.create({
    model: "openai/gpt-4o",
    input: "Write a short story about robots.",
    stream: true,
  });

  for await (const event of stream) {
    if (event.type === "response.output_text.delta") {
      process.stdout.write(event.delta);
    }
  }
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from openai import OpenAI
  import os

  client = OpenAI(
      base_url="https://api.orq.ai/v3/router",
      api_key=os.environ.get("ORQ_API_KEY"),
  )

  stream = client.responses.create(
      model="openai/gpt-4o",
      input="Write a short story about robots.",
      stream=True,
  )

  for event in stream:
      if event.type == "response.output_text.delta":
          print(event.delta, end="", flush=True)
  ```

  ```typescript TypeScript (Chat Completions) theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import OpenAI from "openai";

  const openai = new OpenAI({
    baseURL: "https://api.orq.ai/v3/router",
    apiKey: process.env.ORQ_API_KEY,
  });

  const stream = await openai.chat.completions.create({
    model: "openai/gpt-4o",
    messages: [{ role: "user", content: "Write a short story about robots." }],
    stream: true,
  });

  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta?.content || "");
  }
  ```

  ```python Python (Chat Completions) theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from openai import OpenAI
  import os

  client = OpenAI(
      base_url="https://api.orq.ai/v3/router",
      api_key=os.environ.get("ORQ_API_KEY"),
  )

  stream = client.chat.completions.create(
      model="openai/gpt-4o",
      messages=[{"role": "user", "content": "Write a short story about robots."}],
      stream=True,
  )

  for chunk in stream:
      if chunk.choices and chunk.choices[0].delta.content:
          print(chunk.choices[0].delta.content, end="", flush=True)
  ```
</CodeGroup>

### Model Selection

With Orq.ai, you can use any supported model from 20+ providers:

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

  const openai = new OpenAI({
    baseURL: "https://api.orq.ai/v3/router",
    apiKey: process.env.ORQ_API_KEY,
  });

  const claudeResponse = await openai.responses.create({
    model: "anthropic/claude-sonnet-4-6",
    input: "Explain machine learning",
  });

  const geminiResponse = await openai.responses.create({
    model: "google-ai/gemini-2.5-flash",
    input: "Explain machine learning",
  });

  const groqResponse = await openai.responses.create({
    model: "groq/llama-3.3-70b-versatile",
    input: "Explain machine learning",
  });
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from openai import OpenAI
  import os

  client = OpenAI(
      base_url="https://api.orq.ai/v3/router",
      api_key=os.environ.get("ORQ_API_KEY"),
  )

  claude_response = client.responses.create(
      model="anthropic/claude-sonnet-4-6",
      input="Explain machine learning",
  )

  gemini_response = client.responses.create(
      model="google-ai/gemini-2.5-flash",
      input="Explain machine learning",
  )

  groq_response = client.responses.create(
      model="groq/llama-3.3-70b-versatile",
      input="Explain machine learning",
  )
  ```
</CodeGroup>
