Search Tool Hierarchy
Use the most token-efficient search tool for each query type.
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
| 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 |
The grep-to-leann.sh hook automatically:
# 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
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