Anyone who has ground spices by hand knows what's in a ready-made spice paste. They can judge when the paste is worth it, and they can still cook when there is no packet. You have done the grinding: in the earlier lessons you wrote, by hand, the code for each provider's different request format, stream parsing (splitting a stream into events), the tool loop, chat state and the RAG steps. AI frameworks such as the Vercel AI SDK and LangChain.js are the paste: libraries that already contain the code for those jobs. This lesson compares hand-written code, the Vercel AI SDK and LangChain.js on the same Bookshop jobs.
What a framework wraps
| Job | By hand (earlier lessons) | Vercel AI SDK | LangChain.js |
|---|---|---|---|
| Calling a model, any provider | fetch to /v1/responses or /v1/messages, different JSON shapes |
generateText({ model: openai("gpt-6-luna") }) |
initChatModel("openai:gpt-6-luna") |
| Streaming | readSSE, TextDecoder, each provider's event names |
streamText(...).textStream |
model.stream(...) |
| The tool loop | function_call, call_id, a round cap |
tool({...}) + stopWhen: isStepCount(3) |
createAgent({ tools }) |
| Chat state in React | an array of messages, statuses, AbortController |
useChat() from @ai-sdk/react |
useStream() from @langchain/react |
| RAG | chunk, embed, cosine, top k | embedMany, cosineSimilarity |
RecursiveCharacterTextSplitter, MemoryVectorStore |
| Confirming write tools | an awaiting_confirmation status |
toolApproval |
humanInTheLoopMiddleware |
Versions on npm as of October 2026: the Vercel AI SDK's main package, ai, 7.0.137 (major 7, the first number of the version, released 25 June 2026), @ai-sdk/react 4.0.140, @ai-sdk/openai 4.0.91; langchain 1.5.16 and @langchain/core 1.2.17. None of them is installed in the Bookshop project, so the Lab shows the framework code but runs the hand-written version.
Vercel AI SDK
The Vercel AI SDK is a TypeScript library from Vercel for web apps. It gives the same set of functions for many providers (through the packages @ai-sdk/openai, @ai-sdk/anthropic and others), plus React hooks (functions such as useState). Here is the assistant's route with one tool, following the AI SDK 7 docs:
import { streamText, tool, isStepCount, convertToModelMessages, createUIMessageStreamResponse, toUIMessageStream } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
export async function POST(req) {
const { messages } = await req.json();
const result = streamText({
model: openai("gpt-6-luna"),
providerOptions: { openai: { reasoningEffort: "none" } },
instructions: RULES,
messages: await convertToModelMessages(messages),
tools: {
checkStock: tool({
description: "Check a book's stock and price at the Bookshop.",
inputSchema: z.object({ title: z.string().min(1).max(100) }),
execute: async ({ title }) => (await fetch(`https://api.bookshop.test/books?q=${encodeURIComponent(title)}`)).json(),
}),
},
stopWhen: isStepCount(3),
});
return createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }) });
}
In a React component, the chat needs only one hook:
import { useChat } from "@ai-sdk/react";
import { DefaultChatTransport } from "ai";
const { messages, sendMessage, status, stop, regenerate } = useChat({
transport: new DefaultChatTransport({ api: "/api/assistant" }),
});
openai("gpt-6-luna")uses the Responses API by default (according to its OpenAI provider docs).providerOptionsholds settings for one provider only, here reasoning effortnonefor OpenAI.inputSchemais written with zod (a schema library for JavaScript). That schema is sent to the model, and is also used to validate the model's arguments.executeruns on your server.stopWhen: isStepCount(3)caps the rounds: at most 3 steps, and one step = one model call plus the tools it asked for. WithoutstopWhenthe default isisStepCount(1): the tool runs, but the model isn't called again to write the answer.useChathandles the message list (each message's content is inparts), thestatus(submitted,streaming,ready,error), Stop (stop) and Try again (regenerate). You wrote all of these yourself in the Building Chat UI lesson.
LangChain.js
LangChain.js is a bigger framework. It focuses on agents and on components that plug together: models, prompts, tools, retrievers (they fetch the relevant chunks of your documents) and vector stores (places that store vectors). An agent is a model plus tools, running in a loop until the model can answer. Its docs (October 2026) present createAgent as the main entry point, LangGraph for low-level orchestration (you control every step yourself), and Deep Agents for more complete agents. Install it with npm install langchain @langchain/core (Node 22 or later), plus a provider package such as @langchain/openai.
Here is the Bookshop assistant in LangChain.js. checkStock is your function that gets the stock from the Bookshop API:
import { createAgent, tool } from "langchain";
import * as z from "zod";
const stockTool = tool(checkStock, { name: "checkStock", description: "Check a book's stock and price.", schema: z.object({ title: z.string().min(1).max(100) }) });
const agent = createAgent({ model: "openai:gpt-6-luna", tools: [stockTool], systemPrompt: RULES });
const result = await agent.invoke({ messages: [{ role: "user", content: "Is The Rainbow Troops still in stock?" }] });
console.log(result.messages.at(-1)?.text);
For RAG, the components just plug together:
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
import { OpenAIEmbeddings } from "@langchain/openai";
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";
const splitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000, chunkOverlap: 200 });
const store = new MemoryVectorStore(new OpenAIEmbeddings({ model: "text-embedding-3-small" }));
await store.addDocuments(await splitter.splitDocuments(documents));
const sources = await store.similaritySearch("Is there a novel about a school on Belitung?", 2);
model: "openai:gpt-6-luna"uses theprovider:modelformat. To switch provider, change that text and install the provider's package.createAgentruns the tool loop until the model stops asking for tools.RecursiveCharacterTextSplittercutsdocumentsinto chunks.addDocumentsembeds the chunks and stores them in aMemoryVectorStore(a vector store in memory).similaritySearch(..., 2)returns the top k, with k = 2.chunkSize: 1000, chunkOverlap: 200are the numbers from LangChain's RAG tutorial, in characters.
Switching provider: measured, not claimed
The hand-written tool loop of the Bookshop assistant is 16 lines long. If you move from gpt-6-luna (OpenAI) to claude-haiku-5-5 (Anthropic), how many lines change: about 1, about 3, or more than 10? And if the loop is written with the Vercel AI SDK? Guess before you look at the table.
The Lab compares the OpenAI version and the Anthropic version of the same tool loop, line by line. A comparison like this is called a diff:
| Style | Lines | Switching to Anthropic |
|---|---|---|
| By hand | 16 | 10 lines removed, 11 added: the URL, headers, input_schema, tool_use, tool_result, how the text is read |
| Vercel AI SDK | 13 | 3 removed, 2 added: the provider import, the model line, and the OpenAI-only providerOptions |
| LangChain.js | 6 | 1 removed, 1 added: the model text, plus npm install @langchain/anthropic |
So the hand-written version changes by more than 10 lines, and the AI SDK version by about 3. For RAG, all three ways keep OpenAI embeddings, because the "Embeddings" page in Anthropic's docs says Anthropic doesn't offer its own embedding model, and its example uses Voyage AI (another provider of embedding models).
When to use no framework
A framework is worth it when you use many of its parts. No framework makes more sense when:
- There is only one endpoint, one provider, no tools.
fetchor the officialopenaiSDK is enough. - The backend isn't JavaScript, while both of these frameworks are JavaScript libraries. A Bookshop backend in Laravel (PHP) just needs
Http::postto the Responses API, with the API key, as in the OpenAI API lesson. - You are looking for a bug in the layer underneath, the code the framework wraps (for example a stream that gets cut, or a
call_idthat doesn't match). A framework makes things easier, but doesn't remove that layer. - You don't want to follow every change in the framework. On npm,
aireleased major 5 (31 July 2025), 6 (22 December 2025) and 7 (25 June 2026). That is three majors in a year, and examples for old versions often stop working.
⚠️ Stale examples and the false comfort of a framework
- Copying old examples.
import { useChat } from "ai/react"withinputandhandleSubmit, ortoDataStreamResponse(), come from older versions. In AI SDK 7, you importuseChatfrom@ai-sdk/react, send a message withsendMessage({ text }), and send the answer's stream withcreateUIMessageStreamResponse. Always check the version inpackage.json. - Forgetting
stopWhen. The tool runs, but the final answer is empty. - Thinking the framework secured everything. A zod schema only checks the shape of the arguments. Permissions, value limits and confirming write tools are still your job. For confirmation, the framework only gives you helpers (
toolApproval,humanInTheLoopMiddleware). - Calling the provider from the browser. A framework doesn't change the rule: the API key stays on the server, and the browser calls your route.
TL;DR: AI frameworks such as the Vercel AI SDK (the ai package 7, October 2026) and LangChain.js (langchain 1.5) are libraries that wrap the code you have already written by hand: each provider's format, streaming (streamText, model.stream), the tool loop (tool + stopWhen: isStepCount(n), createAgent), chat state (useChat from @ai-sdk/react with sendMessage, status, stop, regenerate) and RAG (embedMany + cosineSimilarity, or a document splitter + a vector store). The AI SDK's strength is abstractions (wrappers that hide the details) for streaming, chat state and switching providers. Moving the tool loop from OpenAI to Anthropic: the AI SDK removes 3 lines and adds 2, by hand you remove 10 and add 11. LangChain composes AI components (models, prompts, tools, retrievers that fetch chunks of documents) into agents (a model plus tools in a loop) and pipelines. Use no framework for small endpoints, backends that aren't JavaScript, and looking for bugs in the layer underneath (the code a framework wraps). Validation, permissions, confirming write tools and keeping the API key on the server are still your job.
⚡ A ready-made paste for AI code
Meaning: AI frameworks such as the Vercel AI SDK and LangChain.js are libraries that wrap the code you have already written by hand: each provider's request format, stream parsing (splitting a stream into events), the tool loop, chat state and the RAG steps. A framework is like a ready-made spice paste, and hand-written code is like spices you grind yourself. Because you know what's inside, you can judge when a framework is worth it.
⚡ One hook for the whole chat
Write:
import { useChat } from "@ai-sdk/react";
import { DefaultChatTransport } from "ai";
const { messages, sendMessage, status, stop, regenerate } = useChat({
transport: new DefaultChatTransport({ api: "/api/assistant" }),
});
Meaning: This is Vercel AI SDK code in a React component. useChat is a hook (a React function such as useState) that handles the message list, the status, sending (sendMessage), Stop (stop) and Try again (regenerate). Through DefaultChatTransport, the hook calls the route /api/assistant. In that route, streamText (it calls the model with streaming) with tool and stopWhen: isStepCount(3) (at most 3 model calls) replaces your tool loop.
⚡ Switching provider
Guess first: the Bookshop assistant's hand-written tool loop is 16 lines long. Moving from gpt-6-luna (OpenAI) to claude-haiku-5-5 (Anthropic), how many lines change?
More than 10: 10 lines removed, 11 added, because the two providers' requests and responses have different shapes. With the Vercel AI SDK, only 3 removed and 2 added, because the provider package handles the difference.
Meaning: That is the Vercel AI SDK's strength: abstractions (wrappers that hide the details) for streaming, chat state and switching providers. LangChain.js composes AI components (models, prompts, tools, retrievers that fetch chunks of documents) into agents (a model plus tools in a loop) and pipelines.
⚡ When to use no framework
Meaning: Use no framework for a small endpoint with one provider, a backend that isn't JavaScript, or when you look for a bug in the layer underneath (the code a framework wraps). Validation, permissions, confirming write tools and keeping the API key on the server are still your job.