npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

recursive-lm

v0.1.0

Published

Process arbitrarily long contexts by recursively decomposing prompts — based on the Recursive Language Models paper

Readme

recursive-lm

Process arbitrarily long contexts by recursively decomposing prompts — based on the Recursive Language Models paper.

npm License: MIT

What is this?

Traditional LLMs have a fixed context window. When your document exceeds it, you lose information. Recursive LM solves this by treating the prompt as an external environment and letting the model programmatically decompose and recursively process it.

The model gets a REPL interface with three tools:

  • read(start, end) — read a character range from the document
  • llm_query(query, context) — spawn a recursive sub-call to process a chunk
  • FINAL(answer) — return the final answer

This enables divide-and-conquer strategies where the model automatically chunks, summarizes, and synthesizes — processing documents of 1M+ tokens with models that only have 32k-128k context windows.

Install

npm install recursive-lm

Quick Start

import { RecursiveLM } from 'recursive-lm';
import type { LLMProvider, Message } from 'recursive-lm';

// 1. Implement the provider interface for your LLM
class MyProvider implements LLMProvider {
  async generate(messages: Message[]): Promise<string> {
    // Call OpenAI, Anthropic, local model, etc.
    const response = await callYourLLM(messages);
    return response;
  }
}

// 2. Create the RecursiveLM instance
const rlm = new RecursiveLM({
  provider: new MyProvider(),
  maxDepth: 5,       // max recursion depth
  chunkSize: 8000,   // characters per chunk
  maxIterations: 20, // max loop iterations
});

// 3. Query any length document
const answer = await rlm.query(
  'What are the key findings?',
  veryLongDocument   // can be millions of characters
);

API

RecursiveLM

The main class. Wires together Environment, Scaffold, and Sandbox.

new RecursiveLM(config: RLMConfig)

| Option | Type | Default | Description | |---|---|---|---| | provider | LLMProvider | required | Your LLM adapter | | maxDepth | number | 5 | Max recursion depth for llm_query() | | chunkSize | number | 8000 | Chunk size in characters | | maxIterations | number | 20 | Max scaffold loop iterations | | onStep | (e: StepEvent) => void | — | Called on each loop iteration | | onRecurse | (e: RecurseEvent) => void | — | Called on recursive sub-calls | | onFinal | (e: FinalEvent) => void | — | Called when answer is produced |

Methods

  • query(question, context) — Process a single document
  • queryMultiDoc(question, documents) — Process multiple named documents
  • queryWithEnvironment(question, environment) — Use a pre-configured Environment

LLMProvider Interface

interface LLMProvider {
  generate(messages: Message[]): Promise<string>;
}

interface Message {
  role: 'system' | 'user' | 'assistant';
  content: string;
}

Advanced: Direct Component Access

For fine-grained control, use the components directly:

import { Environment, Scaffold, Sandbox } from 'recursive-lm';

const env = new Environment(8000);
env.addDocument('report', longText);

const scaffold = new Scaffold({
  provider: myProvider,
  maxDepth: 5,
  maxIterations: 20,
});

const answer = await scaffold.run('Summarize the report', env);

How It Works

Based on Algorithm 1 from the paper:

1. Initialize: History H ← [system prompt, user query]
2. Loop:
   a. Call LLM(H) → response
   b. If response contains FINAL(answer) → return answer
   c. If response contains ```repl code:
      - Execute code (read, llm_query, etc.)
      - Append results to H
   d. Repeat

The model learns to perform parallel mapping (processing chunks in parallel recursive calls) and hierarchical reduction (combining summaries into higher-level summaries) — automatically adapting its strategy to the query.

License

MIT