#!/usr/bin/env python3 """ rlm-ask: answer a question about a file too large for a model's context window, using a small local model recursively. Designed to be called by Claude Code (or any agent) as a plain shell tool, rather than to sit behind Claude Code's model endpoint. The agent keeps its own loop and tool calling; this is a subroutine it hands bulk reading to. python rlm_ask.py --file huge.log --query "which error appears most often?" cat huge.log | python rlm_ask.py --query "..." --model gemma4:e4b When to use it, from measurements in this repo: - Context EXCEEDS the model's window -> use this. A direct call silently truncates and answers from the fragment it saw. - Context FITS the window -> do NOT use this. Recursion adds error; a plain call scored better (OOLONG-131k: 0.428 direct vs 0.269 recursive). """ from __future__ import annotations import argparse import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from rlm_haiku.rlm_repl import RLM_REPL # noqa: E402 from rlm_haiku.utils.llm import Usage # noqa: E402 def main() -> int: p = argparse.ArgumentParser(description="Ask a question about an oversized file.") p.add_argument("--query", "-q", required=True) p.add_argument("--file", "-f", help="input file; omit to read stdin") p.add_argument("--model", "-m", default="gemma4:e4b", help="ollama model tag") p.add_argument("--sub-model", default=None, help="model for recursive calls") p.add_argument("--backend", default="ollama", choices=("ollama", "claude")) p.add_argument("--max-iters", type=int, default=10) p.add_argument("--workers", type=int, default=4) p.add_argument("--base-url", default="http://127.0.0.1:11434") p.add_argument("--quiet", action="store_true", help="print only the answer") a = p.parse_args() if a.file: context = Path(a.file).read_text(errors="replace") elif not sys.stdin.isatty(): context = sys.stdin.read() else: p.error("provide --file or pipe input on stdin") if not context.strip(): print("error: empty context", file=sys.stderr) return 2 started = time.time() usage = Usage() rlm = RLM_REPL( model=a.model, recursive_model=a.sub_model or a.model, backend=a.backend, max_iterations=a.max_iters, max_workers=a.workers, usage=usage, # A small root model will otherwise answer from an empty REPL on some # inputs -- one call, never having looked at the context. require_repl=True, ) try: answer = rlm.completion(context=context, query=a.query) except Exception as e: # noqa: BLE001 print(f"error: {type(e).__name__}: {e}", file=sys.stderr) return 1 print(answer.strip()) if not a.quiet: u = usage.as_dict() print( f"\n[rlm-ask] {len(context):,} chars | {u['calls']} calls | " f"{u['errors']} errors | {time.time() - started:.0f}s | model={a.model}", file=sys.stderr, ) return 0 if __name__ == "__main__": raise SystemExit(main())