Observability for Vercel AI Gateway with Opik

Vercel AI Gateway provides a unified interface to access multiple AI providers with edge-optimized performance, built-in caching, and comprehensive analytics. It’s designed to work seamlessly with Vercel’s edge infrastructure.

Gateway Overview

Vercel AI Gateway provides enterprise-grade features for managing AI API access, including:

  • Unified API: Access hundreds of models across providers via Vercel’s AI Gateway with minimal code changes
  • Transparent Pricing: No markup on tokens with “Bring Your Own Key” support
  • Automatic Failover: High reliability with requests routed to alternate providers if primary is unavailable
  • Low Latency: Sub-20ms routing latency through Vercel’s global edge network
  • Intelligent Caching: Reduce costs and improve response times with smart caching

Account Setup

Comet provides a hosted version of the Opik platform. Simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Getting Started

Installation

First, ensure you have both opik and openai packages installed:

$pip install opik openai

Configuring Opik

Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:

  • CLI configuration: opik configure
  • Code configuration: opik.configure()
  • Self-hosted vs Cloud vs Enterprise setup
  • Configuration files and environment variables

Configuring Vercel AI Gateway

You’ll need to set up the Vercel AI Gateway in your Vercel project. Follow the Vercel AI Gateway setup guide to configure your gateway.

Set your API keys as environment variables:

$export VERCEL_AI_GATEWAY_URL="YOUR_VERCEL_AI_GATEWAY_URL"
>export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"

Or set them programmatically:

1import os
2import getpass
3
4if "VERCEL_AI_GATEWAY_URL" not in os.environ:
5 os.environ["VERCEL_AI_GATEWAY_URL"] = input("Enter your Vercel AI Gateway URL: ")
6
7if "OPENAI_API_KEY" not in os.environ:
8 os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

Logging LLM Calls

Since Vercel AI Gateway provides an OpenAI-compatible API, we can use the Opik OpenAI SDK wrapper to automatically log Vercel AI Gateway calls as generations in Opik.

Simple LLM Call

1import os
2from opik.integrations.openai import track_openai
3from openai import OpenAI
4
5# Create an OpenAI client with Vercel AI Gateway's base URL
6client = OpenAI(
7 api_key=os.environ["OPENAI_API_KEY"],
8 base_url=os.environ["VERCEL_AI_GATEWAY_URL"]
9)
10
11# Wrap the client with Opik tracking
12client = track_openai(client, project_name="vercel-ai-gateway-demo")
13
14# Make a chat completion request
15response = client.chat.completions.create(
16 model="gpt-3.5-turbo",
17 messages=[
18 {"role": "system", "content": "You are a knowledgeable AI assistant."},
19 {"role": "user", "content": "What is the largest city in France?"}
20 ]
21)
22
23# Print the assistant's reply
24print(response.choices[0].message.content)

Advanced Usage

Using with the @track decorator

If you have multiple steps in your LLM pipeline, you can use the @track decorator to log the traces for each step. If Vercel AI Gateway is called within one of these steps, the LLM call will be associated with that corresponding step:

1import os
2from opik import track
3from opik.integrations.openai import track_openai
4from openai import OpenAI
5
6# Create and wrap the OpenAI client with Vercel AI Gateway's base URL
7client = OpenAI(
8 api_key=os.environ["OPENAI_API_KEY"],
9 base_url=os.environ["VERCEL_AI_GATEWAY_URL"]
10)
11client = track_openai(client)
12
13@track
14def generate_response(prompt: str):
15 response = client.chat.completions.create(
16 model="gpt-3.5-turbo",
17 messages=[
18 {"role": "system", "content": "You are a knowledgeable AI assistant."},
19 {"role": "user", "content": prompt}
20 ]
21 )
22 return response.choices[0].message.content
23
24@track
25def refine_response(initial_response: str):
26 response = client.chat.completions.create(
27 model="gpt-4",
28 messages=[
29 {"role": "system", "content": "You enhance and polish text responses."},
30 {"role": "user", "content": f"Please improve this response: {initial_response}"}
31 ]
32 )
33 return response.choices[0].message.content
34
35@track(project_name="vercel-ai-gateway-demo")
36def generate_and_refine(prompt: str):
37 # First LLM call: Generate initial response
38 initial = generate_response(prompt)
39
40 # Second LLM call: Refine the response
41 refined = refine_response(initial)
42
43 return refined
44
45# Example usage
46result = generate_and_refine("Explain quantum computing in simple terms.")

The trace will show nested LLM calls with hierarchical spans.

Further Improvements

If you have suggestions for improving the Vercel AI Gateway integration, please let us know by opening an issue on GitHub.