---
title: "Discussion on \"My AI coding agent kept grepping my repo to find one function — so I cut its token usage by 90%\" | Hashnode"
description: "Discussion on \"My AI coding agent kept grepping my repo to find one function — so I cut its token usage by 90%\". I got tired of my AI agent grepping my repo, so I built Spelunk: a skill using Tree-sitter and SQLite that resolves AST symbols under 100ms."
url: "https://hashnode.com/posts/ai-agent-codebase-indexer-skill-spelunk/6a609b9ba874e83d3cbc81e5"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [My AI coding agent kept grepping my repo to find one function — so I cut its token usage by 90%](https://roypriyanshu02.hashnode.dev/ai-agent-codebase-indexer-skill-spelunk?utm_source=hashnode&utm_medium=feed)

I got tired of my AI agent grepping my repo, so I built Spelunk: a skill using Tree-sitter and SQLite that resolves AST symbols under 100ms.

Jul 22 · 6 min read · roypriyanshu02.hashnode.dev  
Read the article

-   [#ai](https://hashnode.com/tag/ai)
-   [#developer-tools](https://hashnode.com/tag/developer-tools)
-   [#typescript](https://hashnode.com/tag/typescript)
-   [#opensource](https://hashnode.com/tag/opensource)
-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#ai-agents](https://hashnode.com/tag/ai-agents)
-   [#agent-skills](https://hashnode.com/tag/agent-skills)
-   [#vibe-coding](https://hashnode.com/tag/vibe-coding)
-   [#coding-agents](https://hashnode.com/tag/coding-agents)
-   [#beginner](https://hashnode.com/tag/beginner)
-   [#production](https://hashnode.com/tag/production)

## Written by

[Priyanshu Roy](https://hashnode.com/@roypriyanshu02)

@roypriyanshu02 · Bangalore, India

  
[View profile](https://hashnode.com/@roypriyanshu02)

Think big. Start small. Build now.

## Responses 5

Markdown supported · ⌘↵ to post

[Kartik N V J K](https://hashnode.com/@kartik-nvj)

[Jul 22](https://hashnode.com/posts/ai-agent-codebase-indexer-skill-spelunk/6a609b9ba874e83d3cbc81e5/comment/6a6113d8377eaf776b54f181)

The Tree-sitter + SQLite combo for AST symbol lookup is the right call, and swapping blind grep/cat for a definition resolver is exactly where most of my agent token spend was hiding too. One thing I'd love to hear you dig into: how you handle stale index invalidation on partial writes, since that's the failure mode that eventually made me distrust the tool and re-grep anyway. The <100ms target is only useful if a returned symbol is guaranteed to reflect the file on disk at call time.

[Priyanshu Roy](https://hashnode.com/@roypriyanshu02)

[Jul 23](https://hashnode.com/posts/ai-agent-codebase-indexer-skill-spelunk/6a609b9ba874e83d3cbc81e5/comment/6a6113d8377eaf776b54f181#reply-6a62491ea043a23a8d93266b)

Thanks Kartik. Stale index invalidation was the exact issue that broke my earlier manual workarounds.

Spelunk handles disk changes by checking mtimes and diffing the AST on call time, while the agent keeps its own active session edits in working memory. Together, those cover most stale read scenarios.

Good point to bring up.

[Mateo Ruiz](https://hashnode.com/@mateoruiz)

[Jul 22](https://hashnode.com/posts/ai-agent-codebase-indexer-skill-spelunk/6a609b9ba874e83d3cbc81e5/comment/6a60a7df0865fa0f4a1513cc)

One thing we've noticed is that as repositories grow, retrieval becomes a much bigger bottleneck than generation. If an agent spends most of its time grepping and re-reading files, you're paying in both latency and tokens before it even starts reasoning.

Using AST-based indexing is a smart approach because it gives the agent structural context instead of raw text. It would also be interesting to see benchmarks on incremental re-indexing for large monorepos and how it performs alongside RAG- or embedding-based code retrieval. Nice first project looking forward to seeing how it evolves.

[Priyanshu Roy](https://hashnode.com/@roypriyanshu02)

[Jul 23](https://hashnode.com/posts/ai-agent-codebase-indexer-skill-spelunk/6a609b9ba874e83d3cbc81e5/comment/6a60a7df0865fa0f4a1513cc#reply-6a624be5f7a3d39949bfb835)

Thanks Mateo! Completely agree that retrieval overhead is where most latency and token burn happen on larger projects.

Testing incremental re-indexing on large codebases is high on my list. Spelunk tracks mtimes and file hashes in SQLite, so rescans only re-parse modified files instead of running full passes.

Combining AST indexing with RAG or embeddings is a great idea. AST gives exact symbol resolution while RAG handles semantic search. I'm focusing on Amar right now, but I've noted this down for when I get back to Spelunk features. If you have thoughts on how that hybrid setup should work, feel free to open a GitHub issue.

[Vlad Zoff](https://hashnode.com/@vladzoff)

[Oct 28](https://hashnode.com/posts/ai-agent-codebase-indexer-skill-spelunk/6a609b9ba874e83d3cbc81e5/comment/6ae20a3277d5cbaa0c48c1bc)

Token reduction is a good start, but I'd also test retrieval quality after a symbol is renamed or moved across modules. An index that returns fewer tokens but points to stale locations can make the agent confidently edit the wrong implementation. Measuring recall on a small set of real repo questions would help show whether the savings are actually useful.
