{"slug": "query-aware-code-compression-for-llms-1-3m-loc-to-1500-lines-in-2-8s", "title": "Query-Aware Code Compression for LLMs: 1.3M LOC to 1,500 Lines in 2.8s", "summary": "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.", "body_md": "You want to ask an LLM a question about your codebase. You have three options:\n\n**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.\n\n**Truncate** — Cut at 20k tokens. Hope the relevant code is in the first 400 files alphabetically. It usually isn't.\n\n**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.\n\nNone 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.\n\n```\ncodeshrink \"where is authentication?\" --path ./next.js\n```\n\nOutput: 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.\n\nThe 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.\n\nTree-sitter parses every file into an AST in parallel (via rayon). We support 7 languages: TypeScript, TSX, JavaScript, Python, Rust, Go, Java.\n\nFrom each AST, we extract symbol definitions: function name, start/end line, signature (parameter types, return type), and the file path.\n\nFor a 78k LOC repo (Fastify), this takes ~40ms.\n\nYour query is split into terms. Each term gets expanded with a semantic group:\n\nThis is a static mapping, not an embedding model. It covers the common synonyms that matter in code.\n\nEach symbol gets a score from multiple signals:\n\n`src/auth/` for \"auth\")\nScores are combined with configurable weights. The default ranking produces good results without tuning.\n\nTop-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.\n\nFormatted as Markdown (default), XML (for LLM system prompts), or plain text. Each block includes the file path, line numbers, and the symbol's score.\n\nAll measured on Apple Silicon, release build, single run (no warm-up caching):\n\n| Repo | Query | Input LOC | Output LOC | Compression | Latency | \n|---|---|---|---|---|---|\n| Express (21k LOC) | \"middleware\" | 21,475 | 628 | 97.1% | 28ms | \n| Express (21k LOC) | \"routing\" | 21,475 | 655 | 96.9% | 25ms | \n| Fastify (78k LOC) | \"route handler\" | 77,959 | 1,454 | 98.1% | 93ms | \n| Fastify (78k LOC) | \"plugin\" | 77,959 | 721 | 99.1% | 68ms | \n| Next.js (1.29M LOC) | \"server action\" | 1,294,421 | 1,509 | 99.9% | 2,823ms | \n| Next.js (1.29M LOC) | \"middleware\" | 1,294,421 | 1,709 | 99.9% | 2,253ms | \n\nThe latency is dominated by Tree-sitter parsing on large repos. For typical project sizes (10-100k LOC), it's under 100ms.\n\n|  | CodeShrink | Repomix | CodeGraph | Truncation | RAG | \n|---|---|---|---|---|---|\n| Query-aware | Yes | No (full dump) | Yes (MCP) | No | Partial | \n| Standalone CLI | Yes | Yes | No (MCP server) | N/A | No | \n| Latency (78k LOC) | 93ms | ~500ms | ~2s | 0ms | ~200ms | \n| Output (78k LOC) | 1.4k lines | 78k lines | ~2k lines | 20k tokens | ~8k tokens | \n| Dependencies | 0 (single binary) | Node.js | Python + SQLite | N/A | Embeddings model | \n| npm package | Yes | Yes | No | N/A | Varies | \n\nInstall:\n\n```\n# Rust CLI\ncargo install codeshrink\n\n# or npm\nnpm install codeshrink\n```\n\nCLI:\n\n```\n# Basic query\ncodeshrink \"where is the database connection?\" --path ./my-project\n\n# Narrow context\ncodeshrink \"error handling\" -c 2\n\n# XML output for LLM system prompts\ncodeshrink \"routing\" --format xml\n\n# Pipe into clipboard\ncodeshrink \"auth\" --path ./app | pbcopy\n```\n\nAs a Node.js library (napi-rs bindings):\n\n``` js\nconst { shrink } = require('codeshrink');\n\nconst result = shrink('authentication', '/path/to/repo', {\n    contextLines: 5,\n    maxSymbols: 50,\n    format: 'markdown',\n});\n\nconsole.log(result.stats);\n// { filesScanned: 141, symbolsReturned: 50,\n//   inputLines: 21475, outputLines: 628,\n//   compressionRatio: 0.971 }\n```\n\nAs a Rust library:\n\n``` js\nuse codeshrink_core::{shrink, ShrinkOptions};\nuse std::path::Path;\n\nlet result = shrink(\n    \"where is authentication?\",\n    Path::new(\"./my-project\"),\n    &ShrinkOptions::default(),\n)?;\n\nprintln!(\"{}\", result.compressed);\n```\n\nMIT/Apache-2.0: [github.com/TimurRakhmatullin86/codeshrink](https://github.com/TimurRakhmatullin86/codeshrink)\n\nWhat queries would you run on your codebase? What output format works best for your LLM workflow?", "url": "https://wpnews.pro/news/query-aware-code-compression-for-llms-1-3m-loc-to-1500-lines-in-2-8s", "canonical_source": "https://dev.to/tim860/query-aware-code-compression-for-llms-13m-loc-to-1500-lines-in-28s-1oad", "published_at": "2026-09-07 22:05:14+00:00", "updated_at": "2026-09-07 22:31:16.476891+00:00", "lang": "en", "topics": ["developer-tools", "large-language-models", "artificial-intelligence"], "entities": ["CodeShrink", "Tree-sitter", "Next.js", "Fastify", "Express", "Repomix", "CodeGraph"], "alternates": {"html": "https://wpnews.pro/news/query-aware-code-compression-for-llms-1-3m-loc-to-1500-lines-in-2-8s", "markdown": "https://wpnews.pro/news/query-aware-code-compression-for-llms-1-3m-loc-to-1500-lines-in-2-8s.md", "text": "https://wpnews.pro/news/query-aware-code-compression-for-llms-1-3m-loc-to-1500-lines-in-2-8s.txt", "jsonld": "https://wpnews.pro/news/query-aware-code-compression-for-llms-1-3m-loc-to-1500-lines-in-2-8s.jsonld"}}