Skip to content

Commit 40e5ee3

Browse files
laisbscKludexsamuelcolvinDouweMsummerscope
authored
docs/ai_gateway: add new gateway.md page (#3402)
Co-authored-by: Marcelo Trylesinski <[email protected]> Co-authored-by: Samuel Colvin <[email protected]> Co-authored-by: Douwe Maan <[email protected]> Co-authored-by: summerscope <[email protected]>
1 parent e061744 commit 40e5ee3

File tree

4 files changed

+193
-7
lines changed

4 files changed

+193
-7
lines changed

docs/.overrides/main.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends "base.html" %}
2+
3+
{% block announce %}
4+
<strong>
5+
<a href="/gateway" target="_blank">Pydantic AI Gateway</a> is now available! 🚀
6+
Enterprise-ready AI model routing: One key for all your models with real-time monitoring and budget control that works.
7+
</strong>
8+
{% endblock %}

docs/gateway.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
title: Pydantic AI Gateway
3+
status: new
4+
---
5+
6+
# Pydantic AI Gateway
7+
8+
**[Pydantic AI Gateway](https://pydantic.dev/gateway)** (PAIG) is a unified interface for accessing multiple AI providers with a single key. Features include built-in OpenTelemetry observability, real-time cost monitoring, failover management, and native integration with the other tools in the [Pydantic stack](https://pydantic.dev/).
9+
10+
!!! note "Free while in Beta"
11+
The Pydantic AI Gateway is currently in Beta. You can bring your own key (BYOK) or buy inference through the Gateway (we will eat the card fee for now).
12+
13+
Sign up at [gateway.pydantic.dev](https://gateway.pydantic.dev/).
14+
15+
!!! question "Questions?"
16+
For questions and feedback, contact us on [Slack](https://logfire.pydantic.dev/docs/join-slack/).
17+
18+
## Documentation Integration
19+
20+
To help you get started with [Pydantic AI Gateway](https://gateway.pydantic.dev), 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.
21+
22+
## Key features
23+
24+
- **API key management**: access multiple LLM providers with a single Gateway key.
25+
- **Cost Limits**: set spending limits at project, user, and API key levels with daily, weekly, and monthly caps.
26+
- **BYOK and managed providers:** Bring your own API keys (BYOK) from LLM providers, or pay for inference directly through the platform.
27+
- **Multi-provider support:** Access models from OpenAI, Anthropic, Google Vertex, Groq, and AWS Bedrock. _More providers coming soon_.
28+
- **Backend observability:** Log every request through [Pydantic Logfire](https://pydantic.dev/logfire) or any OpenTelemetry backend (_coming soon_).
29+
- **Zero translation**: Unlike traditional AI gateways that translate everything to one common schema, PAIG allows requests to flow through directly in each provider's native format. This gives you immediate access to the new model features as soon as they are released.
30+
- **Open source with self-hosting**: PAIG's core is [open source](https://github.com/pydantic/pydantic-ai-gateway/) (under [AGPL-3.0](https://www.gnu.org/licenses/agpl-3.0.en.html)), allowing self-hosting with file-based configuration, instead of using the managed service.
31+
- **Enterprise ready**: Includes SSO (with OIDC support), granular permissions, and flexible deployment options. Deploy to your Cloudflare account, or run on-premises with our [consulting support](https://pydantic.dev/contact).
32+
33+
```python {title="hello_world.py"}
34+
from pydantic_ai import Agent
35+
36+
agent = Agent('gateway/openai:gpt-5')
37+
38+
result = agent.run_sync('Where does "hello world" come from?')
39+
print(result.output)
40+
"""
41+
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
42+
"""
43+
```
44+
# Quick Start
45+
46+
This section contains instructions on how to set up your account and run your app with Pydantic AI Gateway credentials.
47+
48+
## Create an account
49+
50+
Using your GitHub or Google account, sign in at [gateway.pydantic.dev](https://gateway.pydantic.dev).
51+
Choose a name for your organization (or accept the default). You will automatically be assigned the Admin role.
52+
53+
A default project will be created for you. You can choose to use it, or create a new one on the [Projects](https://gateway.pydantic.dev/admin/projects) page.
54+
55+
## Add **Providers**
56+
There are two ways to use Providers in the Pydantic AI Gateway: you can bring your own key (BYOK) or buy inference through the platform.
57+
58+
### Bringing your own API key (BYOK)
59+
60+
On the [Providers](https://gateway.pydantic.dev/admin/providers) page, fill in the form to add a provider. Paste your API key into the form under Credentials, and make sure to **select the Project that will be associated to this provider**. It is possible to add multiple keys from the same provider.
61+
62+
### Use Built-in Providers
63+
Go to the Billing page, add a payment method, and purchase $15 in credits to activate built-in providers. This gives you single-key access to all available models from OpenAI, Anthropic, Google Vertex, AWS Bedrock, and Groq.
64+
65+
## Grant access to your team
66+
On the [Users](https://gateway.pydantic.dev/admin/users) page, create an invitation and share the URL with your team to allow them to access the project.
67+
68+
## Create Gateway project keys
69+
On the Keys page, Admins can create project keys which are not affected by spending limits. Users can only create personal keys, that will inherit spending caps from both User and Project levels, whichever is more restrictive.
70+
71+
# Usage
72+
After setting up your account with the instructions above, you will be able to make an AI model request with the Pydantic AI Gateway.
73+
The code snippets below show how you can use PAIG with different frameworks and SDKs.
74+
You can add `gateway/` as prefix on every known provider that
75+
76+
To use different models, change the model string `gateway/<api_format>:<model_name>` to other models offered by the supported providers.
77+
78+
Examples of providers and models that can be used are:
79+
80+
| **Provider** | **API Format** | **Example Model** |
81+
| --- |-----------------|------------------------------------------|
82+
| OpenAI | `openai` | `gateway/openai:gpt-5` |
83+
| Anthropic | `anthropic` | `gateway/anthropic:claude-sonnet-4-5` |
84+
| Google Vertex | `google-vertex` | `gateway/google-vertex:gemini-2.5-flash` |
85+
| Groq | `groq` | `gateway/groq:openai/gpt-oss-120b` |
86+
| AWS Bedrock | `bedrock` | `gateway/bedrock:amazon.nova-micro-v1:0` |
87+
88+
## Pydantic AI
89+
Before you start, make sure you are on version 1.16 or later of `pydantic-ai`. To update to the latest version run:
90+
91+
=== "uv"
92+
93+
```bash
94+
uv sync -P pydantic-ai
95+
```
96+
97+
=== "pip"
98+
99+
```bash
100+
pip install -U pydantic-ai
101+
```
102+
103+
Set the `PYDANTIC_AI_GATEWAY_API_KEY` environment variable to your Gateway API key:
104+
105+
```bash
106+
export PYDANTIC_AI_GATEWAY_API_KEY="YOUR_PAIG_TOKEN"
107+
```
108+
109+
You can access multiple models with the same API key, as shown in the code snippet below.
110+
111+
```python {title="hello_world.py"}
112+
from pydantic_ai import Agent
113+
114+
agent = Agent('gateway/openai:gpt-5')
115+
116+
result = agent.run_sync('Where does "hello world" come from?')
117+
print(result.output)
118+
"""
119+
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
120+
"""
121+
```
122+
123+
124+
## Claude Code
125+
Before you start, log out of Claude Code using `/logout`.
126+
127+
Set your gateway credentials as environment variables:
128+
129+
```bash
130+
export ANTHROPIC_BASE_URL="https://gateway.pydantic.dev/proxy/anthropic"
131+
export ANTHROPIC_AUTH_TOKEN="YOUR_PAIG_TOKEN"
132+
```
133+
134+
Replace `YOUR_PAIG_TOKEN` with the API key from the Keys page.
135+
136+
Launch Claude Code by typing `claude`. All requests will now route through the Pydantic AI Gateway.
137+
138+
## SDKs
139+
140+
### OpenAI SDK
141+
142+
```python {title="openai_sdk.py" test="skip"}
143+
import openai
144+
145+
client = openai.Client(
146+
base_url='https://gateway.pydantic.dev/proxy/chat/',
147+
api_key='paig_...',
148+
)
149+
150+
response = client.chat.completions.create(
151+
model='gpt-5',
152+
messages=[{'role': 'user', 'content': 'Hello world'}],
153+
)
154+
print(response.choices[0].message.content)
155+
#> Hello user
156+
```
157+
158+
### Anthropic SDK
159+
160+
```python {title="anthropic_sdk.py" test="skip"}
161+
import anthropic
162+
163+
client = anthropic.Anthropic(
164+
base_url='https://gateway.pydantic.dev/proxy/anthropic/',
165+
auth_token='paig_...',
166+
)
167+
168+
response = client.messages.create(
169+
max_tokens=1000,
170+
model='claude-sonnet-4-5',
171+
messages=[{'role': 'user', 'content': 'Hello world'}],
172+
)
173+
print(response.content[0].text)
174+
#> Hello user
175+
```

mkdocs.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ nav:
1414
- install.md
1515
- help.md
1616
- troubleshooting.md
17+
- Pydantic AI Gateway: gateway.md
1718

1819
- Documentation:
1920
- Core Concepts:
@@ -87,14 +88,14 @@ nav:
8788
- Integrations:
8889
- Debugging & Monitoring with Pydantic Logfire: logfire.md
8990
- Durable Execution:
90-
- Overview: durable_execution/overview.md
91-
- Temporal: durable_execution/temporal.md
92-
- DBOS: durable_execution/dbos.md
93-
- Prefect: durable_execution/prefect.md
91+
- Overview: durable_execution/overview.md
92+
- Temporal: durable_execution/temporal.md
93+
- DBOS: durable_execution/dbos.md
94+
- Prefect: durable_execution/prefect.md
9495
- UI Event Streams:
95-
- Overview: ui/overview.md
96-
- AG-UI: ui/ag-ui.md
97-
- Vercel AI: ui/vercel-ai.md
96+
- Overview: ui/overview.md
97+
- AG-UI: ui/ag-ui.md
98+
- Vercel AI: ui/vercel-ai.md
9899
- Agent2Agent (A2A): a2a.md
99100

100101
- Related Packages:
@@ -233,6 +234,7 @@ theme:
233234
- navigation.sections
234235
- navigation.tracking
235236
- toc.follow
237+
- announce.dismiss
236238
logo: "img/logo-white.svg"
237239
favicon: "favicon.ico"
238240

tests/test_examples.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def print(self, *args: Any, **kwargs: Any) -> None:
179179
env.set('MOONSHOTAI_API_KEY', 'testing')
180180
env.set('DEEPSEEK_API_KEY', 'testing')
181181
env.set('OVHCLOUD_API_KEY', 'testing')
182+
env.set('PYDANTIC_AI_GATEWAY_API_KEY', 'testing')
182183

183184
prefix_settings = example.prefix_settings()
184185
opt_test = prefix_settings.get('test', '')

0 commit comments

Comments
 (0)