$npx -y skills add massimodeluisa/recursive-decomposition-skill --skill recursive-decompositionBased on the Recursive Language Models (RLM) research by Zhang, Kraska, and Khattab (2025), this skill provides strategies for handling tasks that exceed comfortable context limits through programmatic decomposition and recursive self-invocation. Triggers on phrases like "analyze
| 1 | # Recursive Decomposition Guidelines |
| 2 | |
| 3 | ## References |
| 4 | |
| 5 | Consult these resources as needed: |
| 6 | |
| 7 | - ./references/rlm-strategies.md -- Detailed decomposition patterns from the RLM paper |
| 8 | - ./references/cost-analysis.md -- When to apply recursive vs. direct approaches |
| 9 | - ./references/codebase-analysis.md -- Full walkthrough of codebase-wide analysis |
| 10 | - ./references/document-aggregation.md -- Multi-document information extraction |
| 11 | |
| 12 | ## Core Principles |
| 13 | |
| 14 | **CRITICAL: Treat inputs as environmental variables, not immediate context.** |
| 15 | |
| 16 | Most tasks fail when context is overloaded. Instead of loading entire contexts into the processing window, treat inputs as **environmental variables** accessible through code execution. Decompose problems recursively, process segments independently, and aggregate results programmatically. |
| 17 | |
| 18 | **Progressive Disclosure**: Load information only when necessary. Start high-level to map the territory, then dive deep into specific areas. |
| 19 | |
| 20 | ### When Recursive Decomposition is Required |
| 21 | |
| 22 | - Tasks involving 10+ files |
| 23 | - Input exceeding ~50k tokens where single-prompt context is insufficient |
| 24 | - Multi-hop questions requiring evidence from multiple scattered sources |
| 25 | - Codebase-wide pattern analysis or migration planning |
| 26 | |
| 27 | ### When Direct Processing Works |
| 28 | |
| 29 | - Small contexts (<30k tokens) |
| 30 | - Single file analysis |
| 31 | - Linear complexity tasks with manageable inputs |
| 32 | |
| 33 | ## Operational Rules |
| 34 | |
| 35 | - Always identify the search space size first. |
| 36 | - Always use `grep` or `glob` before `view_file` on directories. |
| 37 | - Always partition lists > 10 items into batches. |
| 38 | - Never read more than 5 files into context without a specific plan. |
| 39 | - Verify synthesized answers by spot-checking source material. |
| 40 | - Mitigate "context rot" by verifying answers on smaller windows. |
| 41 | - **Treat yourself as an autonomous agent, not just a passive responder.** |
| 42 | |
| 43 | ## Large File Handling Protocols |
| 44 | |
| 45 | **CRITICAL**: Do NOT read large files directly into context. |
| 46 | |
| 47 | 1. **Check Size First**: Always run `wc -l` (lines) or `ls -lh` (size) before `view_file`. |
| 48 | 2. **Hard Limits**: |
| 49 | * **Text/Code**: > 2,000 lines or > 50KB -> **MUST** use `view_file` with `start_line`/`end_line` or `head`/`tail`. |
| 50 | * **PDFs**: > 30MB or > 100 pages -> **MUST** be split or processed by metadata only. |
| 51 | 3. **Strategy**: |
| 52 | * For code: Read definitions first (`grep -n "function" ...`) then read specific bodies. |
| 53 | * For text: Read Table of Contents or Abstract first. |
| 54 | |
| 55 | ## Tool Preferences |
| 56 | |
| 57 | - `grep` / `glob` not `ls -R` (unless mapping structure). |
| 58 | - `view_file` with line ranges (offset/limit) not full file reads for huge files. |
| 59 | - `wc -l` / `ls -lh` before reading unknown files. |
| 60 | - `run_command` (grep) not `read_file` for searching. |
| 61 | - `task` tool for sub-agents (recurse). |
| 62 | |
| 63 | ## Empowering Agentic Behavior |
| 64 | |
| 65 | To maximize effectiveness: |
| 66 | |
| 67 | - **Self-Correction**: Always verify your own work. If a result seems empty or wrong, debug the approach (e.g., check grep arguments) before giving up. |
| 68 | - **Aggressive Context Management**: Regularly clear irrelevant history. Don't let the context window rot with dead ends. |
| 69 | - **Plan First**: For any task > 3 steps, write a mini-plan. |
| 70 | - **Safe YOLO Mode**: When appropriate (e.g., read-only searches), proceed with confidence without asking for permission on every single step, but stop for critical actions. |
| 71 | |
| 72 | ## Cost-Performance Tradeoffs |
| 73 | |
| 74 | - **Smaller contexts**: Direct processing may be more efficient. |
| 75 | - **Larger contexts**: Recursive decomposition becomes necessary. |
| 76 | - **Threshold**: Consider decomposition when inputs exceed ~30k tokens or span 10+ files. |
| 77 | |
| 78 | Balance thoroughness against computational cost. For time-sensitive tasks, apply aggressive filtering. For comprehensive analysis, prefer exhaustive decomposition. |
| 79 | |
| 80 | ## Anti-Patterns to Avoid |
| 81 | |
| 82 | - **Excessive sub-calling**: Avoid redundant queries over the same content. |
| 83 | - **Premature decomposition**: Simple tasks don't need recursive strategies. |
| 84 | - **Lost context**: Ensure sub-agents have sufficient context for their sub-tasks. |
| 85 | - **Unverified synthesis**: Always spot-check aggregated results. |
| 86 | |
| 87 | ## Scalability (Chunking & filtering) |
| 88 | |
| 89 | ### 1. Filter Before Deep Analysis |
| 90 | |
| 91 | Narrow the search space before detailed processing: |
| 92 | |
| 93 | ``` |
| 94 | # Instead of reading all files into context: |
| 95 | 1. Use Grep/Glob to identify candidate files by pattern |
| 96 | 2. Filter candidates using domain-specific keywords |
| 97 | 3. Only deeply analyze the filtered subset |
| 98 | ``` |
| 99 | |
| 100 | Apply model priors about domain terminology to construct effective filters. For |