Skip to slide
051 / 221

CH 04 · Model-Provider Abstraction and Multi-Model Strategy · 06/15

Read as one page

Real 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. [1] specificationVersion is the interface generation this object implements. resolveLanguageModel, in packages/ai/src/model/resolve-model.ts, accepts 'v4', 'v3', and 'v2'. Any other value, including a missing one, throws UnsupportedModelVersionError. A v2 or v3 object is passed through asLanguageModelV4 before the rest of the call uses it.
  2. [2] provider is the provider id, anthropic.messages by default, and modelId is that provider's model id. The API key travels in the request headers. doGenerate is the one-shot call, and its argument, LanguageModelV4CallOptions, holds the shared bag: prompt, tools, temperature, stop sequences, and a reasoning level. The call options have no field for a document or a citation. A citation can still come back on a text-end stream part, as part of the response. PromiseLike means the caller can await the result. The removed comment says the do prefix exists so application code does not call the method by accident.
  3. [3] doStream returns a stream of parts under one set of names. Reply text is a text-delta, reasoning is a reasoning-delta, and a tool call being assembled is a tool-input-delta. Anthropic's doStream receives the API event thinking_delta and enqueues a reasoning-delta whose delta is value.delta.thinking. The OpenAI responses language model and the Google language model enqueue reasoning-delta too. 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.