Skip to content

Commit c5c3971

Browse files
authored
feat (docs): add comprehensive provider registry example (#5419)
1 parent 567752d commit c5c3971

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

content/docs/03-ai-sdk-core/40-provider-management.mdx content/docs/03-ai-sdk-core/45-provider-management.mdx

+87-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: Provider Management
3-
description: Learn how to work with multiple providers
2+
title: Provider & Model Management
3+
description: Learn how to work with multiple providers and models
44
---
55

6-
# Provider Management
6+
# Provider & Model Management
77

88
When you work with multiple providers and models, it is often desirable to manage them in a central place
99
and access the models through simple string ids.
@@ -171,3 +171,87 @@ const { image } = await generateImage({
171171
prompt: 'A beautiful sunset over a calm ocean',
172172
});
173173
```
174+
175+
## Combining Custom Providers, Provider Registry, and Middleware
176+
177+
The central idea of provider management is to set up a file that contains all the providers and models you want to use.
178+
You may want to pre-configure model settings, provide model name aliases, limit the available models, and more.
179+
180+
Here is an example that implements the following concepts:
181+
182+
- pass through a full provider with a namespace prefix (here: `xai > *`)
183+
- setup an OpenAI-compatible provider with custom api key and base URL (here: `custom > *`)
184+
- setup model name aliases (here: `anthropic > fast`, `anthropic > writing`, `anthropic > reasoning`)
185+
- pre-configure model settings (here: `anthropic > reasoning`)
186+
- validate the provider-specific options (here: `AnthropicProviderOptions`)
187+
- use a fallback provider (here: `anthropic > *`)
188+
- limit a provider to certain models without a fallback (here: `groq > gemma2-9b-it`, `groq > qwen-qwq-32b`)
189+
- define a custom separator for the provider registry (here: `>`)
190+
191+
```ts
192+
import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
193+
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
194+
import { xai } from '@ai-sdk/xai';
195+
import { groq } from '@ai-sdk/groq';
196+
import {
197+
createProviderRegistry,
198+
customProvider,
199+
defaultSettingsMiddleware,
200+
wrapLanguageModel,
201+
} from 'ai';
202+
203+
export const registry = createProviderRegistry(
204+
{
205+
// pass through a full provider with a namespace prefix
206+
xai,
207+
208+
// access an OpenAI-compatible provider with custom setup
209+
custom: createOpenAICompatible({
210+
name: 'provider-name',
211+
apiKey: process.env.CUSTOM_API_KEY,
212+
baseURL: 'https://api.custom.com/v1',
213+
}),
214+
215+
// setup model name aliases
216+
anthropic: customProvider({
217+
languageModels: {
218+
fast: anthropic('claude-3-haiku-20240307'),
219+
220+
// simple model
221+
writing: anthropic('claude-3-7-sonnet-20250219'),
222+
223+
// extended reasoning model configuration:
224+
reasoning: wrapLanguageModel({
225+
model: anthropic('claude-3-7-sonnet-20250219'),
226+
middleware: defaultSettingsMiddleware({
227+
settings: {
228+
maxTokens: 100000, // example default setting
229+
providerMetadata: {
230+
anthropic: {
231+
thinking: {
232+
type: 'enabled',
233+
budgetTokens: 32000,
234+
},
235+
} satisfies AnthropicProviderOptions,
236+
},
237+
},
238+
}),
239+
}),
240+
},
241+
fallbackProvider: anthropic,
242+
}),
243+
244+
// limit a provider to certain models without a fallback
245+
groq: customProvider({
246+
languageModels: {
247+
'gemma2-9b-it': groq('gemma2-9b-it'),
248+
'qwen-qwq-32b': groq('qwen-qwq-32b'),
249+
},
250+
}),
251+
},
252+
{ separator: ' > ' },
253+
);
254+
255+
// usage:
256+
const model = registry.languageModel('anthropic > reasoning');
257+
```

0 commit comments

Comments
 (0)