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

Pydantic AI is a Python agent framework designed to make it easier to build production-grade applications with Generative AI. By connecting Pydantic AI to Toqan Router’s AI Gateway, you transform experimental agents into production-ready systems with enterprise-grade capabilities.

Key Benefits

Toqan Router’s AI Gateway enhances your Pydantic AI agents with:

Complete Observability

Track every agent step, tool use, and 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 Pydantic AI with Toqan Router, ensure you have:
  • A Toqan Router account and API Key
  • Python 3.9 or higher
  • Pydantic AI SDK installed
To setup your API key, see API keys & Endpoints.

Installation

Install Pydantic AI and the OpenAI SDK:

Configuration

Configure Pydantic AI to use Toqan Router’s AI Gateway by passing a custom OpenAI client:
Python
base_url: https://router.toqan.ai/eu/v3/router

Basic Agent Example

Here’s a complete example of creating and running a Pydantic AI agent through Toqan Router:
Python

Agent with Tools

Pydantic AI agents can use tools while routing through Toqan Router:
Python

Structured Outputs

Pydantic AI excels at structured outputs with type-safe validation:
Python

Model Selection

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

Observability

Getting Started

Integrate Pydantic AI with Toqan Router’s observability to gain complete insights into your AI agent interactions, tool usage, model performance, and conversation flows using OpenTelemetry.

Prerequisites

Before you begin, ensure you have:
  • A Toqan Router account and API Key
  • Pydantic AI installed in your project
  • Python 3.9+

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:

Integration Example

Using LogFire for OpenTelemetry tracing:
Python
Always pass name= to Agent(...). Without it, gen_ai.agent.name defaults to the literal string 'agent' and every trace root renders as agent run in Traces.
Use logfire.span('label') to set a custom name on the trace root for ad-hoc per-call labelling. The wrapper becomes the trace root and the agent runs as its child.
Python

Streaming

Use agent.run_stream(...) to consume tokens incrementally. Traces capture the same shape as run_sync: a chat span under the agent run, with usage recorded on the final chunk.
Python
The same async pattern works with the AI Gateway configuration shown in Integration Example by replacing the agent construction with the OpenAIChatModel(provider=OpenAIProvider(...)) form.

Troubleshooting

404 on span export: verify OTEL_EXPORTER_OTLP_ENDPOINT is exactly https://router.toqan.ai/eu/v2/otel with no trailing path. The OpenTelemetry SDK appends /v1/traces automatically. Traces missing entirely: call logfire.instrument_pydantic_ai() before any Agent(...) invocation, and confirm logfire>=4 is installed.

View Traces

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