AI Gateway Text Generation
Text generation is the default modality in AI Gateway. You send a prompt and a model returns text, either all at once or streamed token by token. The same unified API works across hundreds of models, so you can switch providers with a one-line change.
For a step-by-step setup, see the Text Generation quickstart. To browse available models, see Models and Providers.
For SDK options and result handling, see generateText and Python streaming.
Use generateText from the AI SDK to get a complete response. Set the model with a creator/model-name slug:
These examples use AI SDK 7 and the AI SDK for Python beta. Set AI_GATEWAY_API_KEY before running them. See API format differences for setup, request fields, and response handling.
import { generateText } from 'ai';
const { text } = await generateText({
model: "anthropic/claude-sonnet-5",
prompt: "What is the capital of France?",
});
console.log(text);import asyncio
import ai
async def main():
model = ai.get_model("anthropic/claude-sonnet-5")
messages = [ai.user_message("What is the capital of France?")]
async with ai.stream(model, messages) as stream:
async for event in stream:
if isinstance(event, ai.events.TextDelta):
print(event.chunk, end="", flush=True)
print()
asyncio.run(main())import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await client.chat.completions.create({
model: "anthropic/claude-sonnet-5",
messages: [{ "role": "user", "content": "What is the capital of France?" }],
});
console.log(response.choices[0]?.message.content);import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh/v1",
)
response = client.chat.completions.create(
model="anthropic/claude-sonnet-5",
messages=[{"role": "user", "content": "What is the capital of France?"}],
)
print(response.choices[0].message.content)curl --fail-with-body https://ai-gateway.vercel.sh/v1/chat/completions \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh',
});
const response = await client.messages.create({
model: "anthropic/claude-sonnet-5",
messages: [{ "role": "user", "content": "What is the capital of France?" }],
max_tokens: 1024,
});
for (const block of response.content) {
if (block.type === 'text') console.log(block.text);
}import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh",
)
response = client.messages.create(
model="anthropic/claude-sonnet-5",
messages=[{"role": "user", "content": "What is the capital of France?"}],
max_tokens=1024,
)
for block in response.content:
if block.type == "text":
print(block.text)curl --fail-with-body https://ai-gateway.vercel.sh/v1/messages \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "anthropic/claude-sonnet-5",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
],
"max_tokens": 1024
}'import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await client.responses.create({
model: "anthropic/claude-sonnet-5",
input: "What is the capital of France?",
});
console.log(response.output_text);import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh/v1",
)
response = client.responses.create(
model="anthropic/claude-sonnet-5",
input="What is the capital of France?",
)
print(response.output_text)curl --fail-with-body https://ai-gateway.vercel.sh/v1/responses \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5",
"input": "What is the capital of France?"
}'Switching models is a one-line change, for example to anthropic/claude-sonnet-5 or google/gemini-3.1-pro-preview.
For SDK options and result handling, see streamText and Python stream events.
Use streamText to render output as it arrives, which keeps chat and agent interfaces responsive:
These examples use AI SDK 7 and the AI SDK for Python beta. Set AI_GATEWAY_API_KEY before running them. See API format differences for setup, request fields, and response handling.
import { streamText } from 'ai';
const result = streamText({
model: "anthropic/claude-sonnet-5",
prompt: "Write a short poem about the ocean.",
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}import asyncio
import ai
async def main():
model = ai.get_model("anthropic/claude-sonnet-5")
messages = [ai.user_message("Write a short poem about the ocean.")]
async with ai.stream(model, messages) as stream:
async for event in stream:
if isinstance(event, ai.events.TextDelta):
print(event.chunk, end="", flush=True)
print()
asyncio.run(main())import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await client.chat.completions.create({
model: "anthropic/claude-sonnet-5",
messages: [{ "role": "user", "content": "Write a short poem about the ocean." }],
stream: true,
});
for await (const event of response) {
process.stdout.write(event.choices[0]?.delta.content ?? '');
}import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh/v1",
)
response = client.chat.completions.create(
model="anthropic/claude-sonnet-5",
messages=[{"role": "user", "content": "Write a short poem about the ocean."}],
stream=True,
)
for event in response:
if event.choices:
print(event.choices[0].delta.content or "", end="", flush=True)curl --fail-with-body --no-buffer https://ai-gateway.vercel.sh/v1/chat/completions \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5",
"messages": [
{
"role": "user",
"content": "Write a short poem about the ocean."
}
],
"stream": true
}'import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh',
});
const response = await client.messages.create({
model: "anthropic/claude-sonnet-5",
messages: [{ "role": "user", "content": "Write a short poem about the ocean." }],
max_tokens: 1024,
stream: true,
});
for await (const event of response) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text);
}
}import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh",
)
response = client.messages.create(
model="anthropic/claude-sonnet-5",
messages=[{"role": "user", "content": "Write a short poem about the ocean."}],
max_tokens=1024,
stream=True,
)
for event in response:
if event.type == "content_block_delta" and event.delta.type == "text_delta":
print(event.delta.text, end="", flush=True)curl --fail-with-body --no-buffer https://ai-gateway.vercel.sh/v1/messages \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "anthropic/claude-sonnet-5",
"messages": [
{
"role": "user",
"content": "Write a short poem about the ocean."
}
],
"max_tokens": 1024,
"stream": true
}'import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await client.responses.create({
model: "anthropic/claude-sonnet-5",
input: "Write a short poem about the ocean.",
stream: true,
});
for await (const event of response) {
if (event.type === 'response.output_text.delta') {
process.stdout.write(event.delta);
}
}import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh/v1",
)
response = client.responses.create(
model="anthropic/claude-sonnet-5",
input="Write a short poem about the ocean.",
stream=True,
)
for event in response:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)curl --fail-with-body --no-buffer https://ai-gateway.vercel.sh/v1/responses \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5",
"input": "Write a short poem about the ocean.",
"stream": true
}'Connect a model to functions in your application. See Tool Use for examples in every supported API format, argument schemas, and returning tool results.
For SDK options and result handling, see AI SDK structured output and Python structured output.
Use generateText with Output.object to get typed, schema-validated data instead of free-form text:
import { generateText, Output } from 'ai';
import { z } from 'zod';
const { output } = await generateText({
output: Output.object({
schema: z.object({ name: z.string(), age: z.number().int(), city: z.string() }),
}),
model: "anthropic/claude-sonnet-5",
prompt: "Extract: John is 30 years old and lives in NYC.",
});
console.log(output);import asyncio
import ai
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
city: str
async def main():
model = ai.get_model("anthropic/claude-sonnet-5")
messages = [ai.user_message("Extract: John is 30 years old and lives in NYC.")]
async with ai.stream(model, messages, output_type=Person) as stream:
async for event in stream:
if isinstance(event, ai.events.TextDelta):
print(event.chunk, end="", flush=True)
print(stream.output)
asyncio.run(main())import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await client.chat.completions.create({
response_format: {
type: 'json_schema',
json_schema: {
name: 'person',
strict: true,
schema: {
type: 'object',
properties: {
name: {
type: 'string',
},
age: {
type: 'integer',
},
city: {
type: 'string',
},
},
required: ['name', 'age', 'city'],
additionalProperties: false,
},
},
},
model: 'anthropic/claude-sonnet-5',
messages: [
{
role: 'user',
content: 'Extract: John is 30 years old and lives in NYC.',
},
],
});
console.log(response.choices[0]?.message.content);import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh/v1",
)
response = client.chat.completions.create(
response_format={"type": "json_schema", "json_schema": {"name": "person", "strict": True, "schema": {"type": "object", "properties": {"name": {"type": "string"}, "age": {"type": "integer"}, "city": {"type": "string"}}, "required": ["name", "age", "city"], "additionalProperties": False}}},
model="anthropic/claude-sonnet-5",
messages=[{"role": "user", "content": "Extract: John is 30 years old and lives in NYC."}],
)
print(response.choices[0].message.content)curl --fail-with-body https://ai-gateway.vercel.sh/v1/chat/completions \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5",
"messages": [
{
"role": "user",
"content": "Extract: John is 30 years old and lives in NYC."
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"city": {
"type": "string"
}
},
"required": [
"name",
"age",
"city"
],
"additionalProperties": false
}
}
}
}'import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh',
});
const response = await client.messages.create({
output_config: {
format: {
type: 'json_schema',
schema: {
type: 'object',
properties: {
name: {
type: 'string',
},
age: {
type: 'integer',
},
city: {
type: 'string',
},
},
required: ['name', 'age', 'city'],
additionalProperties: false,
},
},
},
model: 'anthropic/claude-sonnet-5',
messages: [
{
role: 'user',
content: 'Extract: John is 30 years old and lives in NYC.',
},
],
max_tokens: 1024,
});
for (const block of response.content) {
if (block.type === 'text') console.log(block.text);
}import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh",
)
response = client.messages.create(
output_config={"format": {"type": "json_schema", "schema": {"type": "object", "properties": {"name": {"type": "string"}, "age": {"type": "integer"}, "city": {"type": "string"}}, "required": ["name", "age", "city"], "additionalProperties": False}}},
model="anthropic/claude-sonnet-5",
messages=[{"role": "user", "content": "Extract: John is 30 years old and lives in NYC."}],
max_tokens=1024,
)
for block in response.content:
if block.type == "text":
print(block.text)curl --fail-with-body https://ai-gateway.vercel.sh/v1/messages \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "anthropic/claude-sonnet-5",
"messages": [
{
"role": "user",
"content": "Extract: John is 30 years old and lives in NYC."
}
],
"max_tokens": 1024,
"output_config": {
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"city": {
"type": "string"
}
},
"required": [
"name",
"age",
"city"
],
"additionalProperties": false
}
}
}
}'import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await client.responses.create({
text: {
format: {
type: 'json_schema',
name: 'person',
strict: true,
schema: {
type: 'object',
properties: {
name: {
type: 'string',
},
age: {
type: 'integer',
},
city: {
type: 'string',
},
},
required: ['name', 'age', 'city'],
additionalProperties: false,
},
},
},
model: 'anthropic/claude-sonnet-5',
input: 'Extract: John is 30 years old and lives in NYC.',
});
console.log(response.output_text);import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AI_GATEWAY_API_KEY"],
base_url="https://ai-gateway.vercel.sh/v1",
)
response = client.responses.create(
text={"format": {"type": "json_schema", "name": "person", "strict": True, "schema": {"type": "object", "properties": {"name": {"type": "string"}, "age": {"type": "integer"}, "city": {"type": "string"}}, "required": ["name", "age", "city"], "additionalProperties": False}}},
model="anthropic/claude-sonnet-5",
input="Extract: John is 30 years old and lives in NYC.",
)
print(response.output_text)curl --fail-with-body https://ai-gateway.vercel.sh/v1/responses \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5",
"input": "Extract: John is 30 years old and lives in NYC.",
"text": {
"format": {
"type": "json_schema",
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"city": {
"type": "string"
}
},
"required": [
"name",
"age",
"city"
],
"additionalProperties": false
}
}
}
}'- Text Generation quickstart to make your first request
- Models and Providers to choose a model
- Provider options for routing and fallbacks
- Reasoning to enable step-by-step thinking
Was this helpful?