feat(ai-gateway-provider): add createProviderRouter for native SDK routing#439
feat(ai-gateway-provider): add createProviderRouter for native SDK routing#439ryanskidmore wants to merge 1 commit intocloudflare:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 3aca672 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
4ec1f40 to
32550f3
Compare
|
/bonk review this |
|
LGTM but gonna let @threepointone to look at it. Where will this be documented? |
commit: |
Where's best to document it? |
Maybe here? https://developers.cloudflare.com/ai-gateway/integrations/vercel-ai-sdk/ |
…uting Add a provider router that maps model ID prefixes to native AI SDK provider instances, enabling consumers to route requests through the correct native SDK (e.g. Anthropic, OpenAI) based on model name. This preserves provider-specific features like prompt caching that are lost when using the unified provider, while maintaining the convenience of a single provider interface. - createProviderRouter() accepts a map of model prefix -> provider - Falls back to a configurable default provider - Unit tests for routing logic (7 cases) - Integration tests proving cache_control survives the full pipeline: router -> native Anthropic SDK -> createAiGateway -> gateway request
32550f3 to
3aca672
Compare
|
need to think about this a little more, I'll come to it early next week. |
ArshVermaGit
left a comment
There was a problem hiding this comment.
This PR introduces createProviderRouter() to the ai-gateway-provider, enabling routing of model IDs to native AI SDK providers based on prefix matching. This is a thoughtful solution to a real limitation of the unified provider approach, restoring access to provider-specific capabilities (e.g., Anthropic prompt caching) while preserving a single, consistent provider interface.
The design strikes a strong balance between flexibility and usability, particularly for advanced use cases requiring provider-specific features.
Summary
createProviderRouter()toai-gateway-providerthat routes model IDs by prefix to native AI SDK provider instances@cloudflare/ai-gateway-provider/providers/routerProblem
The unified provider (
createUnified()) intercepts allfetchcalls and normalizes them through a common interface. This works well for most use cases, but provider-specific features like Anthropic'scache_controlfields are not preserved because the unified provider doesn't pass through non-standard fields.Consumers who need these features currently have to manually instantiate and switch between different native SDK providers, losing the convenience of a single provider.
Solution
createProviderRouter()accepts a configuration mapping model ID prefixes to native AI SDK provider instances and returns a standard AI SDK provider. It:Test Coverage
7 test cases covering:
textEmbeddingModel()routing