Skip to content
Docs

Anthropic Messages Streaming with AI Gateway

Set stream: true on a Messages request to receive tokens as the model produces them, instead of waiting for the complete response.

Create a streaming message that delivers tokens as they are generated.

stream.ts
import Anthropic from '@anthropic-ai/sdk';
 
const apiKey = process.env.AI_GATEWAY_API_KEY || process.env.VERCEL_OIDC_TOKEN;
 
const anthropic = new Anthropic({
  apiKey,
  baseURL: 'https://ai-gateway.vercel.sh',
});
 
const stream = await anthropic.messages.create({
  model: 'anthropic/claude-opus-5',
  max_tokens: 150,
  messages: [
    {
      role: 'user',
      content: 'Write a one-sentence bedtime story about a unicorn.',
    },
  ],
  temperature: 0.7,
  stream: true,
});
 
for await (const event of stream) {
  if (event.type === 'content_block_delta') {
    if (event.delta.type === 'text_delta') {
      process.stdout.write(event.delta.text);
    }
  }
}
stream.py
import os
import anthropic
 
api_key = os.getenv('AI_GATEWAY_API_KEY') or os.getenv('VERCEL_OIDC_TOKEN')
 
client = anthropic.Anthropic(
    api_key=api_key,
    base_url='https://ai-gateway.vercel.sh'
)
 
with client.messages.stream(
    model='anthropic/claude-opus-5',
    max_tokens=150,
    messages=[
        {
            'role': 'user',
            'content': 'Write a one-sentence bedtime story about a unicorn.'
        }
    ],
    temperature=0.7,
) as stream:
    for text in stream.text_stream:
        print(text, end='', flush=True)
stream.sh
curl -X POST "https://ai-gateway.vercel.sh/v1/messages" \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-opus-5",
    "max_tokens": 150,
    "messages": [
      {
        "role": "user",
        "content": "Write a one-sentence bedtime story about a unicorn."
      }
    ],
    "temperature": 0.7,
    "stream": true
  }'

Streaming responses use Server-Sent Events (SSE). The key event types are:

  • message_start - Initial message metadata
  • content_block_start - Start of a content block (text, tool use, etc.)
  • content_block_delta - Incremental content updates
  • content_block_stop - End of a content block
  • message_delta - Final message metadata (stop reason, usage)
  • message_stop - End of the message
  • Tool calling - Stream a response that calls your functions
  • Extended thinking - Configure how much Claude thinks before answering
  • Advanced - Web search, provider timeouts, and automatic caching
Last updated September 8, 2026

Was this helpful?

supported.