The Agentic Loop

Claude Code operates on an agentic loop1 — a cycle of reasoning, tool use, and observation:

  1. User prompt — The user provides a task or question.
  2. Gather context — Claude searches files, reads code, and builds understanding.
  3. Take action — Claude edits files, runs commands, calls external tools.
  4. Verify results — Claude runs tests, checks output, compares to expectations.
  5. Iterate — Steps 2–4 repeat, course-correcting based on failures, until the task is complete.
  6. Response — Claude presents the final result to the user.

The user is part of this loop — you can interrupt at any point to steer, provide context, or try a different approach. Press Esc to interrupt a running action.

Tool System

Tools make Claude Code agentic. Without them, Claude could only respond with text. Five categories:

File Operations

ToolPurpose
ReadRead file contents (supports code, images, PDFs, Jupyter notebooks)
EditMake targeted string replacements in files
WriteCreate new files or fully rewrite existing ones
GlobFind files by pattern (e.g., **/*.ts)
NotebookEditEdit Jupyter notebook cells
ToolPurpose
GrepSearch file contents with regex (built on ripgrep)
GlobFind files by name/path pattern

Execution

ToolPurpose
BashExecute arbitrary shell commands

Web

ToolPurpose
WebFetchFetch and process web content
WebSearchSearch the web for information

Orchestration

ToolPurpose
AgentLaunch sub-agents for parallel or isolated work
TodoWriteTrack task progress in a structured list
AskUserQuestionGet clarification or decisions from the user

Tools can be called in parallel when their inputs are independent, improving throughput on complex tasks. Claude chooses which tools to use based on the prompt and what it learns at each step.

Example tool chain for “fix failing tests”:

  1. Bash — Run test suite, see what’s failing
  2. Read — Read error output, understand the issue
  3. Grep / Glob — Search for relevant files
  4. Read — Read files for context
  5. Edit — Implement the fix
  6. Bash — Run tests again to verify

Sub-Agents

The Agent tool spawns specialized sub-agents2 that run as child processes with their own context windows:

Agent TypePurposeTools Available
ExploreFast codebase explorationGlob, Grep, Read (read-only)
PlanArchitecture and implementation planningAll read tools
general-purposeFull tool access for complex sub-tasksAll tools
claude-code-guideDocumentation and usage guidanceRead, Web tools

Execution modes:

  • Foreground — Blocking; use when results are needed immediately.
  • Background — Non-blocking; for independent parallel work. You’re notified on completion.
  • Worktree isolationisolation: "worktree" gives the agent an isolated git worktree copy of the repo.

Custom sub-agents can be created via /agents or by placing AGENT.md files in ~/.claude/agents/ or .claude/agents/. Custom agents support:

  • Tool whitelisting/blacklisting
  • Model selection (sonnet, opus, haiku)
  • Persistent memory across sessions
  • Lifecycle hooks
  • MCP server access

When to use sub-agents vs. main conversation:

  • Sub-agents: Verbose output, context isolation, restricted tools, parallel work
  • Main conversation: Frequent back-and-forth, shared context across phases

Context Management

Claude Code automatically manages its context window3:

  • Auto-compaction — When approaching ~95% of context limits, Claude automatically clears old tool outputs first, then summarizes the conversation. CLAUDE.md instructions are preserved through compaction.
  • On-demand reading — Files are read on-demand rather than pre-loaded, keeping context focused.
  • Manual compaction — The /compact command manually summarizes the conversation. Use /compact focus on X to re-focus context on a specific topic.
  • Context inspection/context shows what’s consuming context space.
  • Cost tracking — Token usage and costs are displayed per session via /cost.

Context Cost by Feature

FeatureWhen LoadedCostBest For
CLAUDE.mdSession startEvery requestAlways-on rules
SkillsStart (description) + invocation (content)Low → HighOn-demand knowledge
MCP servers4Session start (tool definitions)Every requestExternal connections
Sub-agentsOn spawnIsolated contextContext isolation
HooksOn eventZero (unless returns output)Automation

Execution Environments

Claude Code runs in three environments:

EnvironmentWhere Code RunsUse Case
LocalYour machineDefault; full access to files, tools, environment
CloudAnthropic-managed VMsOffload tasks; work on repos you don’t have locally
Remote ControlYour machine, controlled from browserWeb UI while keeping everything local

Model Usage

  • Claude Sonnet — Default for most coding tasks; good balance of speed and capability.
  • Claude Opus — Stronger reasoning for complex architectural decisions.
  • Claude Haiku — Fast and cheap for simple tasks; used by Explore sub-agents.
  • Switch with /model during a session or claude --model <name> at startup5.
  • Sub-agents may use different models depending on task complexity.

Safety Mechanisms

  • Checkpoints — Every file edit is reversible. Press Esc twice to rewind to previous state, or ask Claude to undo.
  • Permission prompts — Dangerous operations require explicit approval.
  • Plan mode — Claude uses read-only tools only, creates a plan you can approve before execution.
  • Sandboxing — Optional filesystem and network restrictions via settings.

Footnotes

References

Footnotes

  1. Claude Code Architecture Overview

  2. Claude Code Sub-Agents

  3. Claude Code Context Management

  4. Model Context Protocol Specification

  5. Claude Code Documentation