# Agentic loop

> Claude Code runs a gather → act → verify loop: startup load fills context before you type, tool output piles up, and compaction, /clear, subagents trim it.

URL: https://bartlomiejkrupa.dev/notes/agentic-loop/
Category: concept | Updated: 2026-07-29
Summary: Claude Code runs a gather → act → verify loop: startup load fills context before you type, tool output piles up, and compaction, /clear, subagents trim it.

The **agentic loop** is three phases Claude Code cycles on every task: gather context (read, search, understand), take an action (invoke a tool), verify results (check whether the action moved toward the goal). A question might only gather; a refactor loops through all three dozens of times. Each tool result feeds the next decision - that feedback is the loop.

## What fills the window

Context starts filling before you type - the [startup tax](/notes/startup-tax) (system prompt, `CLAUDE.md`, tool names, skill descriptions, [auto memory](/notes/auto-memory)). During the loop, file reads dominate growth: each read stays in history for every subsequent turn. Path-scoped rules and nested `CLAUDE.md` files load when Claude reads a matching path. PostToolUse hooks can inject `additionalContext` via JSON.

When context drifts mid-session (~turn 30), a constraint may still be in the transcript but no longer near either edge - the [lost in the middle](/notes/lost-in-the-middle) failure. Put anything that must hold for the whole session in a file that gets re-injected (project-root `CLAUDE.md`, or a `SessionStart` hook with `source: "compact"`), not in a chat message.

## Levers

- Compaction / `/compact` - summarize history; project-root `CLAUDE.md` re-injects from disk.
- `/clear` - hard reset of message history.
- [Subagent context isolation](/notes/subagent-context-isolation) - heavy work in a child window, summary back to the parent.

Require the agent to read before it writes and end the task with a command that fails loudly when the target is missing - invented paths become context for everything after them.

Mechanics playbook: [How Claude Code's agentic loop fills your context window](/articles/claude-code-agentic-loop-context). Discipline: [Context engineering](/notes/context-engineering).

Sources:
- How Claude Code works: https://code.claude.com/docs/en/how-claude-code-works
- Explore the context window: https://code.claude.com/docs/en/context-window
- How Claude remembers your project: https://code.claude.com/docs/en/memory
- Hooks reference: https://code.claude.com/docs/en/hooks
