# recursive-decomposition

Adopt this skill: https://proskills.md/skills/other/massimodeluisa-recursive-decomposition-skill-skills-recursive-decomposition

Page: https://proskills.md/skills/other/massimodeluisa-recursive-decomposition-skill-skills-recursive-decomposition
This document is the public skill bundle for that page. It contains SKILL.md and every helper file the catalog carries. No login is required.

## File: skills/recursive-decomposition/SKILL.md
size: 8000
upstream: https://github.com/massimodeluisa/recursive-decomposition-skill/blob/main/skills/recursive-decomposition/SKILL.md

```text
---
name: recursive-decomposition
description: "Decompose dense codebase-wide, multi-document, PDF, and aggregation work even when the input fits the context window, following Recursive Language Models (Zhang, Kraska, Khattab, 2025). Use when the user asks to analyse all files, a whole repo, all docs, large PDFs, or to aggregate or multi-hop across scattered sources. Skip one file, one function, a single needle, or a one-page PDF conversion. Triggers: long context, context rot, large codebase, many files, all files, big document, multi-document, PDF, aggregate, summarize everything, codebase-wide, multi-hop, recursive, sub-agents, map-reduce."
license: MIT
metadata:
  author: massimodeluisa
  version: "1.2.0"
  paper: https://arxiv.org/abs/2512.24601
---

# Recursive Decomposition

Long inputs rot. Details get missed, distant parts get glued together by guesswork, and the reasoning drifts. The RLM paper calls it context rot. Do not load the whole input into the window. Treat it as an environment you query: size it, narrow it, split it, hand independent parts to sub-agents, verify on a small window, synthesise in code. Based on [Recursive Language Models](https://arxiv.org/abs/2512.24601) (Zhang, Kraska, Khattab, 2025).

## How to use

- `/recursive-decomposition`: apply the protocol below to the current task.
- `/recursive-decomposition <path or question>`: size that input first, then run the protocol on it.

## When it applies

Fit is not the test. Dense work can rot inside a million-token window. In the paper, OOLONG-Pairs is 32k tokens and GPT-5 scores 0.1% F1; RLM(depth=1) reaches 58.0% (arXiv:2512.24601, Table 1). 30k and 50k below are harness caps.

| Situation | Approach |
|-----------|----------|
| One file, one function, or a single needle | Read directly |
| Linear aggregate or list-everything, and completeness matters | Decompose (this skill) |
| Pairwise, quadratic, or multi-hop across scattered sources | Decompose, even under 30k tokens |
| 10+ files or 50k+ tokens | Decompose |
| Under 30k tokens and a localised answer | Read directly |

## Protocol

1. **Size the input** before reading anything: count files (glob, `find`), lines (`wc -l`), bytes (`ls -lh`), pages for PDFs.
2. **Filter** the search space with searches (content search, file patterns, keywords, file types) before opening any file. Chain filters: file type, then keyword, then meaning.
3. **Chunk** what remains: natural units (functions, classes, sections), line ranges, or keyword partitions. Batches of 5 to 10 files.
4. **Recurse** at depth 1: one sub-agent per independent batch, self-contained brief (files, question, output schema); run one parallel wave. Write the batch count first. If more batches remain, run the next wave after the merge. Sub-agents answer; they do not spawn sub-agents.
5. **Verify** the synthesised answer on a smaller window: extract the minimal evidence and re-check it; settle disagreements with a targeted re-read.
6. **Synthesise programmatically**: aggregate the structured results, deduplicate, categorise, then write the answer with file and line references.

## Rules

- MUST size the input before reading it
- MUST search before reading a directory; NEVER list a tree recursively as a substitute for search
- MUST read large files by line range: over 2,000 lines or 50 KB never in one read; PDFs over 100 pages or 30 MB by metadata or split
- MUST convert PDFs and Office files to markdown before reasoning. Prefer local anydoc (`npx -y @firecrawl/anydoc FILE -o .firecrawl/FILE.md`) from the Firecrawl convert-documents-to-markdown skill. Use cloud `firecrawl parse` for OCR (`--ocr hosted` / `firecrawl parse`), `-Q`, or `-S`. Never dump the binary into context. Cloud parse caps at 50 MB and about 1 credit per PDF page; split first if larger.
- NEVER load more than 5 files into the main context without a written batch plan
- MUST give every sub-agent its own context: the files, the question, the output schema
- MUST keep recursion depth at 1: sub-agents answer; they MUST NOT launch sub-agents
- MUST write the batch count before launch; one parallel wave, then merge; further waves only after that merge
- MUST spot-check the synthesised result against the sources before answering
- SHOULD read definitions first (`grep -n "function"`) and bodies later; tables of contents and abstracts before full text
- NEVER run the same query over the same content in several sub-agents; partition once into disjoint batches
- NEVER raise depth because the merge looks thin; re-read the disagreement instead

## Tools, agent-agnostic

| Need | Use |
|------|-----|
| Find files | the file search or glob tool, or `find` |
| Find content | the content search or grep tool, never a full read |
| Size | `wc -l`, `ls -lh`, page count (`pdfinfo` if present) |
| Read | the file reader with an offset and a limit, or `sed -n 'START,ENDp'` |
| PDF or Office file | anydoc first (`npx -y @firecrawl/anydoc FILE -o .firecrawl/out.md`), then grep the markdown. Cloud `firecrawl parse` for OCR, `-Q`, or `-S`. Else `pdfinfo` and a split. Never a full binary read |
| Delegate | the sub-agent or task tool, one brief per batch |
| Aggregate | a scratch file or structured notes, then one final pass |

Tool names differ between agents (Claude Code, Codex, Cursor, Gemini CLI); map the row to your agent's equivalent.

## Patterns

### Codebase analysis

"Find all error handling patterns." Glob the source files, grep `catch|throw|Error|except`, batch the matches by module (5 to 10 files), one sub-agent per batch with a fixed report schema, merge into a categorised summary with file references. Worked example: [references/codebase-analysis.md](references/codebase-analysis.md).

### Multi-document question answering

"What features are planned across all PRDs?" Glob the documents, size them, define an extraction schema (name, priority, status, quarter), one sub-agent per document group, deduplicate and categorise, spot-check three entries against the sources. Worked example: [references/document-aggregation.md](references/document-aggregation.md).

### Aggregation

"Summarise all TODO comments." Grep `TODO|FIXME|HACK`, group by module, extract context and priority per group, produce a prioritised list.

### Large PDFs

Glob `*.pdf`, size bytes and page count (`pdfinfo`). Over 100 pages or 30 MB: do not ingest. Convert with anydoc to `.firecrawl/` (Firecrawl skill `convert-documents-to-markdown`), then grep and chunk the markdown. If anydoc exits 3 (scanned pages), rerun with `--ocr hosted` or `firecrawl parse` (Firecrawl skill `firecrawl-parse`, 50 MB / ~1 credit per page). Batch by document, depth 1, spot-check titles against PDF metadata.

A one-page convert-to-markdown job is not this skill. Hand it to anydoc or `firecrawl parse` alone.

### Long output

Split the output into sections, generate each independently, store intermediate results in a file, stitch them with a coherence pass.

## Cost and quality

Decomposition spends coordination tokens. In the paper (arXiv:2512.24601, Table 1 and Figure 1), RLM(GPT-5, depth=1) scored 91.3% on BrowseComp-Plus (6 to 11M tokens) at $0.99 average, versus 70.5% for compaction. On OOLONG it beat GPT-5 by 28.4%. On OOLONG-Pairs it reached 58.0% F1 against a 0.1% base. Figure 1 scales inputs from 2^13 to 2^20 tokens. Median cost stays comparable to the base model; averages can rise on outlier trajectories. Thresholds: [references/cost-analysis.md](references/cost-analysis.md).

## Anti-patterns

| Anti-pattern | Fix |
|--------------|-----|
| Reading everything first "to get context" | Size, filter, then read by range |
| Dumping a PDF binary into the window | Parse to markdown, then grep and chunk |
| Decomposing a single needle or one-file lookup | Read directly |
| Sub-agents without the question or the schema | Self-contained briefs |
| Sub-agents that spawn sub-agents | Depth 1: they answer, they do not delegate |
| Trusting the merged answer | Spot-check on a
```

