rlm-context-manager

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

RLM Context Manager

RLM 上下文管理器

Purpose: Enable Claude to process documents and contexts that exceed typical context window limits, matching Gemini's large context capabilities through intelligent chunking and recursive processing.
用途:让Claude能够处理超出常规上下文窗口限制的文档和上下文,通过智能分块和递归处理,达到与Gemini相当的大上下文处理能力。

Architecture (RLM Paper Implementation)

架构(RLM论文实现)

Based on arXiv:2512.24601 - Recursive Language Models by Zhang, Kraska, Khattab (MIT CSAIL).
ComponentIRP ImplementationModel
Root LLMMain Claude Code conversationClaude Opus 4.5
Sub-LLM (
llm_query
)
rlm-subcall
Task agent
Claude Haiku
External EnvironmentPersistent Python REPLPython 3
State Persistence
${SKILLS_ROOT}/rlm-context-manager/state/
Pickle
基于论文arXiv:2512.24601 - 由MIT CSAIL的Zhang、Kraska、Khattab提出的递归语言模型。
组件IRP实现方式模型
根LLM主Claude对话Claude Opus 4.5
子LLM(
llm_query
rlm-subcall
任务Agent
Claude Haiku
外部环境持久化Python REPLPython 3
状态持久化
${SKILLS_ROOT}/rlm-context-manager/state/
Pickle

Use Cases

适用场景

  • Processing large codebases for analysis
  • Analyzing lengthy documents (research papers, legal docs, logs)
  • Working with Mnemosyne ledger archives
  • Cross-session context restoration
  • Bridging context between Claude and Gemini
  • 处理大型代码库以进行分析
  • 分析长文档(研究论文、法律文档、日志)
  • 处理Mnemosyne账本归档文件
  • 跨会话上下文恢复
  • 实现Claude与Gemini之间的上下文互通

Commands

命令

/rlm init <context_path>     - Initialize REPL with large context file
/rlm status                  - Show current RLM state (chars loaded, chunks, buffers)
/rlm query <question>        - Query the loaded context
/rlm chunk                   - Materialize context into chunk files
/rlm synthesize              - Merge collected evidence into final answer
/rlm reset                   - Clear RLM state
/rlm export                  - Export buffers to file
/rlm init <context_path>     - 初始化REPL并加载大上下文文件
/rlm status                  - 显示当前RLM状态(已加载字符数、分块数、缓冲区)
/rlm query <question>        - 对已加载的上下文进行查询
/rlm chunk                   - 将上下文生成分块文件
/rlm synthesize              - 将收集到的信息合并为最终答案
/rlm reset                   - 清除RLM状态
/rlm export                  - 将缓冲区内容导出到文件

Quick Start

快速开始

bash
undefined
bash
undefined

1. Initialize with a large context file

1. 使用大上下文文件初始化

python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py init /path/to/large_document.txt
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py init /path/to/large_document.txt

2. Check status

2. 查看状态

python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py status
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py status

3. Scout the context (peek at beginning and end)

3. 预览上下文(查看开头和结尾部分)

python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec -c "print(peek(0, 3000))"
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec -c "print(peek(0, 3000))"

4. Create chunks for sub-LLM processing

4. 生成分块以供子LLM处理

python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec <<'PY' paths = write_chunks('${SKILLS_ROOT}/rlm-context-manager/state/chunks', size=200000, overlap=0) print(f"Created {len(paths)} chunks") PY
undefined
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec <<'PY' paths = write_chunks('${SKILLS_ROOT}/rlm-context-manager/state/chunks', size=200000, overlap=0) print(f"Created {len(paths)} chunks") PY
undefined

Step-by-Step Procedure

分步操作流程

1. Initialize the REPL State

1. 初始化REPL状态

bash
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py init <context_path>
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py status
bash
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py init <context_path>
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py status

2. Scout the Context

2. 预览上下文

bash
undefined
bash
undefined

Peek at beginning

查看开头部分

python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec -c "print(peek(0, 3000))"
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec -c "print(peek(0, 3000))"

Peek at end

查看结尾部分

python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec -c "print(peek(len(content)-3000, len(content)))"
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec -c "print(peek(len(content)-3000, len(content)))"

Search for patterns

搜索指定模式

python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec -c "print(grep('pattern', max_matches=10))"
undefined
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec -c "print(grep('pattern', max_matches=10))"
undefined

3. Choose Chunking Strategy

3. 选择分块策略

  • Semantic chunking: For structured formats (markdown headings, JSON, log timestamps)
  • Character chunking: Default ~200k chars with optional overlap
  • 语义分块:适用于结构化格式(Markdown标题、JSON、日志时间戳)
  • 字符分块:默认约20万字符,可设置重叠部分

4. Materialize Chunks

4. 生成分块文件

bash
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec <<'PY'
paths = write_chunks('${SKILLS_ROOT}/rlm-context-manager/state/chunks', size=200000, overlap=0)
print(len(paths))
print(paths[:5])
PY
bash
python3 ${SKILLS_ROOT}/rlm-context-manager/scripts/rlm_repl.py exec <<'PY'
paths = write_chunks('${SKILLS_ROOT}/rlm-context-manager/state/chunks', size=200000, overlap=0)
print(len(paths))
print(paths[:5])
PY

5. Sub-LLM Processing Loop

5. 子LLM处理循环

For each chunk, invoke the rlm-subcall subagent:
Task: rlm-subcall
Prompt: "Query: <user_query>. Chunk file: <chunk_path>. Extract relevant information."
Model: haiku
The sub-LLM returns structured JSON:
json
{
  "chunk_id": "...",
  "relevant": [{"point": "...", "evidence": "...", "confidence": "high|medium|low"}],
  "missing": ["..."],
  "suggested_next_queries": ["..."],
  "answer_if_complete": "..."
}
对每个分块,调用rlm-subcall子Agent:
Task: rlm-subcall
Prompt: "Query: <user_query>. Chunk file: <chunk_path>. Extract relevant information."
Model: haiku
子LLM会返回结构化JSON:
json
{
  "chunk_id": "...",
  "relevant": [{"point": "...", "evidence": "...", "confidence": "high|medium|low"}],
  "missing": ["..."],
  "suggested_next_queries": ["..."],
  "answer_if_complete": "..."
}

6. Synthesis

6. 结果合成

Collect sub-LLM outputs and synthesize final answer in main conversation.
收集子LLM的输出结果,在主对话中合成最终答案。

REPL Helper Functions

REPL辅助函数

FunctionDescription
peek(start, end)
Return substring of context
grep(pattern, max_matches, window)
Regex search with context window
chunk_indices(size, overlap)
Calculate chunk boundaries
write_chunks(out_dir, size, overlap)
Materialize chunks to files
add_buffer(text)
Store intermediate results
函数描述
peek(start, end)
返回上下文的子字符串
grep(pattern, max_matches, window)
带上下文窗口的正则搜索
chunk_indices(size, overlap)
计算分块边界
write_chunks(out_dir, size, overlap)
将分块生成为文件
add_buffer(text)
存储中间结果

Guardrails

防护规则

  • Do NOT paste large raw chunks into main chat context
  • Use REPL to locate exact excerpts; quote only what you need
  • Subagents cannot spawn other subagents (orchestration stays in main conversation)
  • Keep scratch/state files under
    ${SKILLS_ROOT}/rlm-context-manager/state/
  • Prefer chunk sizes ~100k-300k chars per subagent call
  • 请勿将大型原始分块粘贴到主聊天上下文中
  • 使用REPL定位精确片段,仅引用所需内容
  • 子Agent无法生成其他子Agent(编排逻辑保留在主对话中)
  • 临时文件/状态文件请存放在
    ${SKILLS_ROOT}/rlm-context-manager/state/
    目录下
  • 每次调用子Agent时,分块大小建议为10万-30万字符

Integration with IRP Protocols

与IRP协议的集成

Mnemosyne Ledger

Mnemosyne账本

  • Use RLM to process large Mnemosyne archives
  • Export buffers as Mnemosyne packets for cross-session persistence
  • 使用RLM处理大型Mnemosyne归档文件
  • 将缓冲区内容导出为Mnemosyne数据包,实现跨会话持久化

Cross-Model (Gemini Bridging)

跨模型(Gemini桥接)

  • Load Gemini's large context exports
  • Chunk and analyze for Claude consumption
  • Synthesize into Mnemosyne-compatible format
  • 加载Gemini的大上下文导出文件
  • 分块后供Claude分析处理
  • 合成为Mnemosyne兼容格式

CRTP Packets

CRTP数据包

  • RLM outputs can be formatted as CRTP packets for multi-model relay
  • RLM输出可格式化为CRTP数据包,用于多模型中继

File Structure

文件结构

rlm-context-manager/
├── SKILL.md              # This file
├── scripts/
│   └── rlm_repl.py       # Persistent Python REPL
├── agents/
│   └── rlm-subcall.md    # Sub-LLM agent definition
└── state/                # Runtime state (gitignored)
    ├── state.pkl         # Persisted REPL state
    └── chunks/           # Materialized chunk files
rlm-context-manager/
├── SKILL.md              # 本文档
├── scripts/
│   └── rlm_repl.py       # 持久化Python REPL
├── agents/
│   └── rlm-subcall.md    # 子LLM Agent定义
└── state/                # 运行时状态(已加入git忽略)
    ├── state.pkl         # 持久化REPL状态
    └── chunks/           # 生成分块文件

Credits

致谢

Based on the RLM paper by Zhang, Kraska, Khattab (MIT CSAIL) and the Claude Code RLM implementation by Brainqub3.
基于MIT CSAIL的Zhang、Kraska、Khattab所著的RLM论文,以及Brainqub3实现的Claude Code RLM版本。