Skip to content

Pattern Catalog

Quick reference for the architectural patterns behind Claude Code. Each pattern is a proven engineering solution — applicable to any agentic system, not just coding tools.


Engine Patterns

PatternWhat It DoesLearn More
Async Generator Control FlowThe agent loop uses async function* for pull-based streaming. Consumer controls pace; cancel via .return(); pause by not calling next().The Agent Loop
Derived Flag over API SignalneedsFollowUp is derived from observing tool_use blocks in the response — not from trusting stop_reason metadata. Observe behavior, not labels.The Agent Loop
Escalating RecoveryRetry → increase budget → inject recovery message → switch model → surface error. Each level has a circuit breaker. No level retries itself.The Agent Loop
Concurrency-Safe PartitioningEach tool declares safe or exclusive per invocation. Default: exclusive. Tools are batched into concurrent and serial groups at runtime.Tool Orchestration
Streaming Tool ExecutionTools begin executing as soon as their block arrives in the API stream — before the full response is received. LLM generation overlaps with tool I/O.Tool Orchestration
Context Modifier ChainSerial tools can modify shared context for the next tool. Concurrent tools cannot — non-deterministic order would create race conditions.Tool Orchestration

Coordination Patterns

PatternWhat It DoesLearn More
Coordinator RestrictionCoordinator agent limited to 4 tools (create team, delete team, send message, output). Cannot code. Must delegate. Constraint by code, not instruction.Multi-Agent System
Fork Isolation via WorktreeEach fork agent gets a Git worktree on its own branch. Prompt cache shared via byte-identical API prefixes. Anti-recursive guard prevents fork explosion.Multi-Agent System
Disk-Based Async OutputBackground tasks write to files. Readers use outputOffset for incremental reads. Crash-safe, unlimited size, multiple readers.Context & Memory

Security Patterns

PatternWhat It DoesLearn More
Permission Classification Pipeline6-layer classify-then-decide: safe allowlist → mode → rules → dangerous patterns → AST analysis → denial tracking. 99% fast-pathed at layer 1.Permission Pipeline
Denial Tracking Circuit BreakerAfter 3 consecutive denials or 20 total, falls back to prompting user directly. Prevents permission fatigue from repetitive blocked actions.Permission Pipeline

Memory Patterns

PatternWhat It DoesLearn More
Multi-Layer Context Defense5 escalating layers: truncate → microcompact → auto-compact → reactive compact → context collapse. Each runs once; failure escalates.Context & Memory
Compact Boundary (Controlled Forgetting)LLM summarizes conversation into a boundary message. Everything before the boundary is discarded. Keeps decisions, drops transcripts.Context & Memory

Extension Patterns

PatternWhat It DoesLearn More
Conditional Skill ActivationSkills activate by file path patterns. Directory walk discovers .claude/skills/ in ancestor directories. Monorepo-aware.Skill Engine
Shell-in-Prompt InjectionSkills embed shell commands that execute before prompt injection. MCP skills blocked from shell access (remote/untrusted boundary).Skill Engine
4-Point Plugin ExtensionPlugins provide commands + agents + hooks + servers. Single install, multiple capabilities.Plugin Engine
Plugin Security SandboxPlugin agents cannot escalate permissions beyond install-time trust. Per-agent permissionMode, hooks, mcpServers blocked.Plugin Engine
Reconciliation-Based InstallKubernetes-style: diff desired state (settings) vs actual state (disk). Apply changes. Background, non-blocking, hot-reloadable.Plugin Engine

How to Read This

These patterns are grouped by concern, not by importance. Most systems will use a subset. Start with the Engine Patterns — they form the foundation everything else builds on.

Every pattern is general-purpose engineering. Replace FileRead with SensorDataTool, replace Bash with RobotControlTool, and the same patterns apply. The architecture is domain-agnostic; coding is just one application.

Why This Matters to You

  • Building your own agent? Start with the Engine Patterns — async generator loop + escalating recovery covers 80% of what you need
  • Extending Claude Code? The Extension Patterns show how skills, plugins, and hooks compose together
  • Debugging slow sessions? The Memory Patterns explain why /compact works and when to use it
  • Understanding permissions? The Security Patterns demystify why Claude asks or doesn’t ask for approval

See also: Architecture OverviewThe Agent LoopPermission Pipeline