Langfuse launches AI Gateway Learn more
IntegrationsVercel AI SDK (gateway)

Vercel AI SDK with the Langfuse AI Gateway

Beta. The Langfuse AI Gateway is in public beta and can still change.

What is the Vercel AI SDK? The Vercel AI SDK is a lightweight toolkit that lets developers call and stream responses from AI models (like OpenAI, Anthropic, or any compliant provider) directly in web apps with simple server/client functions.

What is Langfuse? Langfuse is an open-source AI engineering platform. It helps teams trace agentic applications, debug issues, evaluate quality, and monitor costs in production. The Langfuse AI Gateway is a central entry point for your AI traffic: it relays model calls to your providers, traces every call in Langfuse, and tracks its cost, without the Langfuse SDK. Use Langfuse Cloud (hosted by Langfuse, free tier, no infrastructure to run) or self-host it.

Use the AI SDK by Vercel with the gateway by pointing its OpenAI or Anthropic provider at the gateway. Each provider needs the matching provider connection in your organization: OPENAI for OpenAI models, ANTHROPIC for Anthropic models.

For tracing the Vercel AI SDK without the gateway, see the Vercel AI SDK integration.

Before you start, create a gateway key and export LANGFUSE_GATEWAY_HOST and LANGFUSE_GATEWAY_KEY as in Send your first request. Replace <model-id> with an ID from the models endpoint.

Configure the provider

Create the provider with the gateway's route as base URL and the gateway key as API key.

Use the gateway's OpenAI route, /openai/v1, with the OpenAI provider.

npm install ai @ai-sdk/openai
import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

const openai = createOpenAI({
  baseURL: `${process.env.LANGFUSE_GATEWAY_HOST}/openai/v1`,
  apiKey: process.env.LANGFUSE_GATEWAY_KEY,
});

const { text } = await generateText({
  model: openai("<model-id>"),
  prompt: "Say hello",
});
console.log(text);

openai("<model-id>") uses the Responses API. To use Chat Completions instead, use openai.chat("<model-id>").

Use the gateway's Anthropic route with the Anthropic provider. Unlike the Anthropic SDK, the AI SDK provider expects /v1 in the base URL, so use /anthropic/v1.

npm install ai @ai-sdk/anthropic
import { createAnthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";

const anthropic = createAnthropic({
  baseURL: `${process.env.LANGFUSE_GATEWAY_HOST}/anthropic/v1`,
  apiKey: process.env.LANGFUSE_GATEWAY_KEY,
});

const { text } = await generateText({
  model: anthropic("<model-id>"),
  prompt: "Say hello",
});
console.log(text);

Add request headers

Set request headers on the provider for every call, or per call for values such as a session ID.

import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

const openai = createOpenAI({
  baseURL: `${process.env.LANGFUSE_GATEWAY_HOST}/openai/v1`,
  apiKey: process.env.LANGFUSE_GATEWAY_KEY,
  headers: { "langfuse-tags": "checkout,production" },
});

const { text } = await generateText({
  model: openai("<model-id>"),
  prompt: "Say hello",
  headers: { "langfuse-session-id": "<session-id>" },
});
import { createAnthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";

const anthropic = createAnthropic({
  baseURL: `${process.env.LANGFUSE_GATEWAY_HOST}/anthropic/v1`,
  apiKey: process.env.LANGFUSE_GATEWAY_KEY,
  headers: { "langfuse-tags": "checkout,production" },
});

const { text } = await generateText({
  model: anthropic("<model-id>"),
  prompt: "Say hello",
  headers: { "langfuse-session-id": "<session-id>" },
});

The gateway already records every call. Don't also trace the same calls with the Langfuse Vercel AI SDK integration, or each model call is recorded twice.


Was this page helpful?

Last updated on