Skip to main content

AI Gateway

Route your LLM calls through the AI Gateway with a single base URL change. Zero vendor lock-in: always run on the best model at the lowest cost for your use case.

Observability

Instrument your code with OpenTelemetry to capture traces, logs, and metrics for every LLM call, agent step, and tool use.

AI Gateway

Overview

The OpenAI SDK provides powerful tools for building AI applications with GPT models. By connecting the SDK to Toqan Router’s AI Gateway, you transform your OpenAI integration into a production-ready system with enterprise-grade capabilities, complete observability, and access to 500+ models across 30+ providers.

Key Benefits

Toqan Router’s AI Gateway enhances your OpenAI SDK with:

Complete Observability

Track every API call, token usage, and model interaction with detailed traces and analytics

Built-in Reliability

Automatic fallbacks, retries, and load balancing for production resilience

Cost Optimization

Real-time cost tracking and spend management across all your AI operations

Multi-Provider Access

Access 500+ LLMs and 30+ providers through a single, unified integration

Prerequisites

Before integrating OpenAI SDK with Toqan Router, ensure you have:
  • A Toqan Router account and API Key
  • Python 3.8+ or Node.js 18+ with TypeScript support
  • OpenAI SDK installed
To setup your API key, see API keys & Endpoints.
To use libraries with private models, see Onboarding Private Models.
For a step-by-step migration of an existing OpenAI SDK application, see Migrate to Toqan Router.

Installation

Configuration

While using the OpenAI SDK, set the Base URL to the AI Gateway to feed calls through our API without changing any other part of your code. Using the Toqan Router AI Gateway, you benefit from Platform Traces and Cost and Usage Monitoring, keeping full compatibility and a unified API with all models while using the OpenAI SDK.
base_url: https://router.toqan.ai/eu/v3/router

Text Generation

Basic text generation with the OpenAI SDK through Toqan Router:

Streaming Responses

Stream responses for real-time output:

Model Selection

With Toqan Router, you can use any supported model from 30+ providers:

Observability

Getting Started

Integrate OpenAI SDK with Toqan Router’s observability to gain complete insights into model performance, token usage, API latency, and conversation flows using OpenTelemetry.

Prerequisites

Before you begin, ensure you have:
  • A Toqan Router account and API Key
  • Python 3.8+ or Node.js 18+
  • OpenAI SDK installed

Install Dependencies

Configure Toqan Router

Set up your environment variables to connect to Toqan Router’s OpenTelemetry collector: Unix/Linux/macOS:
Windows (PowerShell):
Using .env file:

Setup

Configure OpenTelemetry once at application startup:

Examples

The examples below use the Chat Completions endpoint. OpenTelemetry instrumentation works identically with the Responses API: replace client.chat.completions.create(...) with client.responses.create(...).

Basic Example

Streaming Example

Custom Spans Example

Advanced Workflows Example

View Traces

View your traces in the AI Studio in the Traces tab.
Visit your AI Studio to view real-time analytics and traces.