Back to Claude Code hub
BEGINNERsnapshot-basedVerified 2026-03-31core

How Claude Code Works

The high-level architecture behind a Claude Code turn

A code-backed explanation of how Claude Code assembles context, runs the agent loop, invokes tools, and keeps long sessions moving.

The turn starts with context assembly

Claude Code does not start from a blank prompt. It prepends system context, user context, repository state, and date information before the main loop begins.

The context layer also decides whether Git status, CLAUDE.md instructions, and other session metadata should be injected or skipped.

  • System context can include a git snapshot and cache-breaking prompt injection.
  • User context can include discovered CLAUDE.md content and the local date.
  • The context snapshot is intentionally memoized so a single conversation stays coherent.

The query loop is the real engine

The `query` path orchestrates the main Claude Code turn. It normalizes messages, streams assistant output, detects tool use, runs tools, and feeds tool results back into the loop.

This means a single user request can become a multi-step trajectory rather than a one-shot model completion.

  • Messages are normalized before the API call.
  • Tool-use blocks can trigger a second or third assistant step within the same turn.
  • Recovery paths exist for prompt pressure and max-output-token failures.
A practical mental model: Claude Code is a loop over context -> model output -> tool execution -> updated context, not just a chat box.

Tools are curated and mode-sensitive

Tool availability is centrally assembled, then filtered by user type, feature flags, mode, and agent context.

That is why the same conceptual assistant can behave differently in normal mode, plan mode, coordinator mode, worktree mode, or sub-agent contexts.

  • Base tools include read/edit/write, shell, web, search, plans, tasks, and agents.
  • Some tools are explicitly blocked for async agents or coordinator mode.
  • Feature flags expose more specialized tools such as workflows, browser control, or background session features.

Long sessions rely on compaction and budgeting

The query path contains explicit token-budget and compaction logic because long-running development sessions will otherwise hit context and output ceilings.

Claude Code therefore treats context pressure as an expected operating condition, not an exception.

  • Auto-compaction tracks long histories.
  • Reactive recovery exists for max-output-token failures.
  • Context pressure handling is a first-class part of the architecture.

Learn next

These guides are adjacent to the current topic and make the learning path easier to follow.