Studio Notebook

Claude Code Atlas

The Query Engine And Turn Lifecycle

Follow one turn from prepared messages to streamed output and post-processing.

Why this matters

If you only learn one runtime subsystem, learn this one. The query layer is the heart of how Claude Code thinks, streams, stops, and recovers.

Big picture first

The query subsystem takes prepared messages, runs the model loop, reacts to tool calls, and decides whether the session should continue, compact, or stop.

One turn through the query engine

  1. Start with prepared messages, config, and tool context.
  2. Enter the main loop in query.ts.
  3. Stream assistant output and detect tool uses.
  4. Run tools, stop hooks, or recovery paths when needed.
  5. Return a terminal state for the turn.

How this part breaks down

  1. query-loop-and-state-machine This is the main runtime loop and the mutable state it carries.
  2. query-prompts-and-recovery-messages This is the direct text the system injects when it needs special behavior.
  3. query-budget-and-stop-hooks This is where the runtime decides whether to continue or switch into post-turn work.
  4. query-tool-orchestration This is how tool calls become actual execution.
  5. query-compaction-and-recovery This is how the runtime shrinks history and survives prompt-too-long situations.

Takeaways

  • The query root is a parent chapter, not a file-by-file deep dive.
  • Each child page follows the same top-down teaching rhythm.
  • You should understand the loop before you study edge recovery paths.