Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ uv add vercel-ai-sdk
```

```python
import os
import vercel_ai_sdk as ai

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

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

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

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

# Anthropic (native client)
# OpenAI (direct)
llm = ai.openai.OpenAIModel(
model="gpt-4o",
thinking=True,
reasoning_effort="medium",
)

# Anthropic (direct)
llm = ai.anthropic.AnthropicModel(
model="claude-opus-4.6-20250916",
model="claude-opus-4-6-20250916",
thinking=True,
budget_tokens=10000,
)
```

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.

#### MCP

```python
Expand Down
7 changes: 1 addition & 6 deletions examples/fastapi-vite/backend/agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Agent logic for the chat demo."""

import os
from typing import Any

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

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


TOOLS: list[ai.Tool[..., Any]] = [talk_to_mothership]
Expand Down
7 changes: 1 addition & 6 deletions examples/multiagent-textual/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import asyncio
import json
import os
import warnings
from typing import Any

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

llm = ai.anthropic.AnthropicModel(
model="anthropic/claude-haiku-4.5",
base_url="https://ai-gateway.vercel.sh",
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
)
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")

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

Expand Down
7 changes: 1 addition & 6 deletions examples/samples/agent.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
"""Coding agent with local filesystem tools."""

import asyncio
import os

import vercel_ai_sdk as ai
import vercel_ai_sdk.agent as agent


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

coding_agent = agent.Agent(
model=llm,
Expand Down
7 changes: 1 addition & 6 deletions examples/samples/custom_loop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Custom agent loop with @ai.stream and manual tool execution."""

import asyncio
import os
from collections.abc import AsyncGenerator
from typing import Any

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


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

async for msg in ai.run(
agent, llm, "What's the weather and population of New York and Los Angeles?"
Expand Down
7 changes: 1 addition & 6 deletions examples/samples/hooks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Human-in-the-loop approval hooks."""

import asyncio
import os

import pydantic

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


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

async for msg in ai.run(graph, llm, "When will the robots take over?"):
# Hook parts arrive as pending, waiting for resolution
Expand Down
6 changes: 1 addition & 5 deletions examples/samples/mcp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ async def context7_agent(llm: ai.LanguageModel, user_query: str) -> ai.StreamRes


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

async for msg in ai.run(
context7_agent, llm, "How do I create middleware in Next.js?"
Expand Down
7 changes: 1 addition & 6 deletions examples/samples/multiagent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Multi-agent: parallel execution with labels, then summarization."""

import asyncio
import os

import vercel_ai_sdk as ai

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


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

async for msg in ai.run(multiagent, llm, "Process the number 5"):
if msg.text_delta:
Expand Down
7 changes: 1 addition & 6 deletions examples/samples/simple.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import os

import vercel_ai_sdk as ai

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


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

async for msg in ai.run(agent, llm, "When will the robots take over?"):
if msg.text_delta:
Expand Down
7 changes: 1 addition & 6 deletions examples/samples/streaming_tool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Streaming from inside a tool via runtime.put_message()."""

import asyncio
import os

import vercel_ai_sdk as ai

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


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

async for msg in ai.run(agent, llm, "When will the robots take over?"):
if msg.label == "tool_progress":
Expand Down
14 changes: 1 addition & 13 deletions examples/samples/structured_output.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import os

import pydantic

Expand All @@ -15,18 +14,7 @@ class WeatherForecast(pydantic.BaseModel):


async def main() -> None:
# OpenAI-compatible provider
# llm = ai.openai.OpenAIModel(
# model="anthropic/claude-opus-4.6",
# base_url="https://ai-gateway.vercel.sh/v1",
# api_key=os.environ.get("AI_GATEWAY_API_KEY"),
# )

# Anthropic provider
llm = ai.anthropic.AnthropicModel(
model="claude-opus-4-6",
api_key=os.environ.get("ANTHROPIC_API_KEY"),
)
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")

messages = ai.make_messages(
system="You are a weather assistant. Respond with realistic weather data.",
Expand Down
8 changes: 1 addition & 7 deletions examples/temporal-durable/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
from __future__ import annotations

import dataclasses
import os
from typing import Any

import temporalio.activity

import vercel_ai_sdk as ai
import vercel_ai_sdk.anthropic

# ── Tool activities (one per tool, plain functions) ───────────────

Expand Down Expand Up @@ -48,11 +46,7 @@ class LLMCallResult:
@temporalio.activity.defn(name="llm_call")
async def llm_call_activity(params: LLMCallParams) -> LLMCallResult:
"""Call the LLM, drain the stream, return the final message."""
llm = ai.anthropic.AnthropicModel(
model="anthropic/claude-sonnet-4",
base_url="https://ai-gateway.vercel.sh",
api_key=os.environ.get("AI_GATEWAY_API_KEY"),
)
llm = ai.ai_gateway.GatewayModel(model="anthropic/claude-opus-4.6")

messages = [ai.Message.model_validate(m) for m in params.messages]
tools = [ai.ToolSchema.model_validate(t) for t in params.tool_schemas]
Expand Down
3 changes: 2 additions & 1 deletion src/vercel_ai_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import ai_sdk_ui, anthropic, mcp, openai
from . import ai_gateway, ai_sdk_ui, anthropic, mcp, openai
from .core.checkpoint import Checkpoint
from .core.hooks import Hook, hook
from .core.llm import LanguageModel
Expand Down Expand Up @@ -61,6 +61,7 @@
"make_messages",
"hook",
# Submodules
"ai_gateway",
"anthropic",
"mcp",
"openai",
Expand Down
Loading