Query-Aware Code Compression for LLMs: 1.3M LOC to 1,500 Lines in 2.8s A developer introduced CodeShrink, a query-aware code compression tool that uses Tree-sitter ASTs to reduce a 1.29M LOC codebase to about 1,500 lines in 2.8 seconds for LLM queries. The tool extracts relevant symbols with context, outperforming full dumps, truncation, and RAG approaches, and is available as a Rust CLI or npm package. You want to ask an LLM a question about your codebase. You have three options: Paste everything — Repomix dumps your entire repo. For Next.js 1.29M LOC , that's 80k+ tokens. Expensive, slow, and the model loses the answer somewhere in the middle. Truncate — Cut at 20k tokens. Hope the relevant code is in the first 400 files alphabetically. It usually isn't. RAG — Embed your code, retrieve chunks by similarity. You get 8k tokens of tangentially related fragments, split mid-function, missing the imports and types you need to understand them. None of these are query-aware at the AST level. If you ask "where is authentication?", you want the authenticate function, its callers, its types — not a random embedding-similar chunk that mentions "auth" in a comment. codeshrink "where is authentication?" --path ./next.js Output: 1,509 lines from 1.29M LOC. 99.9% compression. 2.8 seconds. Only auth-related functions with their signatures, imports, and 5 lines of context. The key idea: use Tree-sitter ASTs, not embeddings . Parse the code into symbols functions, classes, types , match them against the query, rank them, and extract the top-K with context. Tree-sitter parses every file into an AST in parallel via rayon . We support 7 languages: TypeScript, TSX, JavaScript, Python, Rust, Go, Java. From each AST, we extract symbol definitions: function name, start/end line, signature parameter types, return type , and the file path. For a 78k LOC repo Fastify , this takes ~40ms. Your query is split into terms. Each term gets expanded with a semantic group: This is a static mapping, not an embedding model. It covers the common synonyms that matter in code. Each symbol gets a score from multiple signals: src/auth/ for "auth" Scores are combined with configurable weights. The default ranking produces good results without tuning. Top-K symbols default 50 are extracted with N lines of context above and below default 5 . When two symbols overlap or are adjacent, their ranges merge into one block. Formatted as Markdown default , XML for LLM system prompts , or plain text. Each block includes the file path, line numbers, and the symbol's score. All measured on Apple Silicon, release build, single run no warm-up caching : | Repo | Query | Input LOC | Output LOC | Compression | Latency | |---|---|---|---|---|---| | Express 21k LOC | "middleware" | 21,475 | 628 | 97.1% | 28ms | | Express 21k LOC | "routing" | 21,475 | 655 | 96.9% | 25ms | | Fastify 78k LOC | "route handler" | 77,959 | 1,454 | 98.1% | 93ms | | Fastify 78k LOC | "plugin" | 77,959 | 721 | 99.1% | 68ms | | Next.js 1.29M LOC | "server action" | 1,294,421 | 1,509 | 99.9% | 2,823ms | | Next.js 1.29M LOC | "middleware" | 1,294,421 | 1,709 | 99.9% | 2,253ms | The latency is dominated by Tree-sitter parsing on large repos. For typical project sizes 10-100k LOC , it's under 100ms. | | CodeShrink | Repomix | CodeGraph | Truncation | RAG | |---|---|---|---|---|---| | Query-aware | Yes | No full dump | Yes MCP | No | Partial | | Standalone CLI | Yes | Yes | No MCP server | N/A | No | | Latency 78k LOC | 93ms | ~500ms | ~2s | 0ms | ~200ms | | Output 78k LOC | 1.4k lines | 78k lines | ~2k lines | 20k tokens | ~8k tokens | | Dependencies | 0 single binary | Node.js | Python + SQLite | N/A | Embeddings model | | npm package | Yes | Yes | No | N/A | Varies | Install: Rust CLI cargo install codeshrink or npm npm install codeshrink CLI: Basic query codeshrink "where is the database connection?" --path ./my-project Narrow context codeshrink "error handling" -c 2 XML output for LLM system prompts codeshrink "routing" --format xml Pipe into clipboard codeshrink "auth" --path ./app | pbcopy As a Node.js library napi-rs bindings : js const { shrink } = require 'codeshrink' ; const result = shrink 'authentication', '/path/to/repo', { contextLines: 5, maxSymbols: 50, format: 'markdown', } ; console.log result.stats ; // { filesScanned: 141, symbolsReturned: 50, // inputLines: 21475, outputLines: 628, // compressionRatio: 0.971 } As a Rust library: js use codeshrink core::{shrink, ShrinkOptions}; use std::path::Path; let result = shrink "where is authentication?", Path::new "./my-project" , &ShrinkOptions::default , ?; println "{}", result.compressed ; MIT/Apache-2.0: github.com/TimurRakhmatullin86/codeshrink https://github.com/TimurRakhmatullin86/codeshrink What queries would you run on your codebase? What output format works best for your LLM workflow?