## File: README.md
upstream: https://github.com/massimodeluisa/recursive-decomposition-skill/blob/main/README.md

````text
<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="assets/logo-dark.png">
    <img src="assets/logo-light.png" alt="Recursive Decomposition Skill" width="160">
  </picture>
</p>

<h1 align="center">Recursive Decomposition Skill</h1>

<p align="center">
  <strong>Decompose long-context tasks while keeping the context small.</strong><br />
  An <a href="https://agentskills.io">Agent Skill</a> for Claude Code, Codex, Cursor and compatible agents, based on the Recursive Language Models research
</p>

<p align="center">
  <a href="https://skills.sh/massimodeluisa/recursive-decomposition-skill"><img src="https://skills.sh/b/massimodeluisa/recursive-decomposition-skill" alt="skills.sh" /></a>
  <a href="https://arxiv.org/abs/2512.24601"><img src="https://img.shields.io/badge/arXiv-2512.24601-b31b1b?style=flat-square" alt="arXiv paper" /></a>
  <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-green?style=flat-square" alt="MIT License" /></a>
  <a href="https://agentskills.io"><img src="https://img.shields.io/badge/Format-Agent_Skills-orange?style=flat-square" alt="Agent Skills format" /></a>
</p>

<p align="center">
  <a href="#install">Install</a> ·
  <a href="#usage">Usage</a> ·
  <a href="#how-it-works">How it works</a> ·
  <a href="#eval">Eval</a> ·
  <a href="#repository-structure">Structure</a> ·
  <a href="#acknowledgments">Acknowledgments</a>
