← Blog

Claude API + Vercel AI SDK Integration Guide

2026-11-12 · 4 min read · SubToAPI Team

Connecting Claude to the Vercel AI SDK

The Vercel AI SDK talks to Claude through the @ai-sdk/anthropic provider package, which wraps Anthropic's Messages API in the same streamText, generateText, and useChat interfaces the SDK uses for every other model. You install one package, create a provider instance with an API key, and pass it into the SDK's standard functions — no custom fetch wrapper, no manual SSE parsing.

This matters because the two most common reasons developers search for this integration are: they want streaming chat UI (token-by-token rendering in React) wired to Claude, or they want tool use / function calling to work through the same abstraction they already use for OpenAI models. The AI SDK supports both for Claude out of the box, as long as you're on a recent model version and the @ai-sdk/anthropic package.

Basic setup

Install the SDK and the Anthropic provider:

npm install ai @ai-sdk/anthropic

Create a provider instance and call it from a server route:

// app/api/chat/route.js
import { anthropic } from '@ai-sdk/anthropic';
import { streamText } from 'ai';

export async function POST(req) {
  const { messages } = await req.json();

  const result = streamText({
    model: anthropic('claude-sonnet-4-5'),
    messages,
    system: 'You are a concise, technical assistant.',
  });

  return result.toDataStreamResponse();
}

On the client, useChat from ai/react consumes this route without any extra configuration:

'use client';
import { useChat } from 'ai/react';

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat();

  return (
    <div>
      {messages.map((m) => (
        <div key={m.id}><b>{m.role}:</b> {m.content}</div>
      ))}
      <form onSubmit={handleSubmit}>
        <input value={input} onChange={handleInputChange} />
      </form>
    </div>
  );
}

That's the entire streaming integration. The AI SDK handles backpressure, partial token buffering, and the stream-to-UI wiring; the Anthropic provider handles translating Claude's event format into the SDK's internal chunks.

Tool use with the AI SDK

Claude's tool calling maps cleanly onto the AI SDK's tools object. Define a tool with a Zod schema and an execute function, pass it into streamText or generateText, and the SDK handles the round-trip (model requests a tool call, you run it, result goes back to the model):

import { anthropic } from '@ai-sdk/anthropic';
import { streamText, tool } from 'ai';
import { z } from 'zod';

const result = streamText({
  model: anthropic('claude-sonnet-4-5'),
  messages,
  tools: {
    getWeather: tool({
      description: 'Get current weather for a city',
      parameters: z.object({ city: z.string() }),
      execute: async ({ city }) => {
        const res = await fetch(`https://api.example.com/weather?city=${city}`);
        return res.json();
      },
    }),
  },
});

This works the same whether you're calling Anthropic directly or through a gateway that mirrors the Messages API — the AI SDK doesn't care about the upstream, only that the responses match the expected shape.

Using a managed key instead of a raw Anthropic key

If you're already issuing Claude access to multiple apps or team members, pointing the AI SDK straight at a personal Anthropic key gets messy fast — no per-app usage breakdown, no easy key rotation, no seat management. SubToAPI turns your existing Claude access into a standard HTTPS API with scoped sub_live_... keys, and because it exposes the same Messages-style request/response shape, you can point the Anthropic provider's baseURL at it instead of changing any application code:

import { createAnthropic } from '@ai-sdk/anthropic';

const anthropic = createAnthropic({
  apiKey: process.env.SUBTOAPI_KEY,
  baseURL: 'https://api.subtoapi.app/v1',
});

const result = streamText({
  model: anthropic('claude-sonnet-4-5'),
  messages,
});

Everything downstream — streamText, useChat, the tools object — keeps working exactly as before. What you gain is a dashboard with per-key usage, streaming and tool-use requests logged with metadata, and team seats so you're not sharing one secret across every developer and every deployed app. Check the docs and the quickstart for the exact request format, messages and streaming reference for parity details with the Anthropic API, and tools if you want to confirm tool-call behavior before swapping a production key.

Common integration issues

Streaming stops mid-response in production but works locally. This is almost always an edge runtime or proxy buffering issue, not an SDK bug. If you're deploying on Vercel, make sure the route isn't accidentally running in a runtime that buffers responses, and avoid putting a buffering reverse proxy in front of the streaming endpoint.

Tool calls return but execute never runs. Double-check that your tool's parameters schema actually matches what the model is sending — a mismatched Zod schema will often fail silently or throw inside the SDK's internal validation rather than surfacing a clear error.

Switching models breaks tool use. Not every Claude model version supports tool use identically. If you upgrade or downgrade the model string passed to anthropic(...), re-test your tool-calling flows specifically, since behavior around parallel tool calls can differ between versions.

Rate limits hit faster than expected with multiple developers sharing one key. This is the most common reason teams move off a single raw API key toward a gateway with per-user or per-app quotas — it's a symptom of key sharing, not an SDK problem.

Does the Vercel AI SDK support Claude natively?

Yes. Install @ai-sdk/anthropic and pass a model string like claude-sonnet-4-5 into anthropic(...). It works with streamText, generateText, and useChat the same way other providers do.

Can I stream and use tools at the same time?

Yes. Pass a tools object into streamText alongside your messages. The SDK streams text output and handles tool-call round-trips without requiring a separate non-streaming call.

Can I swap my Anthropic key for a managed gateway without changing code?

If the gateway mirrors the Messages API, yes — set baseURL and apiKey on createAnthropic() and the rest of your streamText/useChat code stays unchanged. SubToAPI is built for exactly this, with a free trial at signup.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →