> ## 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

> Route OpenAI model requests through Orq.ai's AI Gateway. Set up your API key and access GPT, DALL-E, and Whisper models with caching, fallbacks, and streaming.

## Setup Your API Key

To use OpenAI with Orq.ai, follow these steps:

1. Navigate to **AI Gateway** > BYOK
2. Find **OpenAI** in the list
3. Click the **Configure** button next to OpenAI
4. In the modal that opens, select <kbd className="key">Setup your own API Key</kbd>
5. Enter a name for this configuration (e.g., "OpenAI Production")
6. Paste your OpenAI API Key into the provided field
7. Click **Save** to complete the setup

Your OpenAI API key is now configured and ready to use with Orq.ai in **AI Studio** or through the **AI Gateway**.

## Available Models

The **AI Gateway** supports all current OpenAI models. Here are the most commonly used:

### Recommended Models

| Model                | Context | Best For                               |
| -------------------- | ------- | -------------------------------------- |
| `openai/gpt-5.4`     | 128K    | Latest GPT-5 generation                |
| `openai/gpt-5.2-pro` | 128K    | Most advanced, production-ready        |
| `openai/gpt-4o`      | 128K    | High performance, widely available     |
| `openai/gpt-4o-mini` | 128K    | Cost-effective with strong performance |
| `openai/gpt-5-mini`  | 4K      | Budget-friendly for simple tasks       |

<AccordionGroup>
  <Accordion title="View all Supported models" icon="openai">
    #### Latest Generation (GPT-5.x)

    * `openai/gpt-5.4`: Latest GPT-5 generation
    * `openai/gpt-5.2-pro`: Most capable, production-ready
    * `openai/gpt-5.2-chat-latest`: GPT-5.2 latest
    * `openai/gpt-5.2`: GPT-5.2 base
    * `openai/gpt-5.1-chat-latest`: GPT-5.1 latest
    * `openai/gpt-5.1`: GPT-5.1 base
    * `openai/gpt-5-pro`: GPT-5 advanced
    * `openai/gpt-5-chat-latest`: GPT-5 latest
    * `openai/gpt-5-mini`: GPT-5 fast
    * `openai/gpt-5-nano`: GPT-5 lightweight
    * `openai/gpt-5`: GPT-5 base

    #### Current Generation (GPT-4.x)

    * `openai/gpt-4.1-2025-04-14`: GPT-4.1 April 2025
    * `openai/gpt-4.1-mini-2025-04-14`: GPT-4.1 mini April 2025
    * `openai/gpt-4.1-nano-2025-04-14`: GPT-4.1 nano April 2025
    * `openai/gpt-4.1`: GPT-4.1 base
    * `openai/gpt-4.1-mini`: GPT-4.1 mini
    * `openai/gpt-4.1-nano`: GPT-4.1 nano
    * `openai/gpt-4o`: Latest GPT-4o, recommended
    * `openai/gpt-4o-2024-11-20`: GPT-4o November 2024
    * `openai/gpt-4o-2024-08-06`: GPT-4o August 2024
    * `openai/gpt-4o-2024-05-13`: GPT-4o May 2024
    * `openai/gpt-4o-mini`: Optimized for speed and cost
    * `openai/gpt-4o-mini-2024-07-18`: GPT-4o mini July 2024
    * `openai/gpt-4-turbo`: High-performance model
    * `openai/gpt-4-turbo-2024-04-09`: GPT-4 Turbo April 2024

    #### Legacy Models

    * `openai/gpt-5-mini`: Cost-effective, widely used
    * `openai/gpt-5-mini`: GPT-3.5 Turbo January 2025
    * `openai/gpt-5-mini`: Extended context

    #### Reasoning Models (Advanced)

    * `openai/o3-pro`: Advanced reasoning
    * `openai/o3-2025-04-16`: O3 April 2025
    * `openai/o3`: O3 foundation
    * `openai/o3-mini`: O3 lightweight
    * `openai/o3-mini-2025-01-31`: O3-mini January 2025
    * `openai/o1-pro`: Professional reasoning
    * `openai/o1-2024-12-17`: O1 December 2024
    * `openai/o1`: O1 foundation
    * `openai/o4-mini-2025-04-16`: O4-mini April 2025
    * `openai/o4-mini`: O4-mini base

    For a complete and up-to-date list of all available OpenAI models, see [Supported Models](/docs/ai-studio/ai-gateway/supported-models#chat-models).

    All models are available through the **AI Gateway** with the `openai/` prefix.
  </Accordion>
</AccordionGroup>

<Tip>
  Use `openai/gpt-5.4` for the latest model, or `openai/gpt-4o` for the best balance of performance, cost, and availability.
</Tip>

<Note>
  Reasoning models (`o1`, `o1-pro`, `o3`, `o3-pro`, `o4-mini`) require developer mode enabled in your OpenAI account. Enable it in your account settings and accept the reasoning model terms before using these models.
</Note>

<CardGroup cols={2}>
  <Card title="Web Search" icon="magnifying-glass" href="/docs/ai-studio/ai-gateway/web-search" horizontal>
    Add web search to Responses API calls with `web_search_preview` tools.
  </Card>

  <Card title="Reasoning models" icon="brain" href="/docs/ai-studio/ai-gateway/reasoning" horizontal>
    Configure `reasoning_effort` for o1 and o3 models through the **AI Gateway**.
  </Card>
</CardGroup>

## Quick Start

Access OpenAI's GPT models through the **AI Gateway**.

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/responses \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "input": "Explain quantum computing in simple terms"
    }'
  ```

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

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

  const response = await client.responses.create({
    model: "openai/gpt-4o",
    input: "Explain quantum computing in simple terms",
  });

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

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

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

  response = client.responses.create(
      model="openai/gpt-4o",
      input="Explain quantum computing in simple terms",
  )

  print(response.output_text)
  ```

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

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

  const response = await client.chat.completions.create({
    model: "openai/gpt-4o",
    messages: [
      {
        role: "user",
        content: "Explain quantum computing in simple terms",
      },
    ],
  });

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

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

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

  response = client.chat.completions.create(
      model="openai/gpt-4o",
      messages=[
          {
              "role": "user",
              "content": "Explain quantum computing in simple terms",
          }
      ],
  )

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

