CH 04 · Model-Provider Abstraction and Multi-Model Strategy · 06/15
Read as one pageReal code: one interface every provider implements
The adapter pattern is a contract the rest of the program speaks, plus one translator per provider. The Vercel AI SDK writes that contract as the type LanguageModelV4, in the package @ai-sdk/provider. AnthropicLanguageModel, in @ai-sdk/anthropic, is declared implements LanguageModelV4 and sets specificationVersion to 'v4'. The OpenAI and Google language-model classes do the same. Application code talks to the SDK. The SDK calls doGenerate or doStream.
export type LanguageModelV4 = {
/**
* The language model must specify which language model interface version it implements.
*/
readonly specificationVersion: 'v4'; // [1]
// ... (3 lines: doc comment, this string is the provider id)
readonly provider: string;
// ... (3 lines: doc comment, this string is the provider's model id)
readonly modelId: string;
// ... (15 lines: supportedUrls, file URLs the model can read without the SDK downloading them)
// ... (6 lines: doc comment, the "do" prefix so application code does not call this directly)
doGenerate( // [2]
options: LanguageModelV4CallOptions,
): PromiseLike<LanguageModelV4GenerateResult>;
// ... (8 lines: the same naming note, and a note that the result is a stream of output parts)
doStream( // [3]
options: LanguageModelV4CallOptions,
): PromiseLike<LanguageModelV4StreamResult>;
};
Source: vercel/ai, packages/provider/src/language-model/v4/language-model-v4.ts (Apache-2.0)
What to notice
- [1]
specificationVersionis the interface generation this object implements.resolveLanguageModel, inpackages/ai/src/model/resolve-model.ts, accepts'v4','v3', and'v2'. Any other value, including a missing one, throwsUnsupportedModelVersionError. A v2 or v3 object is passed throughasLanguageModelV4before the rest of the call uses it. - [2]
provideris the provider id,anthropic.messagesby default, andmodelIdis that provider's model id. The API key travels in the request headers.doGenerateis the one-shot call, and its argument,LanguageModelV4CallOptions, holds the shared bag: prompt, tools, temperature, stop sequences, and areasoninglevel. The call options have no field for a document or a citation. A citation can still come back on atext-endstream part, as part of the response.PromiseLikemeans the caller can await the result. The removed comment says thedoprefix exists so application code does not call the method by accident. - [3]
doStreamreturns a stream of parts under one set of names. Reply text is atext-delta, reasoning is areasoning-delta, and a tool call being assembled is atool-input-delta. Anthropic'sdoStreamreceives the API eventthinking_deltaand enqueues areasoning-deltawhosedeltaisvalue.delta.thinking. The OpenAI responses language model and the Google language model enqueuereasoning-deltatoo. Switching on each provider's own event names would put the wire format back in the agent loop.
Read it yourself: Find resolvedModel.doStream in stream-language-model-call.ts. The call spreads callSettings (the rest of that function's arguments; the comment documents reasoning as one of them) and passes prompt, tools, and providerOptions. Then read providerOptions in language-model-v4-call-options.ts. A setting the shared fields cannot express goes there, and stays inside the provider.
Finished this idea?
Your learning progress stays in this browser.