Skip to main content
Implement ModelProvider when an existing provider adapter does not fit your API. An Agent calls this interface for generation and streaming; your adapter translates messages, options, responses, usage, and errors. Supporting the interface does not automatically add tool calling, structured output, multimodal input, or cancellation to an upstream service.

ModelProvider interface

Use the exported types as the contract:
ModelResponse.raw contains the adapter’s original response and is typed unknown. It is not copied to Agent.run()’s RunOutput. Normalize token usage into promptTokens, completionTokens, and totalTokens; preserve additional provider accounting in usage.providerMetrics when available.

Example skeleton

This host integration factory accepts a text-only request function owned by your application. It rejects features that this small adapter does not implement. The host function must validate its upstream response and pass signal to the upstream request.
The stream above is explicitly buffered. For live token streaming, map the upstream event stream into the chunk contract below and cancel/close the upstream iterator when consumption ends. Report upstream failures as errors rather than yielding an empty successful answer.

Register with ModelRegistry

Pass a provider instance directly to new Agent({ model }) when only that Agent needs it. Register a factory with modelRegistry when another component, such as admin hydration, resolves a provider/model ID pair.
Your factory validates configuration before using it. Register it during application startup, before resolving a model or hydrating stored blueprints. Core’s separate registry holds live Agent/Team/Workflow instances; it is not the provider registry. Avoid replacing a built-in provider ID unintentionally.

StreamChunk types

Preserve tool-call IDs and provider replay metadata across turns. If you support function tools, test a complete model → tool → model exchange, not only a single text response. See the generated model contracts for exact types.

Full example

This complete local fixture verifies registry resolution, Agent execution, streaming, and cleanup. It echoes text; it is not an AI model or an upstream API integration. Use an ESM TypeScript project ("type": "module" in package.json) on a supported Node version.
Save as provider-demo.ts:
Expect Echo: Hello and Echo: Streaming. Adapt the request boundary to your upstream SDK, then add explicit checks for its usage mapping, cancellation, malformed responses, and every feature you enable. Use the built-in Cohere adapter for Cohere rather than duplicating its API normalization here.