## Using the AI Gateway

Access OpenAI's GPT models (GPT-4, GPT-4o, GPT-4 Turbo) through the **AI Gateway** using the Responses API or Chat Completions, with streaming, function calling, and intelligent model routing. All OpenAI models are available with consistent formatting and automatic request logging.

<Info>
  OpenAI models use the provider slug format: `openai/model-name`. For example: `openai/gpt-4o`
</Info>

### Prerequisites

Before making requests to the **AI Gateway**, you need to configure your environment and install the SDKs if you choose to use them.

**Router Endpoint**

To use the **AI Gateway** with OpenAI models, configure your OpenAI SDK client with the base URL:

```
https://api.orq.ai/v3/router
```

**Required Headers**

Include the following headers in all requests:

```
Authorization: Bearer $ORQ_API_KEY
Content-Type: application/json
```

Optional headers for advanced routing:

```
X-Orq-Provider: openai
X-Orq-Virtual-Key: your-virtual-key-name (optional)
```

**Getting your API Key:**

1. Go to [API Keys](/docs/ai-studio/organization/api-keys)
2. Click <kbd className="key">Create API Key</kbd> and copy it
3. Store it in your environment as `ORQ_API_KEY`

**SDK Installation**

Install the OpenAI SDK for your language:

<CodeGroup>
  ```bash Node.js/TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  npm install openai
  # or
  yarn add openai
  ```

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

<Tip>
  If your OpenAI code is already functioning, you only need to change the `base_url` and `api_key` to the router endpoint and `ORQ_API_KEY`.
</Tip>

### Basic Usage

