Skip to content

Pydantic AI Gateway

Pydantic AI Gateway is a unified interface for accessing multiple AI providers with a single key, managed through Pydantic Logfire. Features include built-in OpenTelemetry observability, real-time cost monitoring, failover management, and native integration with the other tools in the Pydantic stack.

Sign up at logfire.pydantic.dev.

Documentation Integration

To help you get started with Pydantic AI Gateway, some code examples on the Pydantic AI documentation include a “Via Pydantic AI Gateway” tab, alongside a “Direct to Provider API” tab with the standard Pydantic AI model string. The main difference between them is that when using Gateway, model strings use the gateway/ prefix.

Key features

  • API key management: Access multiple LLM providers with a single Gateway key.
  • Cost Limits: Set spending limits at project, user, and API key levels with daily, weekly, and monthly caps.
  • BYOK and managed providers: Bring your own API keys (BYOK) from LLM providers, or pay for inference directly through the platform.
  • Multi-provider support: Access models from OpenAI, Anthropic, Google Vertex, Groq, and AWS Bedrock. More providers coming soon.
  • Backend observability: Log every request through Pydantic Logfire or any OpenTelemetry backend (coming soon).
  • Zero translation: Unlike traditional AI gateways that translate everything to one common schema, Pydantic AI Gateway allows requests to flow through directly in each provider’s native format. This gives you immediate access to new model features as soon as they are released.
  • Enterprise ready: Inherits Logfire’s enterprise features — including SSO, custom roles and permissions.
hello_world.py
from pydantic_ai import Agent

agent = Agent('gateway/openai:gpt-5.2')

result = agent.run_sync('Where does "hello world" come from?')
print(result.output)
"""
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
"""

Quick Start

This section contains instructions on how to set up your account and run your app with Pydantic AI Gateway credentials.

Create an account

  1. Sign up at logfire.pydantic.dev
  2. Choose a region and create an account.
  3. Activate the gateway in your organizations settings.

Create Gateway API keys

Go to your organization’s Gateway settings in Logfire and create an API key.

Usage

After setting up your account with the instructions above, you will be able to make an AI model request with the Pydantic AI Gateway. The code snippets below show how you can use Pydantic AI Gateway with different frameworks and SDKs.

To use different models, change the model string gateway/<api_format>:<model_name> to other models offered by the supported providers.

Examples of providers and models that can be used are:

ProviderAPI FormatExample Model
OpenAIopenaigateway/openai:gpt-5.2
Anthropicanthropicgateway/anthropic:claude-sonnet-4-6
Google Vertexgoogle-vertexgateway/google-vertex:gemini-3-flash-preview
Groqgroqgateway/groq:openai/gpt-oss-120b
AWS Bedrockbedrockgateway/bedrock:amazon.nova-micro-v1:0

Pydantic AI

Before you start, make sure you are on version 1.16 or later of pydantic-ai. To update to the latest version run:

Terminal
uv sync -P pydantic-ai

Set the PYDANTIC_AI_GATEWAY_API_KEY environment variable to your Gateway API key:

Terminal
export PYDANTIC_AI_GATEWAY_API_KEY="pylf_v..."

You can access multiple models with the same API key, as shown in the code snippet below.

hello_world.py
from pydantic_ai import Agent

agent = Agent('gateway/openai:gpt-5.2')

result = agent.run_sync('Where does "hello world" come from?')
print(result.output)
"""
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
"""

Claude Code

Before you start, log out of Claude Code using /logout.

Set your gateway credentials as environment variables, using the base URL that matches your Logfire region:

Terminal
export ANTHROPIC_BASE_URL="https://gateway-us.pydantic.dev/proxy/anthropic"
export ANTHROPIC_AUTH_TOKEN="YOUR_GATEWAY_API_KEY"

Replace YOUR_GATEWAY_API_KEY with the API key from your Logfire organization’s Gateway settings.

Launch Claude Code by typing claude. All requests will now route through the Pydantic AI Gateway.

Codex

Codex uses the OpenAI Responses API, so it should use the Gateway’s openai-responses route.

Set your gateway API key as an environment variable:

Terminal
export PYDANTIC_AI_GATEWAY_API_KEY="YOUR_GATEWAY_API_KEY"

Then add the following configuration to ~/.codex/config.toml, using the base URL that matches your Logfire region:

model = "gpt-5.4"
model_provider = "pydantic_gateway"

[model_providers.pydantic_gateway]
name = "Pydantic AI Gateway"
base_url = "https://gateway-us.pydantic.dev/proxy/openai-responses"
env_key = "PYDANTIC_AI_GATEWAY_API_KEY"
env_key_instructions = "Create a Gateway API key in your Logfire organization's Gateway settings."
wire_api = "responses"

For more details on configuring custom providers in Codex, see the Codex custom model providers docs and the Codex configuration reference.

If you already have a ~/.codex/config.toml, add the [model_providers.pydantic_gateway] block and update model_provider instead of replacing the whole file. Replace gpt-5.4 with whichever OpenAI Responses model you want Codex to use.

Launch Codex by typing codex. All requests will now route through the Pydantic AI Gateway.

SDKs

OpenAI SDK

Use the base URL that matches your Logfire region (gateway-us or gateway-eu).

openai_sdk.py
import openai

client = openai.Client(
    base_url='https://gateway-us.pydantic.dev/proxy/chat/',
    api_key='pylf_v...',
)

response = client.chat.completions.create(
    model='gpt-5.2',
    messages=[{'role': 'user', 'content': 'Hello world'}],
)
print(response.choices[0].message.content)
#> Hello user

Anthropic SDK

Use the base URL that matches your Logfire region (gateway-us or gateway-eu).

anthropic_sdk.py
import anthropic

client = anthropic.Anthropic(
    base_url='https://gateway-us.pydantic.dev/proxy/anthropic/',
    auth_token='pylf_v...',
)

response = client.messages.create(
    max_tokens=1000,
    model='claude-sonnet-4-5',
    messages=[{'role': 'user', 'content': 'Hello world'}],
)
print(response.content[0].text)
#> Hello user

Troubleshooting

Unable to calculate spend

The gateway needs to know the cost of the request in order to provide insights about the spend, and to enforce spending limits. If it’s unable to calculate the cost, it will return a 400 error with the message “Unable to calculate spend”.

When configuring a provider, you need to decide if you want the gateway to block the API key if it’s unable to calculate the cost. If you choose to block the API key, any further requests using that API key will fail.

We are actively working on supporting more providers, and models. If you have a specific provider that you would like to see supported, please let us know on Slack or open an issue on genai-prices.