Beyond grep for coding agents
For AI agents operating in codebases of all sizes, we want to be efficient with our token usage, not just for costs, but for time and…
Beyond grep for coding agents
For AI agents operating in codebases of all sizes, we want to be efficient with our token usage, not just for costs, but for time and performance.
Recently I was using Gemini CLI to ask a simple question of how reconciler backoff worked and what options were available to me in the controller-runtime package.
I watched fascinated as it used a loop of grep, sed and built in read_file tool to walk through the codebase and I instinctively frowned. While this worked, I was sure there was a better way.
Introducing ast-grep
Unlike line-oriented tools, ast-grep operates directly on the Abstract Syntax Tree (AST). This enables agents to target, analyze, and transform specific syntax nodes with high precision, ensuring context windows are populated only with the logical constructs required for the task.
Integrating structural search and manipulation enables deterministic code navigation and editing. This flattens the marginal cost of multi-file refactoring and improves reasoning accuracy by providing cleaner, structured input to the model.
I had Gemini evaluate the scenarios with and without the ast-grep skill enabled to compare the token costs:
Scenario 1: Text-based search (rg + read_file)
Text-based tools lack semantic awareness of code structure. In a complex framework like controller-runtime, keyword-based searches for common terms result in high-volume, low-signal payloads.
- Step 1: rg -i “backoff” pkg/, Extracts roughly 20 noisy lines (~250 tokens)
- Step 2: read_file on pkg/reconcile/reconcile.go to grasp the basic interface contract (~500 tokens)
- Step 3: rg -C 5 “Reconcile” pkg/internal/controller/ to locate the actual execution loop. Because “Reconcile” is ubiquitous, the agent receives a massive dump of tests, mocks, and variable declarations (~1,200 tokens)
- Step 4: rg -C 5 “RateLimiter” pkg/controller/ to find default configurations, resulting in another flooded payload (~1,000 tokens)
Scenario 2: AST-based search (ast-grep)
By leveraging AST pattern matching, the agent executes precise queries and transformations that isolate definitions or function bodies, eliminating textual noise and preventing accidental side effects during editing.
- Overhead: The agent must first read SKILL.md to understand how to format ast-grep queries (~1,800 tokens)
- Step 1: sg run -p ‘RateLimiter: $VAL’ Instantly finds exact structural assignments of the rate limiter (~150 tokens)
- Step 2: sg run -p ‘func DefaultTypedControllerRateLimiter($$$) { $$$ }’ Returns strictly the function signature and body, completely ignoring tests and documentation (~200 tokens)
- Step 3 (The Guess): Finding the execution loop via func (c *Controller[R])reconcileHandler($$) { $$$ }
- Failed attempt (syntax mismatch): ~100 tokens + 1 extra turn
- Successful correction: Returns exactly the method body (~300 tokens)
Efficiency of ast-grep
- Without ast-grep, I used roughly 2950 tokens, with ast-grep I used 2550 tokens
- Without ast-grep, each search used between 250 and 1200 tokens
- With ast-grep, each search used between 150–300 tokens
- The primary overhead of ast-grep was the loading of the skill (roughly 1800 tokens)
While the initial overhead of loading the ast-grep skill (1,800 tokens) eats into the immediate savings, the long-term ROI for complex tasks is undeniable. In multi-step workflows, the drastic reduction in per-search tokens compounds quickly.
More importantly, this structural precision prevents context pollution, which will fundamentally improve the agent’s reasoning speed and accuracy.
More than a search tool
The benefits of using an AST aware tool go beyond reading code, it extends to editing and updating code, allowing for agents to perform more deterministic editing.
AST based editing understands scopes, syntax (function names vs variables, etc) and can perform more intelligent refactoring. One such use case is addressing API deprecations and automating changes across hundreds or thousands of files much more efficiently.
Further reading
Following the use of ast-grep, I learned about the tree-sitter library that ast-grep uses and an interesting looking skill that uses tree-sitter as well:
메타데이터
- post_id
- c2cb251b3ffb
- slug
- beyond-grep-for-coding-agents-c2cb251b3ffb
- url
- https://medium.com/@fruitcup/beyond-grep-for-coding-agents-c2cb251b3ffb
- canonical_url
- https://medium.com/@fruitcup/beyond-grep-for-coding-agents-c2cb251b3ffb
- author_url
- https://medium.com/@fruitcup
- status
- ok
- fetched_at
- 2026-07-16 19:11:01