feat: migrate anthropic provider to AI SDK#11275
Conversation
Reviewed the Anthropic AI SDK migration. One issue flagged:
Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues. |
| const useAuthToken = !!(this.options.anthropicBaseUrl && this.options.anthropicUseAuthToken) | ||
|
|
||
| this.client = new Anthropic({ | ||
| const headers: Record<string, string> = { ...DEFAULT_HEADERS } | ||
| if (useAuthToken && this.options.apiKey) { | ||
| headers["Authorization"] = `Bearer ${this.options.apiKey}` | ||
| } | ||
|
|
||
| this.provider = createAnthropic({ | ||
| apiKey: useAuthToken ? "" : (this.options.apiKey ?? "not-provided"), | ||
| baseURL: this.options.anthropicBaseUrl || undefined, | ||
| [apiKeyFieldName]: this.options.apiKey, | ||
| headers, | ||
| }) |
There was a problem hiding this comment.
When useAuthToken is true, setting apiKey: "" causes @ai-sdk/anthropic to send an x-api-key: "" header on every request, alongside the Authorization: Bearer header added here. The old code used the Anthropic SDK's authToken constructor option, which sent only the Authorization: Bearer header and never set x-api-key. Custom Anthropic-compatible backends that validate x-api-key for non-empty values will reject these requests. This only affects users with a custom anthropicBaseUrl who also enable anthropicUseAuthToken. One possible workaround is to pass apiKey: undefined (if the AI SDK accepts it) or to strip x-api-key from outgoing requests via a custom fetch wrapper.
Fix it with Roo Code or mention @roomote and request a fix.
Summary
Migrates the Anthropic provider (
src/api/providers/anthropic.ts) from using the raw@anthropic-ai/sdkto@ai-sdk/anthropicwith the Vercel AI SDK, following the same pattern already used by Gemini and Vertex providers.Key Changes
New Files
src/api/transform/caching/ai-sdk-anthropic.ts— Cache breakpoint utility that addsproviderOptions.anthropic.cacheControlto AI SDK messagessrc/api/transform/caching/__tests__/ai-sdk-anthropic.spec.ts— Tests for the cache utilityModified Files
src/package.json— Added@ai-sdk/anthropicdependencysrc/api/providers/anthropic.ts— Full rewrite from raw SDK to AI SDK pattern:createAnthropic()+streamText()/generateText()from the AI SDKcaching/directory pattern)providerOptions.anthropic.cacheControlbudget_tokens→budgetTokens(snake_case → camelCase)headersoptiongetRedactedThinkingBlocks()providerMetadata.anthropic.cacheCreationInputTokenssrc/api/transform/ai-sdk.ts— EnhancedprocessAiSdkStreamPartto handlereasoning-endwith signatures;convertToAiSdkMessagespreserves thinking block signatures and redacted_thinkingsrc/api/providers/__tests__/anthropic.spec.ts— Full rewrite: 33 testssrc/api/transform/__tests__/ai-sdk.spec.ts— Updated for thinking/signature handlingTesting
anthropic.spec.tsai-sdk.spec.tsai-sdk-anthropic.spec.tsMigration Pattern
Follows the established AI SDK migration pattern from Gemini/Vertex:
ModelMessage[]viaconvertToAiSdkMessages()addAiSdkAnthropicCacheBreakpoints()streamText()→result.fullStream→processAiSdkStreamPart()result.reasoningafter stream completesImportant
Migrates Anthropic provider to AI SDK, adding cache and reasoning support, with extensive testing.
anthropic.tsto use@ai-sdk/anthropicandaifor message processing.ai-sdk-anthropic.ts.ai-sdk.ts.ai-sdk-anthropic.spec.tsfor cache utility tests.anthropic.spec.tswith 33 tests for new SDK behavior.ai-sdk.spec.tsfor reasoning and signature handling.@ai-sdk/anthropictopackage.json.This description was created by
for 8356162. You can customize this summary. It will automatically update as commits are pushed.