Send messages to OpenAI models and get intelligent responses:

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/responses \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "instructions": "You are a helpful assistant that explains complex concepts simply.",
      "input": "Explain machine learning"
    }'
  ```

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

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

  const response = await client.responses.create({
    model: "openai/gpt-4o",
    instructions: "You are a helpful assistant that explains complex concepts simply.",
    input: "Explain machine learning",
  });

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

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

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

  response = client.responses.create(
      model="openai/gpt-4o",
      instructions="You are a helpful assistant that explains complex concepts simply.",
      input="Explain machine learning",
  )

  print(response.output_text)
  ```

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

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

  const response = await client.chat.completions.create({
    model: "openai/gpt-4o",
    messages: [
      {
        role: "system",
        content: "You are a helpful assistant that explains complex concepts simply.",
      },
      {
        role: "user",
        content: "Explain machine learning",
      },
    ],
    temperature: 0.7,
    max_tokens: 500,
  });

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

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

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

  response = client.chat.completions.create(
      model="openai/gpt-4o",
      messages=[
          {
              "role": "system",
              "content": "You are a helpful assistant that explains complex concepts simply.",
          },
          {
              "role": "user",
              "content": "Explain machine learning",
          },
      ],
      temperature=0.7,
      max_tokens=500,
  )

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

### Function Calling

OpenAI models support function calling for structured interactions:

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/responses \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "input": "What'\''s the weather in San Francisco?",
      "tools": [
        {
          "type": "function",
          "name": "get_weather",
          "description": "Get the current weather in a location",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA"
              },
              "unit": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"]
              }
            },
            "required": ["location"]
          }
        }
      ]
    }'
  ```

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

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

  const response = await client.responses.create({
    model: "openai/gpt-4o",
    input: "What's the weather in San Francisco?",
    tools: [
      {
        type: "function",
        name: "get_weather",
        description: "Get the current weather in a location",
        parameters: {
          type: "object",
          properties: {
            location: {
              type: "string",
              description: "The city and state, e.g. San Francisco, CA",
            },
            unit: {
              type: "string",
              enum: ["celsius", "fahrenheit"],
            },
          },
          required: ["location"],
        },
      },
    ],
  });

  const toolCall = response.output.find((item) => item.type === "function_call");
  if (toolCall?.type === "function_call") {
    console.log(`Calling function: ${toolCall.name}`);
    console.log(`Arguments: ${toolCall.arguments}`);
  }
  ```

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

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

  response = client.responses.create(
      model="openai/gpt-4o",
      input="What's the weather in San Francisco?",
      tools=[
          {
              "type": "function",
              "name": "get_weather",
              "description": "Get the current weather in a location",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "location": {
                          "type": "string",
                          "description": "The city and state, e.g. San Francisco, CA"
                      },
                      "unit": {
                          "type": "string",
                          "enum": ["celsius", "fahrenheit"]
                      }
                  },
                  "required": ["location"]
              }
          }
      ]
  )

  tool_call = next((item for item in response.output if item.type == "function_call"), None)
  if tool_call:
      print(f"Calling function: {tool_call.name}")
      print(f"Arguments: {tool_call.arguments}")
  ```

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

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

  const response = await client.chat.completions.create({
    model: "openai/gpt-4o",
    messages: [
      {
        role: "user",
        content: "What's the weather in San Francisco?",
      },
    ],
    tools: [
      {
        type: "function",
        function: {
          name: "get_weather",
          description: "Get the current weather in a location",
          parameters: {
            type: "object",
            properties: {
              location: {
                type: "string",
                description: "The city and state, e.g. San Francisco, CA",
              },
              unit: {
                type: "string",
                enum: ["celsius", "fahrenheit"],
              },
            },
            required: ["location"],
          },
        },
      },
    ],
  });

  const toolCall = response.choices[0].message.tool_calls?.[0];
  if (toolCall?.type === "function") {
    console.log(`Calling function: ${toolCall.function.name}`);
    console.log(`Arguments: ${toolCall.function.arguments}`);
  }
  ```

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

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

  response = client.chat.completions.create(
      model="openai/gpt-4o",
      messages=[
          {
              "role": "user",
              "content": "What's the weather in San Francisco?"
          }
      ],
      tools=[
          {
              "type": "function",
              "function": {
                  "name": "get_weather",
                  "description": "Get the current weather in a location",
                  "parameters": {
                      "type": "object",
                      "properties": {
                          "location": {
                              "type": "string",
                              "description": "The city and state, e.g. San Francisco, CA"
                          },
                          "unit": {
                              "type": "string",
                              "enum": ["celsius", "fahrenheit"]
                          }
                      },
                      "required": ["location"]
                  }
              }
          }
      ]
  )

  tool_call = next(iter(response.choices[0].message.tool_calls or []), None)
  if tool_call:
      print(f"Calling function: {tool_call.function.name}")
      print(f"Arguments: {tool_call.function.arguments}")
  ```
</CodeGroup>

<Card title="Tool Calling" icon="wrench" href="/docs/ai-studio/ai-gateway/tool-calling" horizontal>
  Full reference for function tools, `tool_choice`, parallel calls, and streaming with tool calls through the **AI Gateway**.
</Card>

### Streaming

Stream responses for real-time output and improved user experience:

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/responses \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "input": "Write a short poem about the ocean",
      "stream": true
    }'
  ```

  ```bash cURL (Chat Completions) theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/chat/completions \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "messages": [
        {
          "role": "user",
          "content": "Write a short poem about the ocean"
        }
      ],
      "stream": true
    }'
  ```
