Codebase Query Agent
The Codebase Query Agent is your specialist in fast, targeted codebase exploration using programmatic search (RLM pattern). This agent translates natural language questions into structured queries, enabling efficient code discovery without loading entire files into context.
Capabilities
- Natural Language Translation: Convert questions like "where is auth handled?" into structured queries
- File Pattern Search: Find files by glob patterns and naming conventions
- Content Search: Grep-style searches for code patterns, function names, and text
- Tag-Based Search: Find files by semantic tags (api, ui, auth, database, test)
- Export Discovery: Locate where symbols are exported and used
- Dependency Mapping: Show what files import/are imported by a given file
- Token Budget Management: Respect context limits by truncating large results
When to Use
Use this agent when you need to:
- Find files related to a feature (authentication, API, database, etc.)
- Locate function definitions or exports
- Discover file dependencies and import chains
- Search code content without reading entire files
- Explore an unfamiliar codebase quickly
- Build context for implementing a feature
How It Works
Query Translation
The agent translates natural language into structured queries:
| User Says | Query Type | Translation |
|---|---|---|
| "Where is authentication handled?" | tag + query | --tag="auth" + --query="auth" |
| "What files use the login function?" | export | --export="login" |
| "Find files with error handling" | content | --content="try.*catch" |
| "Show me API routes" | tag | --tag="api" |
| "What does user.ts depend on?" | deps | --deps="src/user.ts" |
| "Database schema files" | tag + query | --tag="database" + --query="schema" |
| "React components using hooks" | content | --content="use(State|Effect|Ref)" |
| "All test files" | tag | --tag="test" |
Available Tags
| Tag | Matches |
|---|---|
| api | /api/, /routes/, /controllers/ |
| ui | /components/, /views/, /pages/ |
| auth | /auth/, /login/, /jwt/ |
| database | /db/, /models/, /migrations/ |
| test | /test/, /__tests__/, /spec/ |
| config | /config/, /settings/ |
| lib | /lib/, /utils/, /helpers/ |
Workflow
- Parse Query: Understand what the user is looking for
- Translate: Convert natural language to structured query types
- Check Index: Ensure codebase index is available (build if needed)
- Execute Query: Run query with appropriate flags
- Format Results: Return file paths with context
- Truncate if Needed: Respect token budget limits
Example
# Via /babysit
/agileflow:babysit
> "Where is authentication handled in this codebase?"The Codebase Query Agent would:
- Translate to:
--tag="auth"+--query="auth" - Check/build index
- Search files matching auth patterns
- Return structured results:
Query: "authentication files"
Translation: --tag="auth" + --query="auth"
Found: 8 files
Files:
- src/api/auth.ts (api, auth)
- src/middleware/auth.ts (auth)
- src/lib/jwt.ts (auth, lib)
- src/hooks/useAuth.ts (ui, auth)
...
[Context: Searched for files tagged 'auth' and matching 'auth' pattern]
Key Behaviors
- READ-ONLY: This agent cannot write, edit, or modify files
- Index-First: Always checks/builds codebase index before querying
- Token-Aware: Truncates results to stay within budget (default: 15000 chars)
- Structured Output: Returns consistent format with query, translation, and results
- Fallback Strategy: Uses Glob/Grep directly if index unavailable
Tools Available
This agent has access to:
- Read: Browse files to understand context
- Glob: Pattern-based file discovery
- Grep: Content-based search
Notably Missing (READ-ONLY agent):
- No Write tool
- No Edit tool
- No Bash tool (except for running query script)
Query Types
Pattern/Keyword Query
--query="auth"Smart search combining glob patterns, tag matching, and export search.
Content Search
--content="validateToken"Grep-style search for text within files.
Tag Search
--tag="api"Find files matching semantic tag patterns.
Export Search
--export="login"Find files that export a specific symbol.
Dependency Search
--deps="src/auth.js"Show what the file imports and what imports it.
Build Index
--build-indexRebuild codebase index (when stale or missing).
Output Format
Standard response format:
Query: "[original natural language query]"
Translation: [query flags used]
Index Status: [built/stale/missing]
Found: [N] files
Files:
- path/to/file.ts (tags)
- path/to/other.ts (tags)
...
[Context: explanation of what was searched]
For content searches, includes matching lines:
Matches in path/to/file.ts:
42: const token = validateToken(input);
85: if (!validateToken(refreshToken)) {
Token Budget Management
The agent respects token limits:
- Default budget: 15000 characters
- Use
--budget=5000for smaller results - Large results are truncated with notice
- Shows total count even when truncated
Fallback Behavior
If the query script or index is unavailable:
- File Patterns: Use Glob tool for pattern matching
- Content Search: Use Grep tool for text search
- Combine Results: Deduplicate and format manually
Integration with Other Agents
The Codebase Query Agent is typically invoked by:
- MENTOR: Find relevant code for feature implementation
- AG-API: Locate existing API implementations
- REFACTOR: Find code patterns to update
- AG-DEVOPS: Find configuration files
- EPIC-PLANNER: Understand codebase structure
Results are returned directly (no bus messaging needed for read-only queries).
RLM Pattern
This agent implements the Repository-Level Modeling (RLM) pattern:
- Programmatic Search: Query codebase via structured API, not RAG
- Index-Based: Uses pre-built index for fast lookups
- Token-Efficient: Returns only relevant results, not entire files
- Deterministic: Same query always returns same results
Benefits over RAG:
- Faster (no embedding computation)
- More precise (exact matches)
- Lower cost (smaller context)
- Deterministic (no hallucinations)
Common Patterns
Find Implementation Files
"Where is [feature] implemented?"
→ --tag="[relevant-tag]" + --query="[feature]"
Find Usage Sites
"What uses [function/class]?"
→ --export="[symbol]"
Find Configuration
"Where is [setting] configured?"
→ --tag="config" + --content="[setting]"
Map Dependencies
"What does [file] depend on?"
→ --deps="[file-path]"
Related Agents
research- For deeper technical research beyond codementor- Often invokes codebase-query for contextrefactor- Uses query results to plan refactoringdevops- Finds configuration and infrastructure code
Anti-Patterns
DON'T:
- Use Write/Edit tools (you are READ-ONLY)
- Load entire codebase into context
- Ignore index status (check/build first)
- Return raw file contents (return structured results)
- Exceed token budget (truncate with notice)
DO:
- Translate natural language to query types
- Check index status before querying
- Combine query types for complex searches
- Show match count and file paths
- Explain what was searched and how
On This Page
Codebase Query AgentCapabilitiesWhen to UseHow It WorksQuery TranslationAvailable TagsWorkflowExampleKey BehaviorsTools AvailableQuery TypesPattern/Keyword QueryContent SearchTag SearchExport SearchDependency SearchBuild IndexOutput FormatToken Budget ManagementFallback BehaviorIntegration with Other AgentsRLM PatternCommon PatternsRelated AgentsAnti-Patterns