Skip to content

Commit 0597ef1

Browse files
authored
Merge pull request #23 from vercel-labs/add-gateway-provider
Add AI Gateway provider
2 parents 45fd888 + affdf93 commit 0597ef1

14 files changed

Lines changed: 137 additions & 86 deletions

File tree

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ uv add vercel-ai-sdk
99
```
1010

1111
```python
12-
import os
1312
import vercel_ai_sdk as ai
1413

1514
@ai.tool
@@ -27,11 +26,7 @@ async def agent(llm, query):
2726
tools=[talk_to_mothership],
2827
)
2928

30-
llm = ai.openai.OpenAIModel(
31-
model="anthropic/claude-opus-4.6",
32-
base_url="https://ai-gateway.vercel.sh/v1",
33-
api_key=os.environ["AI_GATEWAY_API_KEY"],
34-
)
29+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
3530

3631
async for msg in ai.run(agent, llm, "When will the robots take over?"):
3732
print(msg.text_delta, end="")
@@ -204,23 +199,31 @@ Three event types are tracked:
204199
#### LLM Providers
205200

206201
```python
207-
# OpenAI-compatible (including Vercel AI Gateway)
208-
llm = ai.openai.OpenAIModel(
202+
# Vercel AI Gateway (recommended)
203+
# Uses AI_GATEWAY_API_KEY env var by default
204+
llm = ai.ai_gateway.GatewayModel(
209205
model="anthropic/claude-opus-4.6",
210-
base_url="https://ai-gateway.vercel.sh/v1",
211-
api_key=os.environ["AI_GATEWAY_API_KEY"],
212206
thinking=True, # enable reasoning output
213207
budget_tokens=10000, # or reasoning_effort="medium"
214208
)
215209

216-
# Anthropic (native client)
210+
# OpenAI (direct)
211+
llm = ai.openai.OpenAIModel(
212+
model="gpt-4o",
213+
thinking=True,
214+
reasoning_effort="medium",
215+
)
216+
217+
# Anthropic (direct)
217218
llm = ai.anthropic.AnthropicModel(
218-
model="claude-opus-4.6-20250916",
219+
model="claude-opus-4-6-20250916",
219220
thinking=True,
220221
budget_tokens=10000,
221222
)
222223
```
223224

225+
The gateway provider automatically routes Anthropic models through the native Anthropic API for full feature support, and falls back to the OpenAI-compatible endpoint for structured output and non-Anthropic models.
226+
224227
#### MCP
225228

