Skip to content

feat(ai-gateway-provider): add createProviderRouter for native SDK routing#439

Open
ryanskidmore wants to merge 1 commit intocloudflare:mainfrom
ryanskidmore:feat/provider-router
Open

feat(ai-gateway-provider): add createProviderRouter for native SDK routing#439
ryanskidmore wants to merge 1 commit intocloudflare:mainfrom
ryanskidmore:feat/provider-router

Conversation

@ryanskidmore
Copy link
Copy Markdown

Summary

  • Adds createProviderRouter() to ai-gateway-provider that routes model IDs by prefix to native AI SDK provider instances
  • Enables consumers to use provider-specific features (like Anthropic prompt caching) that are lost when using the unified provider, while maintaining a single provider interface
  • Exports via @cloudflare/ai-gateway-provider/providers/router

Problem

The unified provider (createUnified()) intercepts all fetch calls and normalizes them through a common interface. This works well for most use cases, but provider-specific features like Anthropic's cache_control fields 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:

  1. Matches the requested model ID against configured prefixes (longest match wins)
  2. Routes to the corresponding native SDK provider (which preserves all provider-specific features)
  3. Falls back to a configurable default provider when no prefix matches
import { createProviderRouter, createAnthropic, createOpenAI } from "@cloudflare/ai-gateway-provider/providers";

const provider = createProviderRouter({
  providers: {
    "claude-": createAnthropic({ gateway: { ... } }),
    "gpt-": createOpenAI({ gateway: { ... } }),
  },
  defaultProvider: createOpenAI({ gateway: { ... } }),
});

// Routes to native Anthropic SDK (preserves cache_control, etc.)
const result = await generateText({
  model: provider.languageModel("claude-sonnet-4-20250514"),
  prompt: "Hello",
});

Test Coverage

7 test cases covering:

  • Basic prefix routing to correct provider
  • Fallback to default provider for unknown models
  • Longest prefix match precedence
  • Error when no default provider and no match
  • textEmbeddingModel() routing
  • Empty provider map handling
  • Exact prefix matching (no partial word matches needed — model IDs are prefix-matched by design)

@ryanskidmore ryanskidmore requested a review from G4brym as a code owner March 13, 2026 19:13
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 13, 2026

🦋 Changeset detected

Latest commit: 3aca672

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
ai-gateway-provider Minor

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

@ryanskidmore ryanskidmore force-pushed the feat/provider-router branch from 4ec1f40 to 32550f3 Compare March 13, 2026 19:25
@mattzcarey
Copy link
Copy Markdown
Collaborator

/bonk review this

@mattzcarey
Copy link
Copy Markdown
Collaborator

LGTM but gonna let @threepointone to look at it. Where will this be documented?

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 16, 2026

Open in StackBlitz

npx https://pkg.pr.new/cloudflare/ai/ai-gateway-provider@439
npx https://pkg.pr.new/cloudflare/ai/@cloudflare/tanstack-ai@439
npx https://pkg.pr.new/cloudflare/ai/workers-ai-provider@439

commit: 3aca672

@ryanskidmore
Copy link
Copy Markdown
Author

LGTM but gonna let @threepointone to look at it. Where will this be documented?

Where's best to document it?

@mattzcarey
Copy link
Copy Markdown
Collaborator

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
@threepointone
Copy link
Copy Markdown
Collaborator

need to think about this a little more, I'll come to it early next week.

Copy link
Copy Markdown

@ArshVermaGit ArshVermaGit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants