# 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 ```bash npm i ai @ai-sdk/anthropic ``` ## Create the provider ```ts 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 }); ``` > There is no environment variable for the base URL in `@ai-sdk/anthropic`: pass `baseURL` in code. The plain `anthropic` import always targets Anthropic's own endpoint. ## generateText and streamText ```ts 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); ``` ```ts 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: ```ts 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. ```ts 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. ```ts 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 ID ``` ## Troubleshooting | 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. | ## FAQ ### What is the default baseURL of @ai-sdk/anthropic? https://api.anthropic.com/v1. A gateway replaces the whole prefix, so it must end in /v1, e.g. https://aiprimetech.io/v1. ### Is there an environment variable for the AI SDK Anthropic base URL? No. The provider reads ANTHROPIC_API_KEY (or ANTHROPIC_AUTH_TOKEN) from the environment, but the base URL is set with createAnthropic({ baseURL }). ### Why does the AI SDK fail with a parse error when calling Claude? Usually the baseURL lacks /v1, so the request reaches a website and gets HTML back instead of JSON. ### Should I use @ai-sdk/anthropic or @ai-sdk/openai-compatible for Claude? @ai-sdk/anthropic: it speaks the native Messages API, so thinking and prompt caching work. Use the OpenAI-compatible provider for GPT models. --- Source: https://aiprimetech.io/learn/use-claude-api-with-vercel-ai-sdk/ Provider: AI Prime Tech — independent Anthropic-compatible API gateway (not affiliated with Anthropic).