Overview
Want to build AI-powered apps through code? This by-example tutorial provides 85 heavily annotated examples covering the full spectrum of modern AI application development—from your first API call to production-ready multi-agent systems, RAG pipelines, and observability.
Primary library: Vercel AI SDK ai@6.0.168 with supplementary coverage of OpenAI SDK openai@6.34.0, LangChain.js langchain@1.3.3, LlamaIndex.TS llamaindex@0.12.1, Mastra mastra@1.x, and the Model Context Protocol.
What Is By-Example Learning?
By-example learning is a code-first approach where you learn concepts through annotated, working examples rather than narrative explanations. Each example shows:
- Brief explanation — What the concept is and when to use it
- Mermaid diagram (when appropriate) — Visual representation of complex flows
- Heavily annotated code — Working TypeScript code with 1–2.25 comment lines per code line
- Key Takeaway — 1–2 sentence distillation of the pattern
- Why It Matters — 50–100 words connecting the pattern to production relevance
This approach works best when you already understand TypeScript and basic web development. You learn AI SDK patterns by studying real code rather than theoretical descriptions.
What Is the Vercel AI SDK?
The Vercel AI SDK (ai@6.0.168) is a TypeScript-first framework for integrating large language models into web applications. Key characteristics:
- Provider-agnostic: Swap between OpenAI, Anthropic, Google, and others by changing one line
- Streaming-first: All generation functions support streaming out of the box
- React hooks:
useChat,useCompletion,useObjectfor zero-boilerplate UI - Structured output: Type-safe LLM responses via Zod schemas with
generateObject - Tool calling: First-class tool definitions with
tool(), multi-step loops, human approval gates - Agent primitives:
ToolLoopAgent,prepareStep,stopWhenfor production-grade agents - RAG utilities:
embed(),embedMany(),cosineSimilarity(),rerank()built in - Middleware:
wrapLanguageModel()for logging, caching, and tracing
Critical v6 breaking change: StreamingTextResponse was removed. Use result.toDataStreamResponse() in all route handlers.
Learning Path
%% Color Palette: Blue #0173B2, Orange #DE8F05, Teal #029E73, Purple #CC78BC, Brown #CA9161
graph TD
A["Beginner<br/>Examples 1–28<br/>First calls, streaming, chat UI<br/>embeddings, structured output"] --> B["Intermediate<br/>Examples 29–57<br/>RAG pipelines, tool calling<br/>image gen, middleware, MCP"]
B --> C["Advanced<br/>Examples 58–85<br/>Agents, multi-agent orchestration<br/>evaluation, production hardening"]
D["Prerequisites<br/>TypeScript + Next.js basics"] -.-> A
C -.-> E["Production-ready<br/>AI Application Skills"]
style A fill:#0173B2,stroke:#000,color:#fff
style B fill:#DE8F05,stroke:#000,color:#fff
style C fill:#029E73,stroke:#000,color:#fff
style D fill:#CC78BC,stroke:#000,color:#fff
style E fill:#CA9161,stroke:#000,color:#fff
Coverage by Difficulty
- Beginner (Examples 1–28): First API calls, streaming, chat UI, multi-turn conversations, provider swapping, error handling, embeddings, cosine similarity, semantic search, structured output, and a complete end-to-end minimal chatbot
- Intermediate (Examples 29–57): Batch embeddings, pgvector/Pinecone/Qdrant/Weaviate storage, full RAG pipelines, document chunking, PDF ingestion, hybrid search, reranking, tool definitions, multi-step tool use, human-in-the-loop gates, LangChain LCEL, image generation, audio, middleware, MCP integration
- Advanced (Examples 58–85): ToolLoopAgent, prepareStep for dynamic routing and context management, OpenAI Agents SDK, Mastra agents and workflows, LangGraph, multi-agent orchestration (planner/worker, fan-out/fan-in, competitive evaluation), MCP servers, production RAG, caching, RSC streaming, edge deployment, rate limiting, DevTools, LangSmith, Braintrust, LLM-as-judge, RAGAS evaluation, and a full-stack deployment checklist
Annotation Density: 1–2.25 Comments Per Code Line
All examples maintain 1–2.25 comment lines per code line using // => notation to show values, states, and effects:
const result = await generateText({
// => initiates AI generation call
model: openai("gpt-4o"), // => selects GPT-4o as the LLM
prompt: "What is 2 + 2?", // => user message string
}); // => result contains text, usage, finishReason
console.log(result.text); // => "2 + 2 equals 4."
console.log(result.usage.totalTokens); // => e.g. 42 (prompt + completion tokens)Prerequisites
Before starting, ensure you understand:
- TypeScript fundamentals (types, interfaces, async/await, generics)
- Next.js App Router basics (route handlers, Server Components, Client Components)
- npm package management
- Basic HTTP concepts (requests, responses, streaming)
No prior AI development experience required. The examples build from first principles.
Installation Reference
# Vercel AI SDK with provider adapters (primary library)
npm install ai @ai-sdk/openai @ai-sdk/anthropic
# Direct OpenAI SDK (covered in examples 4-5, 46-47)
npm install openai
# OpenAI Agents SDK (examples 63-65)
npm install @openai/agents
# LangChain.js (examples 48-49, 68, 80)
npm install langchain @langchain/core @langchain/community @langchain/openai
# LlamaIndex.TS (example 37)
npm install llamaindex
# Mastra (examples 66-67)
npm install mastra
# MCP SDK (examples 72-73)
npm install @modelcontextprotocol/sdk
# Vector database clients (examples 31-33)
npm install @pinecone-database/pinecone
npm install @qdrant/js-client-rest
npm install weaviate-clientExamples by Level
Beginner (Examples 1–28)
- Example 1: First API Call with generateText
- Example 2: Streaming Text with streamText and for-await
- Example 3: Streaming to HTTP Response with toDataStreamResponse
- Example 4: Direct OpenAI SDK — Basic Chat Completion
- Example 5: Direct OpenAI SDK — Streaming with for-await on SSE
- Example 6: Chat UI with useChat Hook in Next.js App Router
- Example 7: System Prompts and Message Roles
- Example 8: Multi-Turn Conversation — Managing Messages Array
- Example 9: Switching Models — Provider Swap Pattern
- Example 10: Temperature, maxTokens, and Model Settings
- Example 11: Text Completion with useCompletion Hook
- Example 12: Environment Variable and API Key Management
- Example 13: Building a Simple Q&A Endpoint
- Example 14: Error Handling and Rate Limits with try/catch
- Example 15: Counting Tokens and Tracking Usage
- Example 16: Prompt Templates with TypeScript Template Literals
- Example 17: Streaming to the Browser via Route Handler SSE
- Example 18: Streaming with ReadableStream in Node.js
- Example 19: Abort Signal and Request Cancellation
- Example 20: Image Input to a Vision Model
- Example 21: Generating a Single Embedding Vector with embed()
- Example 22: Cosine Similarity with cosineSimilarity()
- Example 23: Simple Semantic Search — Embed and Sort by Similarity
- Example 24: Structured Output with generateObject and Zod
- Example 25: Simple Classification with z.enum
- Example 26: Streaming Structured Output with streamObject
- Example 27: Reading Streaming Partial Objects with useObject
- Example 28: Building a Minimal Chatbot — End-to-End
Intermediate (Examples 29–57)
- Example 29: Batch Embedding Documents with embedMany()
- Example 30: Storing Embeddings in pgvector with Drizzle ORM
- Example 31: Storing Embeddings in Pinecone
- Example 32: Storing Embeddings in Qdrant
- Example 33: HNSW Index Setup and Cosine-Distance Query
- Example 34: Basic RAG Pipeline — Embed, Retrieve, Inject
- Example 35: RAG Chatbot with addResource and getInformation Tools
- Example 36: Document Chunking Strategies
- Example 37: Ingesting PDFs for RAG with LlamaIndex LiteParse
- Example 38: Hybrid Search — Keyword and Semantic with Weaviate
- Example 39: Reranking Retrieved Documents with rerank()
- Example 40: Single Tool Definition with tool()
- Example 41: Multiple Tools in streamText — Search and Calculate
- Example 42: Multi-Step Tool Use with stopWhen: stepCountIs
- Example 43: Forcing a Specific Tool with toolChoice
- Example 44: Human-in-the-Loop with needsApproval
- Example 45: Streaming Tool Call Inputs in Real Time
- Example 46: Tool Calling with OpenAI SDK Directly
- Example 47: OpenAI Structured Outputs with json_schema Response Format
- Example 48: LangChain.js — LCEL Chain with pipe()
- Example 49: LangChain.js — RAG Chain with createRetrievalChain
- Example 50: Image Generation with generateImage()
- Example 51: Image Editing and Inpainting via Reference Images
- Example 52: Audio Transcription with Whisper
- Example 53: Speech Synthesis with AI SDK
- Example 54: Multimodal Chat — Images and Text in the Same Conversation
- Example 55: Middleware Pattern with wrapLanguageModel()
- Example 56: Provider Fallback — Try Claude, Fall Back to GPT-4o
- Example 57: MCP Client — Connecting AI SDK to an MCP Server
Advanced (Examples 58–85)
- Example 58: ToolLoopAgent — Define Once, Use Across App Contexts
- Example 59: prepareStep — Dynamic Model Switching Per Step
- Example 60: prepareStep — Context Window Management
- Example 61: Forced Tool-Call Termination Pattern
- Example 62: Custom Manual Agent Loop with generateText
- Example 63: OpenAI Agents SDK — Defining an Agent with Handoffs
- Example 64: OpenAI Agents SDK — Guardrails and Safety Checks
- Example 65: OpenAI Agents SDK — Session Management and Conversation State
- Example 66: Mastra Agents — Agent with Persistent Memory
- Example 67: Mastra Workflows — Deterministic Multi-Step Pipeline
- Example 68: LangChain LangGraph — Stateful Agent with Cycles
- Example 69: Multi-Agent Orchestration — Planner and Specialist Workers
- Example 70: Multi-Agent — Parallel Task Execution with Fan-Out/Fan-In
- Example 71: Multi-Agent — Competitive Evaluation (Best-of-N)
- Example 72: Building an MCP Server in TypeScript
- Example 73: MCP OAuth Integration for Authenticated External Services
- Example 74: Production RAG — Chunking, Metadata Filtering, and Reranking
- Example 75: Caching — Prompt Caching and Response Caching
- Example 76: Streaming to React Server Components
- Example 77: Edge-Optimized AI Endpoint
- Example 78: Rate Limiting and Cost Control in Production
- Example 79: AI SDK DevTools — Inspecting Agent Flows Locally
- Example 80: LangSmith Tracing — Instrumenting LangChain.js Agents
- Example 81: Braintrust Evaluation — Scoring LLM Output with TypeScript CI/CD
- Example 82: LLM-as-Judge Evaluation Pattern
- Example 83: Input Sanitization and Prompt Injection Defense
- Example 84: Implementing RAG Evaluation with RAGAS
- Example 85: Full-Stack AI App Deployment Checklist
Last updated April 28, 2026