Query Engine
Source: src/QueryEngine.ts (1,295 lines) and src/query.ts (1,729 lines)
The Query Engine is the core orchestration layer that manages the conversation loop between the user, Claude AI, and tools.
Responsibilities
- Message Management — Build and maintain the conversation history
- API Communication — Send queries to the Claude API with streaming
- Tool Call Handling — Execute tool calls from AI responses and feed results back
- Context Assembly — Gather system prompts, user context, and tool definitions
- Error Recovery — Handle API errors, rate limits, and tool failures
Query Lifecycle
Context Assembly
The query engine assembles context from multiple sources:
- System Prompt — Base instructions for Claude (from
src/constants/) - User Context — CLAUDE.md files, memory files
- Tool Definitions — Available tools with their JSON schemas
- Conversation History — Previous messages in the session
- Project Context — Git status, file structure, environment info
Streaming
Responses from the Claude API are streamed token-by-token. The Query Engine processes streaming events to:
- Render partial text responses in real-time
- Detect tool call blocks as they are streamed
- Track token usage for cost estimation
- Handle stop reasons (end_turn, tool_use, max_tokens)
Tool Call Loop
When the AI response includes tool calls:
- Parse tool call parameters from the response
- Check permissions (may prompt user)
- Execute the tool
- Collect the result (success or error)
- Append tool result to conversation
- Re-query the API with updated conversation
This loop continues until the AI produces a final text response without tool calls.
Key Types
The query system uses types defined in src/types/message.ts:
UserMessage— User text inputAssistantMessage— AI response (text and/or tool calls)SystemMessage— System-level contextProgressMessage— Tool execution progress updates
Deep Dive
- Context Assembly — How system prompt, tools, and user context are assembled into a single API call
- Streaming Pipeline — Token-by-token streaming, event dispatch, and stop reason handling
- Tool Call Loop — The complete permission → execute → collect → re-query cycle
- Error Recovery — API error handling, rate limiting, retry logic, and tool failure recovery
Last updated on