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

# OpenAI

> Use OpenAI GPT models through the Toqan Router AI Gateway with BYOK API key setup, caching, fallbacks, streaming, and full request observability.

## Setup Your API Key

To use OpenAI with Toqan Router, 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>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. Set **Geography** to the region OpenAI issued the key for. The default, **Global (Recommended for most use cases)**, uses the standard OpenAI endpoint. **US**, **Europe**, **Japan**, **South Korea**, **Canada**, **Singapore**, **India**, **Australia**, and **United Kingdom** route requests to that region's OpenAI data residency endpoint.
8. Click **Save** to complete the setup

Your OpenAI API key is now configured and ready to use with Toqan Router 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.6-sol` | 1.05M | Complex professional work and agentic coding |
| `openai/gpt-5.6-terra` | 1.05M | Balanced intelligence and cost for everyday work |
| `openai/gpt-5.6-luna` | 1.05M | Fast, cost-efficient, high-volume workloads |
| `openai/gpt-5.4` | 1.05M | Complex professional work |
| `openai/gpt-5.4-mini` | 400K | Cost-efficient coding and visual reasoning |

For a complete list of available OpenAI models, see [Supported Models](/docs/ai-gateway/supported-models.md#chat-models).

> [!TIP]
>
> Choose a model based on the workload. Use `openai/gpt-5.6-sol` for complex work, `openai/gpt-5.6-terra` for balanced everyday work, or `openai/gpt-5.6-luna` for high-volume workloads.

> [!NOTE]
>
> Reasoning models (`o1`, `o3`, `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.

> [card] [Web Search](/docs/ai-gateway/features/web-search.md)
>
> Add web search to Responses API calls with `web_search_preview` tools.

> [card] [Reasoning models](/docs/ai-gateway/features/reasoning.md)
>
> Configure `reasoning_effort` for o1 and o3 models through the **AI Gateway**.

## Quick Start

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

```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST https://router.toqan.ai/eu/v3/router/responses \
  -H "Authorization: Bearer $TOQAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-sol",
    "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.TOQAN_API_KEY,
  baseURL: "https://router.toqan.ai/eu/v3/router",
});

const response = await client.responses.create({
  model: "openai/gpt-5.6-sol",
  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("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

response = client.responses.create(
    model="openai/gpt-5.6-sol",
    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.TOQAN_API_KEY,
  baseURL: "https://router.toqan.ai/eu/v3/router",
});

const response = await client.chat.completions.create({
  model: "openai/gpt-5.6-sol",
  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("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

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

print(response.choices[0].message.content)
```

## Using the AI Gateway

Access OpenAI's GPT models 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.

> [!NOTE]
>
> OpenAI models use the provider slug format: `openai/model-name`. For example: `openai/gpt-5.6-sol`

### 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://router.toqan.ai/eu/v3/router
```

**Required Headers**

Include the following headers in all requests:

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

Optional headers for advanced routing:

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

**Getting your API Key:**

1. Go to API Keys
2. Click <kbd>Create API Key</kbd> and copy it
3. Store it in your environment as `TOQAN_API_KEY`

**SDK Installation**

Install the OpenAI SDK for your language:

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

> [!TIP]
>
> If your OpenAI code is already functioning, you only need to change the `base_url` and `api_key` to the router endpoint and `TOQAN_API_KEY`.

### Basic Usage

Send messages to OpenAI models and get intelligent responses:

```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST https://router.toqan.ai/eu/v3/router/responses \
  -H "Authorization: Bearer $TOQAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-sol",
    "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.TOQAN_API_KEY,
  baseURL: "https://router.toqan.ai/eu/v3/router",
});

const response = await client.responses.create({
  model: "openai/gpt-5.6-sol",
  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("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

response = client.responses.create(
    model="openai/gpt-5.6-sol",
    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.TOQAN_API_KEY,
  baseURL: "https://router.toqan.ai/eu/v3/router",
});

const response = await client.chat.completions.create({
  model: "openai/gpt-5.6-sol",
  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("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

response = client.chat.completions.create(
    model="openai/gpt-5.6-sol",
    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)
```

### Function Calling

OpenAI models support function calling for structured interactions:

```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST https://router.toqan.ai/eu/v3/router/responses \
  -H "Authorization: Bearer $TOQAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-sol",
    "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.TOQAN_API_KEY,
  baseURL: "https://router.toqan.ai/eu/v3/router",
});

const response = await client.responses.create({
  model: "openai/gpt-5.6-sol",
  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("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

response = client.responses.create(
    model="openai/gpt-5.6-sol",
    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.TOQAN_API_KEY,
  baseURL: "https://router.toqan.ai/eu/v3/router",
});

const response = await client.chat.completions.create({
  model: "openai/gpt-5.6-sol",
  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("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

response = client.chat.completions.create(
    model="openai/gpt-5.6-sol",
    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}")
```

> [card] [Tool Calling](/docs/ai-gateway/features/tool-calling.md)
>
> Full reference for function tools, `tool_choice`, parallel calls, and streaming with tool calls through the **AI Gateway**.

### Streaming

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

```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST https://router.toqan.ai/eu/v3/router/responses \
  -H "Authorization: Bearer $TOQAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-sol",
    "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://router.toqan.ai/eu/v3/router/chat/completions \
  -H "Authorization: Bearer $TOQAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-sol",
    "messages": [
      {
        "role": "user",
        "content": "Write a short poem about the ocean"
      }
    ],
    "stream": true
  }'
```

## Using the Responses API

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

> [card] [Web Search](/docs/ai-gateway/features/web-search.md)
>
> Add web search to Responses API calls with `web_search_preview` tools.

> [card] [Reasoning models](/docs/ai-gateway/features/reasoning.md)
>
> Use the `reasoning` object with o-series models on the Responses API.

### Endpoint

```
POST https://router.toqan.ai/eu/v3/router/responses
```

### Basic Usage

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

```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST https://router.toqan.ai/eu/v3/router/responses \
  -H "Authorization: Bearer $TOQAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-sol",
    "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.TOQAN_API_KEY,
  baseURL: "https://router.toqan.ai/eu/v3/router",
});

const stream = await client.responses.create({
  model: "openai/gpt-5.6-sol",
  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("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

stream = client.responses.create(
    model="openai/gpt-5.6-sol",
    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.TOQAN_API_KEY,
  baseURL: "https://router.toqan.ai/eu/v3/router",
});

const stream = await client.chat.completions.create({
  model: "openai/gpt-5.6-sol",
  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("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

stream = client.chat.completions.create(
    model="openai/gpt-5.6-sol",
    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)
```

## 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.4-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)


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