Overview
Level 1: Foundations · Module 3: Calling LLM APIs from Next.js · 22 min read
Why this matters
Frontend developers venturing into AI are immediately confronted with two competing paradigms: either import official vendor SDKs directly (@anthropic-ai/sdk, openai, @google/genai) or adopt unified abstraction libraries like the Vercel AI SDK (ai, @ai-sdk/openai, @ai-sdk/react). Neither choice is unconditionally superior. Official SDKs provide day-one access to proprietary, model-specific features (like Anthropic prompt caching, OpenAI computer-use, or Gemini 2M context windows), while the Vercel AI SDK provides a standardized API, automated streaming transforms, and powerful React hooks (useChat, useCompletion). Understanding the exact architectural tradeoffs prevents costly mid-project rewrites.
Learning objectives
- Analyze the tradeoffs between official provider SDKs and unified multi-provider wrappers.
- Implement native calls using
@anthropic-ai/sdkandopenai. - Implement unified streaming using Vercel AI SDK Core (
streamText,generateObject). - Understand how React client hooks (
useChat) bind to server stream protocols. - Formulate a technical strategy for choosing or combining both approaches in enterprise codebases.
Architectural Comparison: Native SDKs vs Vercel AI SDK
graph TD
subgraph OfficialProviderSDKs["Direct Official SDKs (Vendor Specific)"]
A["Next.js Server"] -->|"@anthropic-ai/sdk"| Anthropic["Anthropic API<br>(Native Prompt Caching, Thinking Blocks)"]
A -->|"openai"| OpenAI["OpenAI API<br>(Native Audio, Realtime WebSockets)"]
A -->|"Custom Web Stream Parsing"| ClientRaw["Client: Custom EventSource Parser"]
end
subgraph VercelAISDKApproach["Unified Vercel AI SDK (Framework Native)"]
B["Next.js Server"] -->|"ai: streamText({ model: anthropic('claude-3-7-sonnet') })"| UnifiedEngine["AI SDK Core Abstraction"]
UnifiedEngine --> LLMs["OpenAI / Claude / Gemini / Mistral"]
UnifiedEngine -->|"toDataStreamResponse()"| ClientHook["Client: useChat() / useCompletion()"]
end| Decision Metric | Official Provider SDKs | Vercel AI SDK (ai) |
|---|---|---|
| Model Switching | High friction (incompatible schemas & parameters) | 1-line change (openai('...') -> anthropic('...')) |
| Cutting-Edge Features | Day-zero access to vendor betas & unique capabilities | May lag days or weeks behind vendor API launches |
| Frontend Integration | Manual ReadableStream & SSE chunk decoding | Turnkey React hooks (useChat, useCompletion, useObject) |
| Bundle & Dependency | Lean, only vendor dependencies | Extra abstraction layer, tightly coupled to React/Next.js ecosystem |
| Tool Calling / GenUI | Manual recursive function dispatch | Native tools parameter with automated client streaming |
Implementation 1: The Native Provider Approach
When you need vendor-specific features like Anthropic's Prompt Caching (which cuts prompt cost by 90% and latency by 80% on long contexts), the native SDK is the ideal choice:
// app/api/anthropic-native/route.ts
import Anthropic from "@anthropic-ai/sdk";
import { NextRequest } from "next/server";
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY!,
});
export async function POST(req: NextRequest) {
const { prompt } = await req.json();
// Create native Anthropic stream with prompt cache breakpoint
const stream = await anthropic.messages.create({
model: "claude-3-7-sonnet-20250219",
max_tokens: 1024,
stream: true,
system: [
{
type: "text",
text: "You are an expert technical interviewer specializing in React performance and Web Streams.",
// Mark system prompt for caching across turns!
cache_control: { type: "ephemeral" },
},
],
messages: [{ role: "user", content: prompt }],
});
// Convert Anthropic event stream into a standard Web ReadableStream
const readable = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
for await (const chunk of stream) {
if (
chunk.type === "content_block_delta" &&
chunk.delta.type === "text_delta"
) {
controller.enqueue(encoder.encode(chunk.delta.text));
}
}
controller.close();
},
});
return new Response(readable, {
headers: {
"Content-Type": "text/plain; charset=utf-8",
"Cache-Control": "no-cache",
},
});
}Implementation 2: The Vercel AI SDK Approach
When your priority is developer velocity, multi-model flexibility, and turnkey client hooks, the Vercel AI SDK drastically reduces boilerplate:
// app/api/ai-sdk/chat/route.ts
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
// You can seamlessly switch to: import { anthropic } from "@ai-sdk/anthropic";
export const maxDuration = 30;
export async function POST(req: Request) {
const { messages } = await req.json();
// Stream text across any supported LLM provider
const result = streamText({
model: anthropic("claude-3-7-sonnet"),
system: "You are a senior frontend performance engineer.",
messages,
temperature: 0.2,
});
// Automatically streams AI SDK data protocol to useChat()
return result.toDataStreamResponse();
}And in the React client:
// components/ChatWindow.tsx
"use client";
import { useChat } from "ai/react";
export function ChatWindow() {
const { messages, input, handleInputChange, handleSubmit, isLoading, stop } = useChat({
api: "/api/ai-sdk/chat",
});
return (
<div className="flex flex-col h-[500px] border border-gray-200 dark:border-gray-800 rounded-xl overflow-hidden">
<div className="flex-1 overflow-y-auto p-4 space-y-4">
{messages.map((m) => (
<div
key={m.id}
className={`p-3 rounded-lg text-sm max-w-[85%] ${
m.role === "user"
? "ml-auto bg-blue-600 text-white"
: "bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-gray-100"
}`}
>
<span className="font-semibold block mb-1 text-xs opacity-75">
{m.role === "user" ? "You" : "Assistant"}
</span>
{m.content}
</div>
))}
</div>
<form onSubmit={handleSubmit} className="p-3 border-t border-gray-200 dark:border-gray-800 flex gap-2">
<input
value={input}
onChange={handleInputChange}
placeholder="Ask a question..."
className="flex-1 px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-900 text-sm"
/>
{isLoading ? (
<button
type="button"
onClick={stop}
className="px-4 py-2 bg-red-600 text-white text-sm font-medium rounded-lg"
>
Stop
</button>
) : (
<button
type="submit"
className="px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg"
>
Send
</button>
)}
</form>
</div>
);
}Structured Output: generateObject with Zod
The AI SDK also simplifies schema-guaranteed JSON parsing without manual prompt engineering or regex stripping:
// app/actions/sentiment.ts
"use server";
import { generateObject } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
export async function analyzeFeedback(customerText: string) {
const { object } = await generateObject({
model: google("gemini-2-flash"),
schema: z.object({
sentiment: z.enum(["positive", "neutral", "negative"]),
confidence: z.number().min(0).max(1),
keyTopics: z.array(z.string()).max(4),
actionRequired: z.boolean(),
}),
prompt: `Analyze customer feedback: "${customerText}"`,
});
return object; // Fully typed as { sentiment: ..., confidence: ... }
}The Production Decision Framework
Use this rubric when architecting your team's stack:
- Choose Vercel AI SDK when:
- You are building React/Next.js chat or completion interfaces.
- You want out-of-the-box support for
useChat, scroll restoration, and message state. - You anticipate swapping or benchmarking models (e.g., testing DeepSeek, Claude 3.7, and o3-mini side-by-side).
- Choose Native Provider SDKs when:
- You rely on non-standard vendor features (Anthropic Prompt Caching, OpenAI Realtime WebSockets, Gemini Vertex multi-modal video analysis).
- Your application runs in non-React runtimes (Go microservices, background Node workers).
- You cannot afford any additional runtime dependency overhead.
Quick Check
- What client-side hook does the Vercel AI SDK provide to automatically manage message history, streaming tokens, and stop triggers?
- Which method on the Vercel AI SDK result converts an active token stream into a Next.js HTTP response?
- What is one major reason you might choose the native
@anthropic-ai/sdkover a multi-provider wrapper?