Claude with the Vercel AI SDK: createAnthropic baseURL, streaming and fixes
The AI SDK's Anthropic provider, @ai-sdk/anthropic, talks to the Messages API. Its baseURL defaults to https://api.anthropic.com/v1, so for a gateway you replace that whole prefix, including /v1. OpenCode, Kilo Code and many agent frameworks use this same provider underneath, which is why the same rule shows up in their configs.
Install
npm i ai @ai-sdk/anthropicCreate the provider
import { createAnthropic } from '@ai-sdk/anthropic';
export const anthropic = createAnthropic({
baseURL: 'https://aiprimetech.io/v1', // replaces https://api.anthropic.com/v1
apiKey: process.env.AIPRIME_API_KEY, // sent as x-api-key; defaults to ANTHROPIC_API_KEY
});@ai-sdk/anthropic: pass baseURL in code. The plain anthropic import always targets Anthropic's own endpoint.generateText and streamText
import { generateText } from 'ai';
import { anthropic } from './lib/anthropic';
const { text, usage } = await generateText({
model: anthropic('claude-sonnet-5'),
prompt: 'Explain idempotency keys in two sentences.',
});
console.log(text, usage);import { streamText } from 'ai';
import { anthropic } from './lib/anthropic';
const result = streamText({
model: anthropic('claude-sonnet-5'),
prompt: 'Write a haiku about load balancers.',
});
for await (const chunk of result.textStream) process.stdout.write(chunk);Both calls accept maxRetries (2 by default) for transient failures such as 429 and 5xx.
Thinking and prompt caching
Anthropic-specific options go in providerOptions.anthropic. Current models use adaptive thinking:
const { text } = await generateText({
model: anthropic('claude-sonnet-5'),
providerOptions: { anthropic: { thinking: { type: 'adaptive' } } },
prompt: 'Plan a zero-downtime database migration in five steps.',
});Cache a long, stable system prompt with cacheControl; cache reads then show up in usage.inputTokenDetails.cacheReadTokens. Prompts below the model's minimum length are not cached.
const result = await generateText({
model: anthropic('claude-sonnet-5'),
messages: [
{
role: 'system',
content: longStableInstructions,
providerOptions: { anthropic: { cacheControl: { type: 'ephemeral' } } },
},
{ role: 'user', content: question },
],
});The OpenAI-compatible alternative
The gateway also serves the OpenAI Chat Completions shape, so @ai-sdk/openai-compatible works with the same /v1 base URL. It is the route for GPT models on the same key; for Claude, the native provider above keeps thinking and cache control.
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
const gateway = createOpenAICompatible({
name: 'aiprime',
baseURL: 'https://aiprimetech.io/v1',
apiKey: process.env.AIPRIME_API_KEY,
});
// gateway('claude-sonnet-5') or a GPT model IDTroubleshooting
| What you see | Cause | Fix |
|---|---|---|
A JSON parse error, response starting with < | baseURL without /v1: the request hit the website | baseURL: 'https://aiprimetech.io/v1' |
404 page not found | /v1 twice | Exactly one /v1 |
Invalid API key | process.env value undefined in that runtime (serverless and edge functions have their own env), so the SDK fell back to ANTHROPIC_API_KEY | Set the variable in the deployment's environment |
| Model not supported | Old or misspelled model ID | Use an ID from the table below |
Concurrency limit exceeded for user | Promise.all over a large array | Limit parallel calls, e.g. with p-limit |
No available accounts / 503 | Temporary upstream capacity | Retried automatically; raise maxRetries |
| Model ID | Use it for |
|---|---|
claude-sonnet-5 | Default for coding and agent work: fast, strong, mid-priced. |
claude-opus-5-5 | Harder reasoning, large refactors, long agent runs. |
claude-fable-5-1 | Most capable and most expensive; save it for the hardest tasks. |
claude-haiku-4-5 | Cheap and quick: titles, summaries, classification, small steps. |
Frequently asked questions
What is the default baseURL of @ai-sdk/anthropic?
Is there an environment variable for the AI SDK Anthropic base URL?
Why does the AI SDK fail with a parse error when calling Claude?
Should I use @ai-sdk/anthropic or @ai-sdk/openai-compatible for Claude?
Get an API key — no Anthropic account or waitlist required.
Get your API keyAI Prime Tech is an independent API gateway. It is not affiliated with, endorsed by, or a reseller of Anthropic. Claude and related model names are trademarks of their respective owners.