DEV Community

Uwe c
Uwe c

Posted on

How I cut AI context usage by 50x with a Tree-sitter code index

I watched Claude burn through half its context window just to find where a function is defined.

Ask any AI coding assistant "Where is PlayerHealth defined?" and here's what happens:

  1. It runs grep "PlayerHealth" → 200 matches across 40 files
  2. It reads File1.cs, File2.cs, File3.cs...
  3. 2000+ tokens gone, 5+ tool calls — for a simple lookup

Do that 10 times and your context window is toast. Not from coding — from navigation.

grep is the wrong tool for AI

Think about it: grep searches text. Search for log and you'll match catalog, logarithm, blog, every comment mentioning
"log", and every string containing it. The AI has to read through all that noise to find the actual log function.

What if the AI could just query an index instead?

AiDex: one query, exact answer

I built https://github.com/CSCSoftware/AiDex — an MCP server that pre-indexes your codebase using Tree-sitter and
gives AI assistants instant access.

Before (grep):
grep "PlayerHealth" → 200 matches, AI reads 5 files
→ 2000+ tokens, 5 tool calls, 10+ seconds

After (AiDex):
aidex_query({ term: "PlayerHealth" })
→ Engine.cs:45, Player.cs:23, UI.cs:156
→ ~50 tokens, 1 tool call, 3ms

50x less context. One call instead of five.

Tree-sitter parses your code into an AST — it knows what's a function, what's a class, what's a variable. AiDex
indexes only identifiers. So log finds only log, not catalog.

What you get

Instant code search — find any function, class, or variable:
aidex_query({ term: "render", mode: "starts_with" })
→ renderFrame (engine.ts:45), renderUI (app.ts:120)

Method signatures — see all methods without reading the file:
aidex_signature({ file: "src/engine.ts" })
→ class Engine { ... }
→ function renderFrame(delta: number): void

Time-based filtering — "what changed in the last 2 hours?":
aidex_query({ term: "render", modified_since: "2h" })

Cross-project search, session notes that persist between chats, a task backlog for tracking TODOs and bugs, and an
interactive browser viewer at localhost:3333.

Performance
┌─────────┬───────┬────────────┬────────────┐
│ Project │ Files │ Index time │ Query time │
├─────────┼───────┼────────────┼────────────┤
│ Small │ ~20 │ <1s │ 1-5ms │
├─────────┼───────┼────────────┼────────────┤
│ Medium │ ~100 │ <1s │ 1-5ms │
├─────────┼───────┼────────────┼────────────┤
│ Large │ ~500+ │ ~2s │ 1-10ms │
└─────────┴───────┴────────────┴────────────┘
Single SQLite file. No cloud, no telemetry. Everything runs locally.

11 languages: C#, TypeScript, JavaScript, Rust, Python, C, C++, Java, Go, PHP, Ruby

Setup in 30 seconds

npm install -g aidex-mcp
aidex setup

aidex setup auto-detects your AI tools and registers AiDex with them. Works with Claude Code, Claude Desktop, Cursor,
Windsurf, Gemini CLI, VS Code Copilot, and anything else that speaks MCP.

How it works

  1. Tree-sitter parses each file into an AST
  2. Extractor walks the AST, collects identifiers + method signatures
  3. SQLite stores everything (WAL mode, fast reads)
  4. MCP server exposes 20 tools via stdio transport
  5. Incremental updates — only changed files get re-indexed

The bottom line

AI assistants are getting better at writing code but still waste most of their context on finding code. Persistent
indexing fixes that. The AI gets instant, precise answers and spends its tokens on what matters — actually building
things.

I've been using it daily for months. The difference is immediately noticeable, especially on longer sessions.

Give it a try and let me know what you think — what's your biggest pain point with AI context usage?

GitHub: https://github.com/CSCSoftware/AiDex
npm: npm install -g aidex-mcp
MCP Registry: io.github.CSCSoftware/aidex


Open source, MIT licensed. 1200+ npm downloads. Contributions welcome.

Top comments (0)