add-provider-package
Original:🇺🇸 English
Translated
Guide for adding new AI provider packages to the AI SDK. Use when creating a new @ai-sdk/<provider> package to integrate an AI service into the SDK.
3installs
Sourcevercel/ai
Added on
NPX Install
npx skill4agent add vercel/ai add-provider-packageTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Adding a New Provider Package
This guide covers the process of creating a new package to integrate an AI service into the AI SDK.
@ai-sdk/<provider>First-Party vs Third-Party Providers
- Third-party packages: Any provider can create a third-party package. We're happy to link to it from our documentation.
- First-party packages: If you prefer a first-party package, please create an issue first to discuss.
@ai-sdk/<provider>
Reference Example
See https://github.com/vercel/ai/pull/8136/files for a complete example of adding a new provider.
Provider Architecture
The AI SDK uses a layered provider architecture following the adapter pattern:
- Specifications (): Defines interfaces like
@ai-sdk/provider,LanguageModelV3, etc.EmbeddingModelV3 - Utilities (): Shared code for implementing providers
@ai-sdk/provider-utils - Providers (): Concrete implementations for each AI service
@ai-sdk/<provider> - Core (): High-level functions like
ai,generateText,streamTextgenerateObject
Step-by-Step Guide
1. Create Package Structure
Create a new folder with the following structure:
packages/<provider>packages/<provider>/
├── src/
│ ├── index.ts # Main exports
│ ├── version.ts # Package version
│ ├── <provider>-provider.ts # Provider implementation
│ ├── <provider>-provider.test.ts
│ ├── <provider>-*-options.ts # Model-specific options
│ └── <provider>-*-model.ts # Model implementations (e.g., language, embedding, image)
├── package.json
├── tsconfig.json
├── tsconfig.build.json
├── tsup.config.ts
├── turbo.json
├── vitest.node.config.js
├── vitest.edge.config.js
└── README.mdDo not create a file. It will be auto-generated.
CHANGELOG.md2. Configure package.json
Set up your with:
package.json"name": "@ai-sdk/<provider>"- (initial version, will be updated by changeset)
"version": "0.0.0" "license": "Apache-2.0""sideEffects": false- Dependencies on and
@ai-sdk/provider(use@ai-sdk/provider-utils)workspace:* - Dev dependencies: ,
@ai-sdk/test-server,@types/node,@vercel/ai-tsconfig,tsup,typescriptzod "engines": { "node": ">=18" }- Peer dependency on (both v3 and v4):
zod"zod": "^3.25.76 || ^4.1.8"
Example exports configuration:
json
{
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
}
}3. Create TypeScript Configurations
tsconfig.json:
json
{
"extends": "@vercel/ai-tsconfig/base.json",
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist"]
}tsconfig.build.json:
json
{
"extends": "./tsconfig.json",
"exclude": [
"**/*.test.ts",
"**/*.test-d.ts",
"**/__snapshots__",
"**/__fixtures__"
]
}4. Configure Build Tool (tsup)
Create :
tsup.config.tstypescript
import { defineConfig } from 'tsup';
export default defineConfig({
entry: ['src/index.ts'],
format: ['cjs', 'esm'],
dts: true,
sourcemap: true,
clean: true,
});5. Configure Test Runners
Create both and (copy from existing provider like ).
vitest.node.config.jsvitest.edge.config.jsanthropic6. Implement Provider
Provider implementation pattern:
typescript
// <provider>-provider.ts
import { NoSuchModelError } from '@ai-sdk/provider';
import { loadApiKey } from '@ai-sdk/provider-utils';
export interface ProviderSettings {
apiKey?: string;
baseURL?: string;
// provider-specific settings
}
export class ProviderInstance {
readonly apiKey?: string;
readonly baseURL?: string;
constructor(options: ProviderSettings = {}) {
this.apiKey = options.apiKey;
this.baseURL = options.baseURL;
}
private get baseConfig() {
return {
apiKey: () =>
loadApiKey({
apiKey: this.apiKey,
environmentVariableName: 'PROVIDER_API_KEY',
description: 'Provider API key',
}),
baseURL: this.baseURL ?? 'https://api.provider.com',
};
}
languageModel(modelId: string) {
return new ProviderLanguageModel(modelId, this.baseConfig);
}
// Shorter alias
chat(modelId: string) {
return this.languageModel(modelId);
}
}
// Export default instance
export const providerName = new ProviderInstance();7. Implement Model Classes
Each model type (language, embedding, image, etc.) should implement the appropriate interface from :
@ai-sdk/provider- for text generation models
LanguageModelV3 - for embedding models
EmbeddingModelV3 - for image generation models
ImageModelV1 - etc.
Schema guidelines:
Provider Options (user-facing):
- Use unless
.optional()is meaningfulnull - Be as restrictive as possible for future flexibility
Response Schemas (API responses):
- Use instead of
.nullish().optional() - Keep minimal - only include properties you need
- Allow flexibility for provider API changes
8. Create README.md
Include:
- Brief description linking to documentation
- Installation instructions
- Basic usage example
- Link to full documentation
9. Write Tests
- Unit tests for provider logic
- API response parsing tests using fixtures in subdirectory
__fixtures__ - Both Node.js and Edge runtime tests
See skill for capturing real API responses for testing.
capture-api-response-test-fixture10. Add Examples
Create examples in for each model type the provider supports:
examples/ai-functions/src/- - Basic text generation
generate-text/<provider>.ts - - Streaming text
stream-text/<provider>.ts - - Structured output (if supported)
generate-object/<provider>.ts - - Streaming structured output (if supported)
stream-object/<provider>.ts - - Embeddings (if supported)
embed/<provider>.ts - - Image generation (if supported)
generate-image/<provider>.ts - etc.
Add feature-specific examples as needed (e.g., , ).
<provider>-tool-call.ts<provider>-cache-control.ts11. Add Documentation
Create documentation in
content/providers/01-ai-sdk-providers/<last number + 10>-<provider>.mdxInclude:
- Setup instructions
- Available models
- Model capabilities
- Provider-specific options
- Usage examples
- API configuration
12. Create Changeset
Run and:
pnpm changeset- Select the new provider package
- Choose version (for new packages starting at 0.0.0)
major - Describe what the package provides
13. Update References
Run from the workspace root to update tsconfig references.
pnpm update-references14. Build and Test
bash
# From workspace root
pnpm build
# From provider package
cd packages/<provider>
pnpm test # Run all tests
pnpm test:node # Run Node.js tests
pnpm test:edge # Run Edge tests
pnpm type-check # Type checking
# From workspace root
pnpm type-check:full # Full type check including examples15. Run Examples
Test your examples:
bash
cd examples/ai-functions
pnpm tsx src/generate-text/<provider>.ts
pnpm tsx src/stream-text/<provider>.tsProvider Method Naming
- Full names: ,
languageModel(id),imageModel(id)(required)embeddingModel(id) - Short aliases: ,
.chat(id),.image(id)(for DX).embedding(id)
File Naming Conventions
- Source files:
kebab-case.ts - Test files:
kebab-case.test.ts - Type test files:
kebab-case.test-d.ts - Provider classes: ,
<Provider>Provider, etc.<Provider>LanguageModel
Security Best Practices
- Never use directly - use
JSON.parseorparseJSONfromsafeParseJSON@ai-sdk/provider-utils - Load API keys securely using from
loadApiKey@ai-sdk/provider-utils - Validate all API responses against schemas
Error Handling
Errors should extend from and use a marker pattern:
AISDKError@ai-sdk/providertypescript
import { AISDKError } from '@ai-sdk/provider';
const name = 'AI_ProviderError';
const marker = `vercel.ai.error.${name}`;
const symbol = Symbol.for(marker);
export class ProviderError extends AISDKError {
private readonly [symbol] = true;
constructor({ message, cause }: { message: string; cause?: unknown }) {
super({ name, message, cause });
}
static isInstance(error: unknown): error is ProviderError {
return AISDKError.hasMarker(error, marker);
}
}Pre-release Mode
If is set up to publish releases, no further action is necessary. Just make sure not to backport it to the stable branch since it will result in an npm version conflict once we exit pre-release mode on .
mainbetavX.YmainChecklist
- Package structure created in
packages/<provider> - configured with correct dependencies
package.json - TypeScript configs set up (,
tsconfig.json)tsconfig.build.json - Build configuration ()
tsup.config.ts - Test configurations (,
vitest.node.config.js)vitest.edge.config.js - Provider implementation complete
- Model classes implement appropriate interfaces
- Unit tests written and passing
- API response test fixtures captured
- Examples created in
examples/ai-functions/src/ - Documentation added in
content/providers/01-ai-sdk-providers/ - README.md written
- Major changeset created
- run
pnpm update-references - All tests passing (from package)
pnpm test - Type checking passing (from root)
pnpm type-check:full - Examples run successfully
Common Issues
- Missing tsconfig references: Run from workspace root
pnpm update-references - Type errors in examples: Run to catch issues early
pnpm type-check:full - Test failures: Ensure both Node and Edge tests pass
- Build errors: Check that is configured correctly
tsup.config.ts
Related Documentation
- Provider Architecture
- Provider Development Notes
- Develop AI Functions Example
- Capture API Response Test Fixture