Search Hierarchy
Routes code search queries to the most token-efficient tool: AST-grep for structural patterns like d
by parcadei · MIT
The skill
Routes code search queries to the most token-efficient tool: AST-grep for structural patterns like def, class, import or decorators, LEANN for conceptual how-does-it-work questions, Grep for exact identifiers and regex, and Read only after finding the file. Use when searching a codebase with ast-grep, leann or grep, or choosing which search tool to run before reading source files.
Search Tool Hierarchy
Use the most token-efficient search tool for each query type.
Decision Tree
Query Type?
├── STRUCTURAL (code patterns)
│ → AST-grep (~50 tokens output)
│ Examples: "def foo", "class Bar", "import X", "@decorator"
│
├── SEMANTIC (conceptual questions)
│ → LEANN (~100 tokens if path-only)
│ Examples: "how does auth work", "find error handling patterns"
│
├── LITERAL (exact identifiers)
│ → Grep (variable output)
│ Examples: "TemporalMemory", "check_evocation", regex patterns
│
└── FULL CONTEXT (need complete understanding)
→ Read (1500+ tokens)
Last resort after finding the right file
Token Efficiency Comparison
| Tool | Output Size | Best For |
|---|---|---|
| AST-grep | ~50 tokens | Function/class definitions, imports, decorators |
| LEANN | ~100 tokens | Conceptual questions, architecture, patterns |
| Grep | ~200-2000 | Exact identifiers, regex, file paths |
| Read | ~1500+ | Full understanding after finding the file |
Hook Enforcement
The grep-to-leann.sh hook automatically:
- Detects query type (structural/semantic/literal)
- Blocks and suggests AST-grep for structural queries
- Blocks and suggests LEANN for semantic queries
- Allows literal patterns through to Grep
DO
- Start with AST-grep for code structure questions
- Use LEANN for "how does X work" questions
- Use Grep only for exact identifier matches
- Read files only after finding them via search
DON'T
- Use Grep for conceptual questions (returns nothing)
- Read files before knowing which ones are relevant
- Use Read when AST-grep would give file:line
- Ignore hook suggestions
Examples
# STRUCTURAL → AST-grep
ast-grep --pattern "async def $FUNC($$$):" --lang python
# SEMANTIC → LEANN
leann search opc-dev "how does authentication work" --top-k 3
# LITERAL → Grep
Grep pattern="check_evocation" path=opc/scripts
# FULL CONTEXT → Read (after finding file)
Read file_path=opc/scripts/z3_erotetic.py
Optimal Flow
1. AST-grep: "Find async functions" → 3 file:line matches
2. Read: Top match only → Full understanding
3. Skip: 4 irrelevant files → 6000 tokens saved
Use this skill
Connect your agent to the Skill Store MCP server and ask for it by ref: parcadei/search-hierarchy-63261744551.
Details
- Publisher
- parcadei
- Category
- Software Development › Developer docs & tooling
- License
- MIT
- Updated
- 2026-01-26
References
More from parcadei
- Trace Claude Code Automatically trace Claude Code conversations to Braintrust for observability.
- Agent Context Isolation Guides orchestration of Claude Code subagents (Task tool) to keep their output out of the main conte
- Agent Orchestration Agent Orchestration Rules
- Agentic Workflow Orchestrates a five-stage multi-agent pipeline for implementation tasks: research via NIA Oracle, pl