</p>

<p align="center">
  <img src="assets/og.png" alt="Recursive Decomposition Skill: decompose long-context tasks, keep the context small" width="800" />
</p>

---

## The problem

Large codebases, dozens of documents, long reports: as the context grows, models miss details, link distant parts by guesswork and lose accuracy. The Recursive Language Models paper calls it context rot.

## What it does

When a task is codebase-wide, multi-document, a pile of PDFs, or a dense aggregate, even if it fits the window, the skill makes the agent treat the input as an environment to query instead of text to swallow:

1. **Size** the input before reading anything.
2. **Filter** the search space with searches, not reads.
3. **Chunk** what remains into batches of 5 to 10 files or natural units.
4. **Recurse** at depth 1 with one sub-agent per batch, each with a self-contained brief. Sub-agents do not spawn sub-agents.
5. **Verify** the merged answer on a small window against the sources.
6. **Synthesise** programmatically, with file and line references.

PDFs go through [anydoc](https://skills.sh/firecrawl/anydoc/convert-documents-to-markdown) first (`npx -y @firecrawl/anydoc FILE -o .firecrawl/out.md`), then grep the markdown. Cloud [`firecrawl parse`](https://skills.sh/firecrawl/cli/firecrawl-parse) is for OCR, `-Q`, or `-S`. A one-page convert job is not this skill.

## Install

With the [skills CLI](https://github.com/vercel-labs/skills):

```bash
npx skills add massimodeluisa/recursive-decomposition-skill
```

Add `-g` for a user-level install, `-a claude-code` (or another agent) to target one agent.

As a Claude Code plugin:

```bash
claude plugin marketplace add massimodeluisa/recursive-decomposition-skill
claude plugin install recursive-decomposition@recursive-decomposition-skill
```

Manual: copy `skills/recursive-decomposition` into `~/.claude/skills/` (or your agent's skills directory) and restart the agent.

## Usage

- `/recursive-decomposition` applies the protocol to the current task.
- `/recursive-decomposition src/` sizes that input first, then runs the protocol.

The skill also activates on its own for prompts like:

```text
Analyze error handling patterns across this entire codebase
Find all TODO comments in the project and categorize by priority
What API endpoints are defined across all route files?
Summarize the key decisions from all meeting notes in docs/
Find security issues across all Python files
```

## How it works

| Situation | Approach |
|-----------|----------|
| One file, one function, or a single needle | Read directly |
| Linear aggregate or list-everything, and completeness matters | Decompose |
| Pairwise, quadratic, or multi-hop across scattered sources | Decompose, even under 30k tokens |
| 10+ files or 50k+ tokens | Decompose |
| Under 30k tokens and a localised answer | Read directly |

Results reported in the [paper](https://arxiv.org/abs/2512.24601):

| Task | Direct model | With RLM |
|------|--------------|----------|
| Multi-hop QA (6 to 11M tokens) | 70% | 91% |
| Linear aggregation | baseline | +28 to 33% |
| Quadratic reasoning | under 0.1% | 58% |
| Context scaling | 2^14 tokens | 2^18 tokens |

RLM runs were about 3x cheaper than summarisation baselines.

## Eval

Fixture is a git submodule, not files copied into this repo: [tccao/mortgage-doc-rag](https://github.com/tccao/mortgage-doc-rag) (MIT), 131 public-domain mortgage PDFs, about 63 MB. Thanks to that project for the corpus.

```bash
git submodule update --init --depth 1 skills/recursive-decomposition/evals/files/mortgage-doc-rag
bash .github/scripts/eval-skill.sh check
```

Pre/post on that tree, same prompt (count, 10 largest, titles from at most three files):

| Path | What ran | Time | Titles from the 3 largest |
|------|----------|------|---------------------------|
| With skill | size first, anydoc on 2 digital PDFs, `firecrawl parse` on 1 scan (anydoc exit 3) | size 0.02 s; anydoc 1.64 s; OCR parse 15.45 s | APPRAISAL OF REAL PROPERTY; TILA RESPA Integrated Disclosure; Uniform Residential Appraisal Report |
| Without skill | `pdftotext` on all 131 PDFs | 1.65 s | two titles from digital PDFs; **empty** on the largest file (scan, 4 bytes) |

The naive path is faster and blind on scans. The skill is slower because it OCRs one file, and that is the file `pdftotext` cannot read. Details: [evals/README.md](skills/recursive-decomposition/evals/README.md).

## Repository structure

```text
recursive-decomposition-skill/
├── .claude-plugin/          plugin.json, marketplace.json (the repo is the plugin)
├── .github/                 bash validator and CI workflow
├── skills/recursive-decomposition/
│   ├── SKILL.md             protocol, rules, patterns
│   ├── references/          rlm-strategies, cost-analysis, codebase-analysis, document-aggregation
│   └── evals/               trigger queries, mortgage PDF submodule, bash scorer
├── assets/                  social preview, logo (light and dark)
├── AGENTS.md · CONVENTIONS.md · CONTRIBUTING.md · CHANGELOG.md
└── LICENSE
```

## Acknowledgments

This skill is based on the **Recursive Language Models** paper. Thanks to the authors:

<table>
  <tr>
    <td align="center"><a href="https://x.com/a1zhang"><b>Alex L. Zhang</b></a><br><sub>MIT CSAIL</sub></td>
    <td align="center"><a href="https://x.com/tim_kraska"><b>Tim Kraska</b></a><br><sub>MIT</sub></td>
    <td align="center"><a href="https://x.com/lateinteraction"><b>Omar Khattab</b></a><br><sub>MIT CSAIL, creator of DSPy</sub></td>
  </tr>
</table>

> **Recursive Language Models**, Alex L. Zhang, Tim Kraska, Omar Khattab, arXiv:2512.24601, December 2025. [Abstract](https://arxiv.org/abs/2512.24601) · [PDF](https://arxiv.org/pdf/2512.24601)

Eval corpus: [tccao/mortgage-doc-rag](https://github.com/tccao/mortgage-doc-rag) (MIT), public-domain mortgage PDFs. Thanks to that project.

PDF conversion: Firecrawl [anydoc](https://skills.sh/firecrawl/anydoc/convert-documents-to-markdown) and [firecrawl-parse](https://skills.sh/firecrawl/cli/firecrawl-parse). Thanks to Firecrawl.

This skill is an independent project and is not affiliated with the paper authors, MIT, tccao, or Firecrawl.

## Author

<p>
  <a href="https://x.com/massimodeluisa"><img src="https://img.shields.io/badge/@massimodeluisa-000000?style=flat-square&logo=x" alt="X" /></a>
  <a href="https://github.com/massimodeluisa"><img src="https://img.shields.io/badge/GitHub-massimodeluisa-181717?style=flat-square&logo=github" alt="GitHub" /></a>
</p>

**Massimo De Luisa**: [massimo.deluisa.bio](https://massi
````
