fix(anthropic): normalize ANTHROPIC_BASE_URL to accept bare-host form (fixes Claude Code / Cursor / LiteLLM environments)#15581
Open
AkshatRaj00 wants to merge 1 commit into
Conversation
Claude Code, Cursor, and LiteLLM inject ANTHROPIC_BASE_URL as a bare host (https://api.anthropic.com) following the official @anthropic-ai/sdk convention. The Vercel AI SDK expected /v1 to already be present, causing silent 404 errors for all users in those environments. This commit adds a normalizeAnthropicBaseURL() helper that accepts both the bare-host form and the /v1 form and always produces the /v1 form. Existing usage is unaffected — users passing https://api.anthropic.com/v1 explicitly continue to work correctly. Fixes: vercel#15542 Fixes: vercel#15580
| * https://my.proxy.com -> https://my.proxy.com/v1 | ||
| * https://my.proxy.com/v1 -> https://my.proxy.com/v1 | ||
| */ | ||
| export function normalizeAnthropicBaseURL(url: string): string { |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
@ai-sdk/anthropicreadsANTHROPIC_BASE_URLbut doesn't normalize it. Tools like Claude Code, Cursor, and LiteLLM inject this env var as a bare host (https://api.anthropic.com) — following the official@anthropic-ai/sdkconvention. Because the Vercel AI SDK expected/v1to already be present, the final request URL became:Instead of the correct:
The
AI_APICallError: Not Founderror message gives no hint about the missing/v1, so developers using Claude Code / Cursor as their IDE have been silently hit with broken Anthropic calls, with no easy path to diagnosing the root cause.Related issues: #15542 #15580
Solution
Added
normalizeAnthropicBaseURL()— a small helper that always produces the/v1form regardless of whether the input has it or not:This is applied to the resolved
baseURLbefore it's passed to any model constructor.https://api.anthropic.comhttps://api.anthropic.com/v1✅https://api.anthropic.com/v1https://api.anthropic.com/v1✅https://api.anthropic.com/v1/https://api.anthropic.com/v1✅https://my.proxy.comhttps://my.proxy.com/v1✅https://my.proxy.com/v1https://my.proxy.com/v1✅Changes
packages/anthropic/src/anthropic-provider.tsnormalizeAnthropicBaseURL()helpercreateAnthropic()to apply normalization to the resolved base URLbaseURLoption to document bare-host form supportWhy No Breaking Changes
Users who already pass
https://api.anthropic.com/v1(with/v1) are unaffected — the normalization strips the trailing/v1and immediately re-appends it, resulting in the exact same string.Test Plan
https://api.anthropic.com) → normalizes to…/v1✅/v1suffix → unchanged ✅/v1→ gets/v1appended ✅/v1→ unchanged ✅Ready for unit tests in
anthropic-provider.test.tsif the team points me at the preferred test pattern — happy to add them in a follow-up commit.