Skip to main content

Overview

ChatVercel provides access to Vercel AI Gateway, an OpenAI-compatible API that routes requests to multiple LLM providers including OpenAI, Anthropic, Google, Meta, Mistral, Cohere, DeepSeek, xAI, and more. It includes features like rate limiting, caching, and monitoring.

Basic Usage

Configuration

Required Parameters

str
required
Model identifier in format provider/model. Available providers and models:OpenAI:
  • openai/gpt-4o, openai/gpt-4.1-mini, openai/gpt-5, openai/o3-mini
Anthropic:
  • anthropic/claude-sonnet-4.5, anthropic/claude-opus-4.1, anthropic/claude-haiku-4.5
Google:
  • google/gemini-2.5-flash, google/gemini-2.5-pro
Meta:
  • meta/llama-4-maverick, meta/llama-4-scout, meta/llama-3.3-70b
Mistral:
  • mistral/magistral-medium, mistral/mistral-large, mistral/codestral
DeepSeek:
  • deepseek/deepseek-v3.2-exp, deepseek/deepseek-r1
xAI:
  • xai/grok-4, xai/grok-3-mini-fast
And many more - see full list in source code.

Model Parameters

float
default:"None"
Sampling temperature (0.0 to 2.0). Controls randomness in responses.
int
default:"None"
Maximum tokens to generate.
float
default:"None"
Nucleus sampling parameter (0.0 to 1.0).
list[str]
List of reasoning model patterns that require prompt-based JSON extraction instead of native structured output.

Client Parameters

str
default:"None"
Vercel API key for authentication.
Get your API key from Vercel Dashboard
str
default:"https://ai-gateway.vercel.sh/v1"
Vercel AI Gateway endpoint URL.
float
default:"None"
Request timeout in seconds or httpx.Timeout object.
int
default:"5"
Maximum number of retries for failed requests.
dict
default:"None"
Additional headers to include in all requests.
dict
default:"None"
Additional query parameters for all requests.
httpx.AsyncClient
default:"None"
Custom async HTTP client instance.

Gateway-Specific Parameters

dict
default:"None"
Provider routing options for the AI Gateway. Use this to control which providers are used and in what order.Example:

Advanced Usage

Provider Routing

Control which providers handle your requests:

Structured Output

Automatic structured output with provider-specific optimizations:
ChatVercel automatically handles different structured output methods:
  • OpenAI models: Native JSON schema
  • Anthropic models: Prompt-based extraction
  • Google models: Gemini-optimized schema
  • Reasoning models: Prompt-based extraction

Multiple Providers

Access different providers through the same interface:

Reasoning Models

Environment Setup

.env

Error Handling

Properties

provider

Returns the provider name: "vercel"

name

Returns the model identifier.

Methods

get_client()

Returns an AsyncOpenAI client configured for Vercel AI Gateway.

ainvoke()

Asynchronously invoke the model with messages.

Parameters

  • messages (list[BaseMessage]): List of messages
  • output_format (type[T] | None): Optional Pydantic model for structured output

Returns

ChatInvokeCompletion[T] | ChatInvokeCompletion[str] with:
  • completion: Response content
  • usage: Token usage including:
    • prompt_tokens: Input tokens
    • completion_tokens: Output tokens
    • total_tokens: Total tokens used
    • prompt_cached_tokens: Cached tokens (when available)
  • stop_reason: Completion reason

Gateway Features

Rate Limiting

  • Built-in rate limiting across providers
  • Automatic request queuing
  • Configurable limits per model

Caching

  • Response caching for repeated requests
  • Reduced latency and costs
  • Automatic cache invalidation

Monitoring

  • Request tracing and analytics
  • Performance metrics
  • Error tracking
  • Usage statistics

Provider Fallback

  • Automatic fallback to alternative providers
  • High availability
  • Load balancing

Schema Optimization

Provider-Specific Handling

ChatVercel automatically optimizes schemas for different providers: Gemini Models:
  • Removes additionalProperties
  • Resolves $ref references
  • Handles empty object types
  • Cleans unsupported properties
Anthropic Models:
  • Prompt-based JSON extraction
  • Custom schema instructions
  • Markdown code block parsing
Reasoning Models:
  • Prompt-based extraction
  • No native structured output
  • JSON validation and cleanup

Supported Models

The implementation supports 150+ models across providers:
  • OpenAI: GPT-4o, GPT-5, o3-mini, o4-mini
  • Anthropic: Claude Sonnet 4.5, Opus 4.1, Haiku 4.5
  • Google: Gemini 2.5 Flash, Gemini 2.5 Pro
  • Meta: Llama 4 Maverick, Llama 4 Scout, Llama 3.3
  • Mistral: Magistral, Mistral Large, Codestral
  • DeepSeek: DeepSeek v3.2, DeepSeek R1
  • xAI: Grok 4, Grok 3 Mini
  • Cohere: Command A, Command R+
  • Amazon: Nova Pro, Nova Lite
  • And many more…
See the source code for the complete list of available models.

Benefits

Unified Interface

  • Single API for multiple providers
  • Consistent error handling
  • Standardized token counting

Cost Optimization

  • Route to cheapest available provider
  • Automatic caching reduces costs
  • Pay only for what you use

Reliability

  • Built-in retries and fallbacks
  • High availability
  • Enterprise-grade infrastructure

Flexibility

  • Easy provider switching
  • A/B testing different models
  • Multi-provider redundancy