LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 2024 min read

The JavaScript/TypeScript AI Ecosystem

Learn the Vercel AI SDK and LangChain.js, the two most widely used tools for building AI features in JavaScript and TypeScript.

Introduction

Every example so far has used Python, but the frontend and full-stack side of generative AI leans heavily on JavaScript and TypeScript — chat UIs need to stream tokens straight into a React component, and a Next.js API route is a natural place to call an LLM from. This lesson covers the two tools most used to do that.

What You Will Learn
  • Why frontend-facing AI features often reach for JS/TS-native tooling instead of calling a Python backend.
  • How to stream an LLM response with the Vercel AI SDK.
  • How LangChain.js compares to its Python counterpart from lesson 9.

A Real-Life Analogy First

Think of a restaurant's front-of-house waiter versus its back-of-house kitchen staff. The kitchen (Python, most of this course) focuses purely on producing the dish correctly. The waiter (JavaScript/TypeScript on the frontend) focuses on a completely different set of concerns: bringing food to the table promptly, refilling water without being asked, and updating the table the moment something is ready rather than making them wait in silence for the whole meal to be plated. Streaming a chat response token-by-token into a UI is exactly that "waiter" job — the words are ready before the full response is, and good UI shows them immediately instead of making the user stare at a blank screen.

Why JS/TS Has Its Own Ecosystem

A chat interface needs to display tokens as they stream in, handle React state updates efficiently, and often runs at the edge for low latency — problems specific to the frontend/full-stack world that a Python backend doesn't directly solve. JS/TS-native SDKs are built with exactly those concerns in mind.

The Vercel AI SDK

Use case: the Vercel AI SDK (the ai package) provides a unified interface across LLM providers plus React/Vue/Svelte hooks purpose-built for streaming chat UIs — useChat() alone handles message state, streaming, and re-renders that would otherwise take significant custom code.

npm install ai @ai-sdk/openai

Vercel AI SDK in Action

// app/api/chat/route.ts (Next.js API route)
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: openai('gpt-4o-mini'),
messages,
});
return result.toDataStreamResponse();
}
// app/page.tsx (a React client component)
'use client';
import { useChat } from 'ai/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div>
{messages.map((m) => (
<p key={m.id}><strong>{m.role}:</strong> {m.content}</p>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
</form>
</div>
);
}
What Happens

Click Run to see what this code prints.

Provider-Agnostic, Just Like LangChain

Swapping openai('gpt-4o-mini') for anthropic('claude-sonnet-5') (with the @ai-sdk/anthropic package) is the only change needed to switch providers — the same design principle seen in LangChain's Python wrappers back in lesson 9.

LangChain.js

Use case: LangChain.js mirrors the Python LangChain library from lesson 9 — prompt templates, provider-agnostic model wrappers, and LCEL-style chain composition — for projects that want the same orchestration patterns without leaving the JS/TS ecosystem.

npm install langchain @langchain/openai
import { ChatOpenAI } from '@langchain/openai';
import { ChatPromptTemplate } from '@langchain/core/prompts';
const model = new ChatOpenAI({ model: 'gpt-4o-mini', temperature: 0.3 });
const prompt = ChatPromptTemplate.fromMessages([
['system', 'You are a concise technical writer.'],
['user', 'Explain {topic} to a beginner in two sentences.'],
]);
const chain = prompt.pipe(model);
const result = await chain.invoke({ topic: 'database indexing' });
console.log(result.content);
Terminal Output

Click Run to see what this code prints.

Choosing Between Them

SituationRecommendation
Building a streaming chat UI in React/Next.jsVercel AI SDK — its hooks solve this directly
Building a complex multi-step chain or agent in NodeLangChain.js — mirrors the Python orchestration patterns
Both a rich UI and complex orchestrationUse both together — the AI SDK for the UI layer, LangChain.js for the pipeline

Common Mistakes

Avoid These Mistakes
  • Calling an LLM provider directly from client-side code, exposing an API key in the browser bundle — always route calls through a server API route.
  • Rebuilding streaming UI state management by hand instead of using useChat()'s built-in handling.
  • Assuming LangChain.js has 100% feature parity with Python LangChain — check current docs, as some integrations lag between the two.

Best Practices

  • Always call provider SDKs from a server-side route or server component, never directly from client-side code.
  • Reach for the Vercel AI SDK first for any chat-style UI — its hooks solve the hardest part (streaming state) for you.
  • Reuse this course's Python examples conceptually — the patterns (chains, RAG, agents) transfer directly even when the exact syntax differs.

Frequently Asked Questions

No — both the Vercel AI SDK and LangChain.js can call provider APIs directly from a Node/Next.js server, with no Python involved.

No — it also supports plain React, Vue, Svelte, and Node backends generally, though it is most commonly seen paired with Next.js.

Yes — Pinecone, Weaviate, and Chroma all publish official JavaScript/TypeScript clients alongside their Python SDKs.

You can start here for the JS-specific syntax, but the concepts (provider SDKs, chains, RAG) were introduced with full explanations in lessons 5, 9, and 15 in Python — read those for the "why" even if you'll write the "how" in JavaScript, since this lesson deliberately doesn't re-explain concepts already covered earlier in the course.

Key Takeaways

  • The Vercel AI SDK specializes in streaming chat UIs with React/Vue/Svelte hooks like useChat().
  • LangChain.js mirrors Python LangChain's orchestration patterns for JS/TS projects.
  • Provider calls must always happen server-side, never directly from client-side code.
  • Most vector databases and providers covered earlier in this course also ship official JS/TS SDKs.

Summary

The JS/TS side of this ecosystem is not a separate world from the Python examples earlier in this course — it is the same patterns (provider calls, chains, streaming) expressed in the language your frontend already speaks.

Lesson 20 Completed
  • You understand why JS/TS has its own AI tooling ecosystem.
  • You can stream a chat response with the Vercel AI SDK.
  • You know how LangChain.js compares to its Python counterpart.
Next Lesson →

Model Context Protocol (MCP) & Tool-Use Standards