$ show vercel/ai
The Vercel AI SDK is a comprehensive TypeScript monorepo providing a unified, provider-agnostic interface for building AI-powered applications. It abstracts interactions with dozens of LLM providers (OpenAI, Anthropic, Google, Mistral, Cohere, xAI, etc.) behind a consistent API for text generation, streaming, structured output, image/video generation, speech, transcription, embeddings, and tool calling. The architecture is modular: a core `ai` package exposes high-level functions (`generateText`, `streamText`, `generateImage`, `embed`, `transcribe`, `ToolLoopAgent`, `HarnessAgent`) and a middleware system, while separate `@ai-sdk/provider` packages define versioned model interfaces (v2–v4) and `@ai-sdk/provider-utils` provides shared utilities for schema validation, error handling, and HTTP. Each provider lives in its own package (e.g., `@ai-sdk/openai`, `@ai-sdk/anthropic`) with model-specific options and error handlers. The SDK also includes a harness/agent framework (`@ai-sdk/harness`) for integrating external coding agents (Claude Code, Codex, Cursor, GitHub Copilot, etc.) with sandboxed execution and workflow orchestration, plus UI integrations for React, Vue, Svelte, and Angular. Key data flows involve constructing prompts, calling provider APIs via fetch, streaming responses as Server-Sent Events or ReadableStreams, and transforming messages between model and UI formats. External dependencies include provider API endpoints, Zod for schema validation, and OpenTelemetry for telemetry.
The LLM identified 7 files worth highlighting for reuse:
Defines the `LanguageModelV4` interface, the core abstraction for all text generation providers. It specifies methods for `doGenerate` (non-streaming) and `doStream` (streaming), along with versioned metadata, usage tracking, and provider options. This interface is the contract that every provider package implements, enabling the SDK to swap providers transparently. It is reusable because any new provider can implement this interface to integrate with the entire SDK ecosystem.
Provides a factory function `createJsonErrorResponseHandler` that parses JSON error responses from provider APIs into typed error objects. It uses Zod schemas to validate error data and returns a standardized error structure. This pattern is reusable across any HTTP-based API integration, not just AI providers, and demonstrates clean separation of error parsing from business logic.
Implements `wrapLanguageModel`, a decorator function that allows chaining middleware (e.g., logging, RAG, default settings, reasoning extraction) around any language model. Middleware can intercept `doGenerate` and `doStream` calls, modify parameters, and transform results. This pattern is highly reusable for adding cross-cutting concerns to any model invocation without modifying the provider implementation.
Defines the base `AISDKError` class with a marker-based `isInstance` static method for type-safe error checking. All SDK errors (e.g., `NoSuchToolError`, `InvalidArgumentError`, `RetryError`) extend this class and register unique markers. This pattern enables reliable error discrimination without relying on `instanceof` across package boundaries, and is reusable in any multi-package TypeScript project.
Exports the `tool()` factory function for defining tools with Zod input schemas, optional execute functions, and model output transformations. It supports both client-executed and provider-executed tools, and integrates with the SDK's type system for automatic inference of tool types. This pattern is reusable for any application that needs to define callable functions with validated inputs and typed outputs.
Implements `HarnessAgent`, a framework for running external coding agents (e.g., Claude Code, Codex) inside sandboxed sessions with tool forwarding, telemetry, and workflow integration. It provides `resumeOrCreateSession`, `detachAndPersist`, and `stream` methods for durable multi-turn conversations. This pattern is reusable for any system that needs to orchestrate third-party agents with state persistence and streaming UI updates.
Provides a `createTransformer` utility for building jscodeshift-based codemods that automate API migrations. It handles common patterns like renaming imports, changing function calls, and updating type references. This is reusable for any TypeScript/JavaScript project that needs to automate breaking changes across a codebase, and demonstrates a clean separation between transformation logic and file I/O.