queryLoop
The actual main loop implementation of the Claude Code agent, located in src/query.ts. It is a function based on an explicit state machine, where the state.transition.reason union type drives the path selection for each iteration.
Timeline (last 90 days)
Claude Code's Agent main loop is the queryLoop function in src/query.ts, not src/QueryEngine.ts.
queryLoop is a function of more than 1700 lines that implements an explicit state machine.
The main loop's state machine logic is to continue as long as the transition is not terminal, rather than a simple while (has_tool_use).
One iteration consists of five steps: building the request, calling the LLM, evaluating the result, running tools, and choosing the next transition.
Claude Code's recovery mechanism is a state transition within the loop rather than nested try/catch, and recoverable failures become the next round's transition.
All Facts (5)
Claude Code's Agent main loop is the queryLoop function in src/query.ts, not src/QueryEngine.ts.
50%UnverifiedqueryLoop is a function of more than 1700 lines that implements an explicit state machine.
50%UnverifiedThe main loop's state machine logic is to continue as long as the transition is not terminal, rather than a simple while (has_tool_use).
50%UnverifiedOne iteration consists of five steps: building the request, calling the LLM, evaluating the result, running tools, and choosing the next transition.
50%UnverifiedClaude Code's recovery mechanism is a state transition within the loop rather than nested try/catch, and recoverable failures become the next round's transition.
50%