An agnostic Node.js library for summarizing "infinite" texts using binary tree reduction.
LLMs have context window limits. To summarize a book or a 100-page document, you cannot simply send everything at once. Infinity solves this by breaking the text into overlapping chunks and creating a hierarchy of summaries.
- Agnostic: Works with any LLM (OpenAI, OpenRouter, Anthropic, Local models).
- Efficient: Uses binary tree reduction to process chunks in parallel levels.
- Resilient: Built-in concurrency control and retry logic.
- Context-Aware: Supports chunk overlap to preserve meaning at borders.
npm installconstInfinity=require('infinity');constOpenRouterAdapter=require('./adapters/openRouterAdapter');constmodel=newOpenRouterAdapter('YOUR_API_KEY');constsummarizer=newInfinity(model,{chunkSize: 4000});constsummary=awaitsummarizer.summarize(veryLongText);You can connect any provider by implementing a class with a generate(prompt) method:
classMyAdapter{asyncgenerate(prompt){constres=awaitmyApi.call(prompt);returnres.text;}}chunkSize: Max characters per chunk.chunkOverlap: Characters to overlap between chunks.concurrency: Number of simultaneous API calls.maxFinalLength: Desired length for the final output.mapPrompt: Custom prompt for initial chunking (use{{content}}).reducePrompt: Custom prompt for merging summaries (use{{textA}}and{{textB}}).