AutoGen Tutorial (TypeScript): chunking large documents for beginners

By Cyprian AaronsUpdated 2026-04-21
autogenchunking-large-documents-for-beginnerstypescript

This tutorial shows you how to split a large document into smaller chunks in TypeScript, then feed those chunks into an AutoGen workflow safely. You need this when a document is too large for a single model call, or when you want cleaner retrieval, summarization, or per-section analysis.

What You'll Need

  • Node.js 18+
  • A TypeScript project with ts-node or tsx
  • autogen installed
  • An OpenAI API key set as an environment variable
  • A large text file to chunk, such as a policy document, contract, or report
  • Basic familiarity with AutoGen agents and chat messages

Install the package:

npm install autogen
npm install -D typescript tsx @types/node

Set your API key:

export OPENAI_API_KEY="your-key-here"

Step-by-Step

  1. Start with a plain text document loader and a chunking function. For beginners, keep the first version simple: split by word count with overlap so chunks don’t lose context at the boundaries.
import { readFileSync } from "node:fs";

function chunkText(text: string, chunkSize = 800, overlap = 100): string[] {
  const words = text.split(/\s+/).filter(Boolean);
  const chunks: string[] = [];

  for (let start = 0; start < words.length; start += chunkSize - overlap) {
    const slice = words.slice(start, start + chunkSize).join(" ");
    if (slice.trim()) chunks.push(slice);
    if (start + chunkSize >= words.length) break;
  }

  return chunks;
}

const raw = readFileSync("./document.txt", "utf8");
const chunks = chunkText(raw);

console.log(`Loaded ${raw.split(/\s+/).length} words`);
console.log(`Created ${chunks.length} chunks`);
  1. Build a small helper that labels each chunk. In production, metadata matters because you will eventually trace answers back to the exact section that produced them.
type Chunk = {
  id: number;
  text: string;
  wordCount: number;
};

function makeChunks(text: string): Chunk[] {
  return chunkText(text).map((chunk, index) => ({
    id: index + 1,
    text: chunk,
    wordCount: chunk.split(/\s+/).filter(Boolean).length,
  }));
}

const documentChunks = makeChunks(raw);

for (const c of documentChunks.slice(0, 3)) {
  console.log(`Chunk ${c.id}: ${c.wordCount} words`);
}
  1. Create an AutoGen assistant that can summarize one chunk at a time. The key pattern is to keep each message small and deterministic instead of dumping the entire document into one prompt.
import { AssistantAgent } from "autogen";

const assistant = new AssistantAgent({
  name: "chunk_summarizer",
  modelClientOptions: {
    model: "gpt-4o-mini",
    apiKey: process.env.OPENAI_API_KEY,
  },
});

async function summarizeChunk(chunkText: string): Promise<string> {
  const result = await assistant.run([
    {
      role: "user",
      content:
        "Summarize this document chunk in 3 bullet points. Keep names, dates, and obligations intact.\n\n" +
        chunkText,
    },
  ]);

  return result.messages.at(-1)?.content?.toString() ?? "";
}
  1. Run the summarizer over every chunk and collect structured output. This gives you a clean intermediate layer that you can later merge into one final summary or search index.
async function main() {
  const summaries: { id: number; summary: string }[] = [];

  for (const chunk of documentChunks.slice(0, 5)) {
    const summary = await summarizeChunk(chunk.text);
    summaries.push({ id: chunk.id, summary });
    console.log(`Done chunk ${chunk.id}`);
  }

  console.log(JSON.stringify(summaries, null, 2));
}

main().catch(console.error);
  1. If you want better results on long documents, add section-aware splitting before word splitting. This keeps headings together and makes downstream retrieval much easier.
function splitByHeadings(text: string): string[] {
  const parts = text.split(/\n(?=#{1,6}\s)/g).map((p) => p.trim()).filter(Boolean);
  return parts.length > 1 ? parts : [text];
}

function smartChunk(text: string): string[] {
  const sections = splitByHeadings(text);
  const output: string[] = [];

  for (const section of sections) {
    if (section.split(/\s+/).length <= 800) {
      output.push(section);
      continue;
    }
    output.push(...chunkText(section));
  }

  return output;
}

Testing It

Run the script against a real text file that is larger than your chosen chunk size. You should see multiple chunks created and multiple summarization calls completed without hitting token limits.

Check that overlapping content does its job by comparing adjacent summaries for continuity around shared terms or repeated obligations. If important details disappear at boundaries, increase overlap from 100 to 150 words.

Also verify that each returned summary stays focused on one section only. If summaries start mixing topics from distant parts of the document, your chunks are too large.

A good smoke test is to ask the model a follow-up question using only the collected summaries instead of the full source text. If it answers correctly and cites consistent details, your pipeline is working.

Next Steps

  • Add embeddings and store {chunkId, text} in a vector database for retrieval
  • Replace word-based splitting with token-based splitting using tiktoken
  • Add a second AutoGen agent that merges per-chunk summaries into one final report

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides