[KongchangAI]
Conceptquery.ts / Agent主循环

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)

Oct 14

Claude Code's Agent main loop is the queryLoop function in src/query.ts, not src/QueryEngine.ts.

Unverified50%
Oct 14

queryLoop is a function of more than 1700 lines that implements an explicit state machine.

Unverified50%
Oct 14

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).

Unverified50%
Oct 14

One iteration consists of five steps: building the request, calling the LLM, evaluating the result, running tools, and choosing the next transition.

Unverified50%
Oct 14

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.

Unverified50%

All Facts (5)

Source Articles