226229
```python

examples/fastapi-vite/backend/agent.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Agent logic for the chat demo."""
22

3-
import os
43
from typing import Any
54

65
import vercel_ai_sdk as ai
@@ -14,11 +13,7 @@ async def talk_to_mothership(question: str) -> str:
1413

1514
def get_llm() -> ai.LanguageModel:
1615
"""Create the LLM instance."""
17-
return ai.openai.OpenAIModel(
18-
model="anthropic/claude-sonnet-4",
19-
base_url="https://ai-gateway.vercel.sh/v1",
20-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
21-
)
16+
return ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
2217

2318

2419
TOOLS: list[ai.Tool[..., Any]] = [talk_to_mothership]

examples/multiagent-textual/server.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import asyncio
1414
import json
15-
import os
1615
import warnings
1716
from typing import Any
1817

@@ -181,11 +180,7 @@ async def ws_endpoint(websocket: fastapi.WebSocket) -> None:
181180
await websocket.accept()
182181
print("Client connected")
183182

184-
llm = ai.anthropic.AnthropicModel(
185-
model="anthropic/claude-haiku-4.5",
186-
base_url="https://ai-gateway.vercel.sh",
187-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
188-
)
183+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
189184

190185
result = ai.run(multiagent, llm, "When will the robots take over?")
191186

examples/samples/agent.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
"""Coding agent with local filesystem tools."""
22

33
import asyncio
4-
import os
54

65
import vercel_ai_sdk as ai
76
import vercel_ai_sdk.agent as agent
87

98

109
async def main() -> None:
11-
llm = ai.openai.OpenAIModel(
12-
model="anthropic/claude-sonnet-4-20250514",
13-
base_url="https://ai-gateway.vercel.sh/v1",
14-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
15-
)
10+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
1611

1712
coding_agent = agent.Agent(
1813
model=llm,

examples/samples/custom_loop.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Custom agent loop with @ai.stream and manual tool execution."""
22

33
import asyncio
4-
import os
54
from collections.abc import AsyncGenerator
65
from typing import Any
76

@@ -64,11 +63,7 @@ async def agent(llm: ai.LanguageModel, user_query: str) -> ai.StreamResult:
6463

6564

6665
async def main() -> None:
67-
llm = ai.anthropic.AnthropicModel(
68-
model="anthropic/claude-sonnet-4",
69-
base_url="https://ai-gateway.vercel.sh",
70-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
71-
)
66+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
7267

7368
async for msg in ai.run(
7469
agent, llm, "What's the weather and population of New York and Los Angeles?"

examples/samples/hooks.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Human-in-the-loop approval hooks."""
22

33
import asyncio
4-
import os
54

65
import pydantic
76

@@ -57,11 +56,7 @@ async def graph(llm: ai.LanguageModel, query: str) -> ai.StreamResult:
5756

5857

5958
async def main() -> None:
60-
llm = ai.openai.OpenAIModel(
61-
model="anthropic/claude-sonnet-4-20250514",
62-
base_url="https://ai-gateway.vercel.sh/v1",
63-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
64-
)
59+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
6560

6661
async for msg in ai.run(graph, llm, "When will the robots take over?"):
6762
# Hook parts arrive as pending, waiting for resolution

examples/samples/mcp_tools.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ async def context7_agent(llm: ai.LanguageModel, user_query: str) -> ai.StreamRes
3030

3131

3232
async def main() -> None:
33-
llm = ai.openai.OpenAIModel(
34-
model="openai/gpt-4.1",
35-
base_url="https://ai-gateway.vercel.sh/v1",
36-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
37-
)
33+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
3834

3935
async for msg in ai.run(
4036
context7_agent, llm, "How do I create middleware in Next.js?"

examples/samples/multiagent.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Multi-agent: parallel execution with labels, then summarization."""
22

33
import asyncio
4-
import os
54

65
import vercel_ai_sdk as ai
76

@@ -54,11 +53,7 @@ async def multiagent(llm: ai.LanguageModel, user_query: str) -> ai.StreamResult:
5453

5554

5655
async def main() -> None:
57-
llm = ai.anthropic.AnthropicModel(
58-
model="anthropic/claude-haiku-4.5",
59-
base_url="https://ai-gateway.vercel.sh",
60-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
61-
)
56+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
6257

6358
async for msg in ai.run(multiagent, llm, "Process the number 5"):
6459
if msg.text_delta:

examples/samples/simple.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import os
32

43
import vercel_ai_sdk as ai
54

@@ -21,11 +20,7 @@ async def agent(llm: ai.LanguageModel, user_query: str) -> ai.StreamResult:
2120

2221

2322
async def main() -> None:
24-
llm = ai.openai.OpenAIModel(
25-
model="anthropic/claude-sonnet-4",
26-
base_url="https://ai-gateway.vercel.sh/v1",
27-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
28-
)
23+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
2924

3025
async for msg in ai.run(agent, llm, "When will the robots take over?"):
3126
if msg.text_delta:

examples/samples/streaming_tool.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Streaming from inside a tool via runtime.put_message()."""
22

33
import asyncio
4-
import os
54

65
import vercel_ai_sdk as ai
76

@@ -34,11 +33,7 @@ async def agent(llm: ai.LanguageModel, user_query: str) -> ai.StreamResult:
3433

3534

3635
async def main() -> None:
37-
llm = ai.openai.OpenAIModel(
38-
model="anthropic/claude-sonnet-4",
39-
base_url="https://ai-gateway.vercel.sh/v1",
40-
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
41-
)
36+
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")
4237

4338
async for msg in ai.run(agent, llm, "When will the robots take over?"):
4439
if msg.label == "tool_progress":

0 commit comments

Comments
 (0)