Build an AI YouTube Video Summarizer with LangChain and Next.js
A working YouTube summarizer built with LangChain and Next.js: fetch the transcript, chunk it with a text splitter, summarize each chunk, then combine the results into one final summary - full code for every step.
00:08:00 · SEP 26, 2026 · UPDATED OCT 09, 2026
Quick answer
Install LangChain
This tutorial uses the current LangChain JS package split - core primitives, the OpenAI integration, and text splitters each live in their own package:
npm install langchain @langchain/core @langchain/openai @langchain/textsplittersFetch the transcript
Get a key from the dashboard (100 free credits, no card required), then call the transcript endpoint from a Next.js route handler:
// app/api/summarize/route.ts
import { NextResponse } from 'next/server';
async function fetchTranscript(videoId: string) {
const res = await fetch(
`https://getyoutubetranscript.com/api/v1/transcript?v=${videoId}`,
{ headers: { Authorization: `Bearer ${process.env.GETYOUTUBETRANSCRIPT_API_KEY}` } }
);
const json = await res.json();
if (!json.success) throw new Error(json.message);
// json.data.transcript is the full plain-text transcript,
// json.data.word_count is its length in words.
return json.data as { transcript: string; word_count: number; title: string };
}transcript is the full plain-text transcript and word_count is its length - useful for deciding whether to skip the chunking step entirely on a short video.
Split the transcript into chunks
A long transcript can easily exceed what's comfortable to send an LLM in one prompt, so split it first. RecursiveCharacterTextSplitter tries to break on paragraph, then sentence, then word boundaries, in that order, so chunks stay readable rather than cutting mid-sentence:
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 4000,
chunkOverlap: 200,
});
const chunks = await splitter.splitText(transcript);
// A 20-minute video is usually 1-2 chunks; a 2-hour one might be 8-10.chunkOverlap keeps a little context from the end of one chunk at the start of the next, so a sentence split across the boundary isn't lost to either chunk.
Summarize each chunk
ChatPromptTemplate + a model + StringOutputParser piped together (LangChain's expression language, LCEL) is the current, stable way to build a chain - each chunk gets its own short summary in parallel:
import { ChatOpenAI } from '@langchain/openai';
import { ChatPromptTemplate } from '@langchain/core/prompts';
import { StringOutputParser } from '@langchain/core/output_parsers';
const model = new ChatOpenAI({ model: 'gpt-4.1-mini', temperature: 0 });
const chunkPrompt = ChatPromptTemplate.fromTemplate(
'Summarize this part of a video transcript in 3-4 sentences, keeping any concrete facts, numbers, or names:\n\n{chunk}'
);
const chunkChain = chunkPrompt.pipe(model).pipe(new StringOutputParser());
const chunkSummaries = await Promise.all(
chunks.map((chunk) => chunkChain.invoke({ chunk }))
);Running these in parallel with Promise.all matters for latency: a 10-chunk transcript summarized one-by-one takes 10x as long as summarizing all 10 at once.
Combine the chunk summaries into one
The chunk summaries are short enough now to fit in a single prompt together, so a second chain combines them into the final result:
const finalPrompt = ChatPromptTemplate.fromTemplate(
'These are summaries of consecutive parts of the same video transcript, titled "{title}".\n' +
'Combine them into one coherent summary (a short paragraph, then 3-5 key takeaways as bullet points):\n\n{summaries}'
);
const finalChain = finalPrompt.pipe(model).pipe(new StringOutputParser());
const summary = await finalChain.invoke({
title,
summaries: chunkSummaries.join('\n\n'),
});
return NextResponse.json({ summary });This two-pass structure (summarize chunks, then summarize the summaries) is what LangChain's own map-reduce pattern does under the hood - written out explicitly here instead of using a legacy helper chain, since summarization-chain helper APIs have changed across LangChain versions and plain LCEL primitives are the more stable foundation to build on.
Why fetch the transcript from an API instead of scraping it yourself
The transcript-fetching step above is the one part of this pipeline that gets genuinely harder in production - self-hosted scraping libraries commonly get blocked once deployed to a cloud server. A hosted YouTube Transcript API is built specifically to keep that step working so the rest of this pipeline doesn't have to think about it.
Full endpoint reference, authentication, and rate limits are in the API docs. Get a key with 100 free credits from the dashboard to try this end to end.
LangChain summarizer FAQs
Do I need to chunk short videos too?
Not necessarily - check word_count from the transcript response first. A 10-minute video's transcript is usually well under what a single modern LLM prompt can hold, so you can skip straight to one summarization call and skip the chunking/combining steps entirely.
Why RecursiveCharacterTextSplitter instead of a fixed character split?
A fixed split cuts wherever the character count lands, potentially mid-sentence or mid-word. RecursiveCharacterTextSplitter tries paragraph breaks first, then sentences, then words, only falling back to a hard cut if it has to - so chunks stay coherent for the model to summarize.
Can I use a different LLM provider instead of OpenAI?
Yes - LangChain's chat model classes share the same interface, so swapping ChatOpenAI for another provider's LangChain integration (Anthropic, Google, etc.) doesn't change anything else in the pipeline.
Why pipe a prompt, model, and output parser together instead of using LLMChain?
LLMChain is LangChain's older chain-building API. The piped/LCEL style (prompt.pipe(model).pipe(parser)) is the current recommended pattern - it composes more predictably and is what LangChain's own newer documentation builds around.
What does this cost to run?
Two costs: the LLM calls (one per chunk, plus one to combine - billed by your model provider), and 1 credit per transcript fetch from the transcript API. 100 free credits cover the fetch side while testing.
What happens if the video has no transcript at all?
The transcript endpoint returns a clear error code (TRANSCRIPT_DISABLED or TRANSCRIPT_NOT_FOUND) rather than an empty string - check for success: false before handing anything to the text splitter, since summarizing an empty transcript wastes an LLM call for nothing.
Related
- YouTube Captions by the Numbers: What 30,000 Transcripts Show (2026)
- Faceless YouTube Channel with AI: Research with Transcripts, Write Scripts YouTube Will Pay For
- How to Translate a YouTube Video: Transcript and Subtitles in Any Language
- How to Get a YouTube Transcript into ChatGPT (Any Plan, Business Plugins and Codex)
- How to Get a YouTube Transcript into Claude (Claude.ai, Desktop and Claude Code)
- We Benchmarked Three Ways to Get a YouTube Transcript: Home Connection vs Cloud Server
- YouTube Transcript Errors Explained: IpBlocked, RequestBlocked, TranscriptsDisabled and the Rest
- We Read 191 Starter Story Founder Interviews. Here Is What Actually Got Them Customers
- How to Get the Transcript of a YouTube Video (Computer, Phone, and Download)
- n8n YouTube Transcript Failing on Your Server? Why YouTube Blocks It and How to Fix It
- YouTube API Quota Exceeded: Causes and Fixes
- YouTube Transcript API Rate Limit: What It Is and How to Handle 429s
- YouTube Transcript MCP Server: Setup Guide for Claude and Other AI Tools
- YouTube Transcripts in n8n: HTTP Request Workflow Guide
- Migrating from Supadata: A Field Guide
- How to Get YouTube Transcripts in Python
- Migrating from TranscriptAPI.com: A Field Guide
- How to Build a RAG Pipeline with YouTube Transcripts
- The Best YouTube Transcript APIs in 2026
- GetYouTubeTranscript vs TranscriptAPI
- GetYouTubeTranscript vs youtubetotranscript.com
- GetYouTubeTranscript vs youtube-transcript.io
- GetYouTubeTranscript vs NoteGPT
- GetYouTubeTranscript vs Tactiq
- GetYouTubeTranscript vs Glasp
- GetYouTubeTranscript vs Eightify
- GetYouTubeTranscript vs Supadata
- GetYouTubeTranscript vs DownSub
- GetYouTubeTranscript vs youtubetranscript.dev























