</CodeGroup>

## Using the Responses API

The Responses API combines the best of Chat Completions and Assistants APIs. Use `responses.create()` with the **AI Gateway**:

<CardGroup cols={2}>
  <Card title="Web Search" icon="magnifying-glass" href="/docs/ai-studio/ai-gateway/web-search" horizontal>
    Add web search to Responses API calls with `web_search_preview` tools.
  </Card>

  <Card title="Reasoning models" icon="brain" href="/docs/ai-studio/ai-gateway/reasoning" horizontal>
    Use the `reasoning` object with o-series models on the Responses API.
  </Card>
</CardGroup>

### Endpoint

```
POST https://api.orq.ai/v3/router/responses
```

### Basic Usage

Configure the OpenAI SDK to use the **AI Gateway** and call the Responses API:

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/responses \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "input": "Write a three sentence bedtime story about a unicorn"
    }'
  ```

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

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

  const stream = await client.responses.create({
    model: "openai/gpt-4o",
    input: "Write a short poem about the ocean",
    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(
      api_key=os.environ.get("ORQ_API_KEY"),
      base_url="https://api.orq.ai/v3/router",
  )

  stream = client.responses.create(
      model="openai/gpt-4o",
      input="Write a short poem about the ocean",
      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 client = new OpenAI({
    apiKey: process.env.ORQ_API_KEY,
    baseURL: "https://api.orq.ai/v3/router",
  });

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

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

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

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

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

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

## Automatic Request Logging

All requests made through the **AI Gateway** are automatically logged to your dashboard. You can view:

* **Request details**: Model used, tokens, latency
* **Cost tracking**: Per-request and aggregate costs
* **Error monitoring**: Failed requests with error messages
* **Performance metrics**: Response times and throughput

No additional configuration is needed. Logging happens automatically.

## Troubleshooting

| Issue                  | Problem                         | Solution                                                                                                                                                                       |
| ---------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Rate Limiting          | Too many requests to OpenAI API | Implement exponential backoff for retries. The AI Gateway automatically retries failed requests with appropriate delays.                                                       |
| High Latency           | Slow response times             | Monitor dashboard for model performance. Consider using `gpt-5-mini` for latency-sensitive applications.                                                                       |
| Invalid Request Errors | Malformed API requests          | Verify model name format (`openai/model-name`). Ensure required fields are present in messages array. Check that messages contain valid role values (user, assistant, system). |
| API Errors             | HTTP errors from OpenAI         | Handle errors with try/catch. Check error status codes and messages. Use the AI Gateway's automatic retry mechanism.                                                           |

## Reference

* [OpenAI API Documentation](https://platform.openai.com/docs/)
* [Model Pricing](https://openai.com/pricing)
* [API Reference](https://developers.openai.com/api/reference/overview)
* [Chat Completions Guide](https://platform.openai.com/docs/guides/chat-completions)
