AutoGen Tutorial (TypeScript): handling long documents for intermediate developers

By Cyprian AaronsUpdated 2026-04-21
autogenhandling-long-documents-for-intermediate-developerstypescript

This tutorial shows you how to build a TypeScript AutoGen workflow that can ingest, split, summarize, and answer questions over long documents without blowing past model context limits. You need this when you’re dealing with policy PDFs, claims files, contract bundles, or any other input that is too large to send to a model in one shot.

What You'll Need

  • Node.js 18+ and npm
  • A TypeScript project with ts-node or a build step
  • AutoGen for TypeScript:
    • @autogenai/autogen
  • An OpenAI-compatible API key
    • OPENAI_API_KEY
  • A model with a reasonable context window
    • Example: gpt-4o-mini, gpt-4.1-mini, or similar
  • A long text document in .txt form for the first pass
  • Basic familiarity with AutoGen agents and message passing

Step-by-Step

  1. Start by installing dependencies and setting up your environment. Keep the document handling logic separate from agent logic so you can swap in PDF extraction later without rewriting the workflow.
npm init -y
npm install @autogenai/autogen dotenv
npm install -D typescript ts-node @types/node
  1. Create a small helper that chunks long text into token-safe pieces. For production, I prefer character-based chunking with overlap because it is deterministic and easy to test before you move to token-aware splitting.
export function chunkText(
  text: string,
  chunkSize = 6000,
  overlap = 500,
): string[] {
  const chunks: string[] = [];
  let start = 0;

  while (start < text.length) {
    const end = Math.min(start + chunkSize, text.length);
    chunks.push(text.slice(start, end));
    if (end === text.length) break;
    start = Math.max(0, end - overlap);
  }

  return chunks;
}
  1. Load the document and summarize each chunk with an AutoGen assistant agent. The key pattern here is map-reduce: summarize each chunk first, then combine those summaries into one final answerable representation.
import "dotenv/config";
import fs from "node:fs/promises";
import { AssistantAgent } from "@autogenai/autogen";
import { chunkText } from "./chunkText";

const clientConfig = {
  apiKey: process.env.OPENAI_API_KEY!,
};

const modelConfig = {
  model: "gpt-4o-mini",
};

async function main() {
  const doc = await fs.readFile("./document.txt", "utf8");
  const chunks = chunkText(doc);

  const summarizer = new AssistantAgent({
    name: "summarizer",
    modelClient: clientConfig,
    model: modelConfig,
    systemMessage:
      "Summarize the provided document chunk in concise bullet points. Keep names, dates, obligations, exceptions, and numeric values.",
  });

  const summaries: string[] = [];

  for (let i = 0; i < chunks.length; i++) {
    const response = await summarizer.run({
      messages: [
        {
          role: "user",
          content: `Chunk ${i + 1}/${chunks.length}:\n\n${chunks[i]}`,
        },
      ],
    });

    summaries.push(response.messages.at(-1)?.content?.toString() ?? "");
  }

  console.log(summaries.join("\n\n---\n\n"));
}

main();
  1. Add a second pass that answers questions using only the compressed summaries. This keeps your final prompt small and makes the system much more stable than sending the full document every time.
import { AssistantAgent } from "@autogenai/autogen";

const answerer = new AssistantAgent({
  name: "answerer",
  modelClient: clientConfig,
  model: modelConfig,
  systemMessage:
    "Answer questions using only the provided summaries. If the summaries do not contain enough information, say what is missing.",
});

async function answerQuestion(summaries: string[], question: string) {
  const response = await answerer.run({
    messages: [
      {
        role: "user",
        content:
          `Document summaries:\n\n${summaries.join("\n\n")}\n\nQuestion:\n${question}`,
      },
    ],
  });

  return response.messages.at(-1)?.content?.toString() ?? "";
}
  1. Wire both passes together and run a real question against your document. In practice, this is where you’ll add retrieval filters or metadata tags later, but this version is enough to prove the pipeline works end-to-end.
async function main() {
  const doc = await fs.readFile("./document.txt", "utf8");
  const chunks = chunkText(doc);

  const summarizer = new AssistantAgent({
    name: "summarizer",
    modelClient: clientConfig,
    model: modelConfig,
    systemMessage:
      "Summarize the provided document chunk in concise bullet points. Keep names, dates, obligations, exceptions, and numeric values.",
  });

  const summaries: string[] = [];

  for (let i = 0; i < chunks.length; i++) {
    const response = await summarizer.run({
      messages: [
        { role: "user", content: `Chunk ${i + 1}/${chunks.length}:\n\n${chunks[i]}` },
      ],
    });

    summaries.push(response.messages.at(-1)?.content?.toString() ?? "");
  }

  const result = await answerQuestion(
    summaries,
    "What are the main obligations and deadlines mentioned in this document?",
  );

  console.log(result);
}

main();

Testing It

Run the script against a long .txt file that is clearly larger than your target model’s comfortable prompt size. You should see per-chunk summaries first, then a final answer that references details from across multiple sections instead of only the beginning of the file.

If the output gets vague, reduce chunkSize or increase overlap so important clauses don’t get split across boundaries. If you see hallucinated details, tighten the system message to force “answer only from summaries” behavior.

For real validation, test three cases:

  • A document with repeated terms and cross-references
  • A document with tables or dense numeric data
  • A document where the answer exists only in late sections

Next Steps

  • Replace naive character chunking with token-aware splitting using your tokenizer of choice.
  • Add metadata per chunk so you can trace answers back to exact source sections.
  • Move from summary-only answering to retrieval over embeddings when documents become too large for full summarization passes.

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