Skip to main content

Claude Code Architecture

Understanding Claude Code's architecture helps you leverage SpecWeave's full power. This guide explains how the core components work and relate to each other.

Official Documentation

This guide is based on the official Claude Code documentation and the Claude Agent SDK.

Cross-Platform Compatibility

SpecWeave works on all platforms (macOS, Linux, Windows) and with multiple AI tools:

  • Claude Code — Native integration (best experience)
  • Cursor, Windsurf, GitHub Copilot — Via CLAUDE.md instructions
  • Any AI IDE — Via specweave generate --template=md

While Claude Code provides the deepest integration with native hooks and skills, SpecWeave's core workflow (spec.md, plan.md, tasks.md) works with any AI coding assistant.


The Big Picture

Claude Code is an extensible AI coding assistant with a plugin-based architecture. Everything flows through plugins, which bundle related functionality together.


Component Relationships

How They Differ

ComponentInvocationPurposeRuns In
SkillsAuto (keyword triggers)Provide expertise in conversationMain context
AgentsSpawned (Task tool)Execute complex isolated tasksSeparate context
CommandsManual (/slash)Perform specific actionsMain context
HooksAuto (events)React to lifecycle eventsBackground

The Flow

┌─────────────────────────────────────────────────────────────────────┐
│ USER INTERACTION │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ User: "Review this code for security issues" │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────┐ │
│ │ SKILL MATCHING (Auto) │ │
│ │ "security" keyword detected │ │
│ │ → sw:security skill activates │ │
│ └──────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────┐ │
│ │ SKILL PROVIDES EXPERTISE │ │
│ │ Security patterns, OWASP, etc. │ │
│ │ Claude knows HOW to review │ │
│ └──────────────────────────────────────┘ │
│ │ │
│ Complex task? Spawns agent │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────┐ │
│ │ AGENT (Isolated Context) │ │
│ │ Runs security scan in subprocess │ │
│ │ Returns results when done │ │
│ └──────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘

📦 Plugins: The Container

Plugins are packages that bundle related functionality. Each plugin can contain any combination of skills, agents, commands, hooks, and MCP servers.

Plugin Structure

my-plugin/
├── .claude-plugin/
│ └── plugin.json # Required: plugin manifest
├── commands/ # Custom slash commands
│ └── my-command.md
├── agents/ # Custom agents (subagents)
│ └── specialist.md
├── skills/ # Agent Skills
│ └── my-skill/
│ └── SKILL.md
├── hooks/ # Event handlers
│ └── hooks.json
└── .mcp.json # MCP server definitions

SpecWeave Plugins (24 total)

PluginSkillsAgentsCommandsPurpose
sw (core)92222Increment lifecycle
sw-github218GitHub sync
sw-jira218JIRA sync
sw-frontend330React/Vue/Next.js
sw-backend221Node.js/Python APIs
sw-k8s110Kubernetes/GitOps
sw-ml234ML pipelines
sw-infra551DevOps/SRE
...

Total: 136 skills, 68 agents, 53 commands


⚡ Skills: Auto-Activating Expertise

Skills are SKILL.md files that Claude automatically loads when relevant keywords appear in your conversation.

How Skills Work

  1. Discovery: Skills are discovered at startup from .claude/skills/ directories
  2. Matching: Claude matches your request against skill descriptions
  3. Loading: Only relevant skill content is loaded (saves tokens!)
  4. Guidance: The skill provides expertise inline in your conversation

Skill Anatomy

---
name: security-review
description: Security expert for code review and vulnerability detection
triggers:
- security
- owasp
- vulnerability
- authentication
allowed-tools: Read, Grep, Glob
context: fork # NEW in 2.1.0: Run in isolated sub-agent
model: opus # NEW in 2.1.0: Specify execution model
---

# Security Review Skill

When reviewing code for security issues:

1. Check for OWASP Top 10 vulnerabilities
2. Review authentication and authorization
3. Look for SQL injection, XSS, CSRF
...

Key Properties

  • Auto-activate: No manual invocation needed
  • Main context: Runs in your conversation (not isolated) — unless context: fork is set
  • Keyword triggers: Activates when description matches
  • Token efficient: Only loaded when needed

New Frontmatter Fields (Claude Code 2.1.0+)

FieldPurposeExample
context: forkRun skill in isolated sub-agent contextPrevents context pollution
modelSpecify which model executesopus, sonnet, haiku
hooksEmbed skill-scoped hooksSee Hooks section below

Example Flow

User: "Check this login function for security issues"


┌─────────────────────────┐
│ Keyword: "security" │
│ → security skill loads │
└─────────────────────────┘


Claude now has security expertise
and reviews code accordingly

🤖 Agents: Isolated Task Execution

Agents (subagents) are separate Claude instances spawned to handle complex tasks in isolation.

How Agents Work

  1. Spawning: Main Claude uses the Task tool to spawn an agent
  2. Isolation: Agent runs with its own context (doesn't pollute main conversation)
  3. Tools: Agent can have restricted tool access
  4. Return: Agent completes and returns results to main Claude

Agent Anatomy

---
description: Expert code review specialist for quality, security, and maintainability
model: sonnet
tools: Read, Grep, Glob
---

# Code Reviewer Agent

You are a code review specialist. When reviewing:

1. Identify security vulnerabilities
2. Check for performance issues
3. Verify coding standards
...

Key Properties

  • Isolated context: Separate from main conversation
  • Spawned explicitly: Via Task tool or user request
  • Tool restrictions: Can limit what the agent can do
  • Model override: Can use different model (haiku for speed)

Why Use Agents?

BenefitDescription
Context isolationComplex tasks don't pollute main conversation
ParallelizationMultiple agents can run concurrently
SpecializationTailored instructions for specific domains
Tool restrictionsRead-only agents can't modify files

Example Flow

User: "Use the k8s architect to design my deployment"


┌─────────────────────────┐
│ Task tool spawns │
│ kubernetes-architect │
└─────────────────────────┘


┌─────────────────────────┐
│ Agent works in │
│ isolated context │
│ (generates manifests) │
└─────────────────────────┘


Results returned to main Claude

📝 Commands: User-Invoked Actions

Commands are slash commands (/command) that users explicitly invoke.

How Commands Work

  1. Discovery: Commands from .claude/commands/ are listed at startup
  2. Invocation: User types /command-name or /plugin:command
  3. Execution: Command content becomes instructions for Claude
  4. Arguments: Can accept $ARGUMENTS or positional $1, $2

Command Anatomy

---
description: Create git commit with intelligent message
allowed-tools: Bash, Read, Grep
argument-hint: [optional message]
---

# Commit Command

1. Run `git status` to see changes
2. Run `git diff HEAD` to understand modifications
3. Generate a concise commit message
4. Create the commit: `git commit -m "message"`

Key Properties

  • User-invoked: Only runs when explicitly called
  • Namespaced: Plugin commands use plugin:command format
  • Arguments: Can accept parameters
  • Tool restrictions: Can limit available tools

Built-in vs Custom Commands

TypeLocationExample
Built-inClaude Code core/compact, /clear, /help
Plugin.claude/plugins/*/commands//sw:increment, /sw-github:sync
Project.claude/commands/Custom project commands
Personal~/.claude/commands/Your global commands

🪝 Hooks: Event-Driven Automation

Hooks are scripts that run automatically when specific events occur.

Hook Events

EventWhen It FiresCommon Use
PreToolUseBefore any tool executesBlock dangerous operations
PostToolUseAfter tool completesLog actions, update docs
StopSession endsSave state, cleanup
SubagentStopSubagent completesAggregate results
UserPromptSubmitUser sends messageInject context
SessionStartSession beginsInitialize logging

Hook Anatomy (hooks.json)

{
"hooks": [
{
"event": "PreToolUse",
"matcher": "Write|Edit",
"script": "./validate-file.sh"
},
{
"event": "PostToolUse",
"script": "./update-docs.sh"
}
]
}

Key Properties

  • Auto-execute: No user action required
  • Event-based: Fires on specific lifecycle events
  • Matchers: Can filter by tool name (for tool hooks)
  • Control flow: Can block, modify, or allow operations

Skill-Scoped Hooks (Claude Code 2.1.0+)

New in 2.1.0: Hooks can be embedded directly in skill/command frontmatter, scoped to the component's lifecycle:

---
name: sw:do
description: Execute increment tasks
hooks:
PostToolUse:
- matcher: Edit
hooks:
- type: command
command: bash plugins/specweave/hooks/v2/guards/task-ac-sync-guard.sh
---

Benefits of skill-scoped hooks:

  • ~50% fewer invocations: Only fire when that skill is active
  • Isolation: Hooks don't interfere across different skills
  • Self-contained: All behavior (instructions + hooks) in one file
  • Auto-cleanup: Hooks removed when skill completes

SpecWeave Hybrid Hook Architecture

SpecWeave uses both global and skill-scoped hooks for optimal performance:

┌─────────────────────────────────────────────────────────────────────┐
│ HYBRID HOOK ARCHITECTURE │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ GLOBAL HOOKS (hooks.json) │
│ ───────────────────────── │
│ Cross-cutting concerns that always run: │
│ • SessionStart → Initialize logging, load memory │
│ • UserPromptSubmit → Inject context, detect patterns │
│ • PostToolUse (increments) → Update living docs │
│ • Stop → Reflect learnings, check auto mode │
│ │
│ SKILL-SCOPED HOOKS (command frontmatter) │
│ ───────────────────────────────────────── │
│ Command-specific logic that fires only when skill active: │
│ • /sw:do → Task-AC sync guard (PostToolUse on Edit/Write) │
│ • /sw:done → Completion guard (Stop) │
│ • /sw:validate → Spec validation guard (Stop) │
│ • /sw:increment → Duplicate guard (PostToolUse) │
│ │
│ ~50% fewer hook invocations vs all-global approach │
│ │
└─────────────────────────────────────────────────────────────────────┘

SpecWeave Hook Examples

HookLocationEventPurpose
Session initGlobalSessionStartLoad memory, initialize state
Living docs syncGlobalPostToolUseAuto-update specs on increment changes
Task-AC syncSkill-scopedPostToolUseSync ACs when task completed (only during /sw:do)
Quality gatesSkill-scopedStopValidate before increment closes (only during /sw:done)
Reflect learningGlobalStopExtract learnings from session
Auto mode checkGlobalStopContinue autonomous execution

🔌 MCP Servers: External Integrations

MCP (Model Context Protocol) servers connect Claude to external tools.

MCP vs CLI

SpecWeave prefers CLI over MCP when available:

ApproachProsCons
CLI (preferred)Direct, full features, latest APIsRequires auth setup
MCPStructured interfaceExtra layer, limited APIs
# CLI (preferred)
gh issue create --title "Bug fix"
wrangler deploy
supabase db push

# MCP (when no CLI available)
mcp__custom_service__create_item

How Components Interact

Complete Flow Example

┌─────────────────────────────────────────────────────────────────────┐
│ SPECWEAVE WORKFLOW │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ 1. User: /sw:increment "Add user auth" │
│ │ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ COMMAND executes │ │
│ │ /sw:increment │ │
│ └─────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ SKILL activates │ │
│ │ increment-planner │ │
│ └─────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ AGENTS spawned │ │
│ │ PM → Architect → │ │
│ │ Tech Lead │ │
│ └─────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ HOOK fires │ │
│ │ (increment.created)│ │
│ │ → Creates GitHub │ │
│ │ issue │ │
│ └─────────────────────┘ │
│ │
│ Result: spec.md + plan.md + tasks.md + GitHub Issue #42 │
│ │
└─────────────────────────────────────────────────────────────────────┘

Quick Decision Guide

When to Use What

I want to...Use
Add domain expertiseSkill (auto-activates)
Run isolated complex taskAgent (spawns subprocess)
Create user actionCommand (/slash)
React to eventsHook (auto-fires)
Connect external toolMCP or CLI

Skills vs Agents

┌─────────────────────────────────────────────────────────────────────┐
│ │
│ SKILL AGENT │
│ ────── ───── │
│ • Auto-activates • Explicitly spawned │
│ • Main conversation • Isolated context │
│ • Provides guidance • Executes tasks │
│ • Lightweight • Can run in parallel │
│ │
│ Example: Example: │
│ "How do I optimize SQL?" "Analyze all 50 files │
│ → SQL skill guides you and write a report" │
│ → Agent works isolated │
│ │
└─────────────────────────────────────────────────────────────────────┘

Latest Optimizations (2026)

Recent Claude Code updates improve the architecture:

FeatureVersionBenefit
Skill hot-reloadv2.1.0Skills update instantly without restart
Context forkv2.1.0Skills can run in isolated sub-agent context
Skill-scoped hooksv2.1.0Hooks in frontmatter, scoped to skill lifecycle
Agent field in skillsv2.1.0Specify which model executes the skill
Agent type in SessionStartv2.1.2Agent-specific initialization
Merged skills/commandsv2.1.3Simpler mental model
Large output → file refv2.1.2Large outputs saved to disk, not truncated
Nested skill discoveryv2.1.6Auto-finds skills in subdirectories
Wildcard permissionsv2.1.0Bash(npm *) pattern matching

SpecWeave 2.1.x Optimizations

SpecWeave now leverages these Claude Code 2.1.x features:

OptimizationSkills AffectedBenefit
context: forkpm, architect, tech-lead, increment-plannerPrevents context pollution in main conversation
model: opuspm, architect, increment-plannerEnsures highest quality for critical decisions
Skill-scoped hooks/sw:do, /sw:done, /sw:validate, /sw:increment~50% fewer hook invocations (only fire when skill active)
Agent-type initSessionStart hookAgent-specific startup messages and context

SpecWeave Architecture

SpecWeave builds on Claude Code's architecture with hybrid hooks:

┌─────────────────────────────────────────────────────────────────────┐
│ SPECWEAVE │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ 24 PLUGINS │
│ ├── sw (core) - Increment lifecycle │
│ ├── sw-github - GitHub Issues sync │
│ ├── sw-jira - JIRA Epic/Story sync │
│ ├── sw-frontend - React/Vue/Next.js expertise │
│ ├── sw-backend - Node.js/Python APIs │
│ ├── sw-k8s - Kubernetes/GitOps │
│ ├── sw-ml - ML pipelines/MLOps │
│ └── ... 17 more │
│ │
│ 136 SKILLS (auto-activate based on keywords) │
│ 68 AGENTS (spawn for complex isolated tasks) │
│ 53 COMMANDS (user-invoked slash commands) │
│ │
│ HYBRID HOOKS (65+ total) │
│ ├── Global (hooks.json) - Cross-cutting: start, stop, prompt│
│ └── Skill-scoped (frontmatter) - Command-specific: ~50% fewer │
│ invocations vs all-global │
│ │
└─────────────────────────────────────────────────────────────────────┘

Learn More

SpecWeave Resources

Anthropic Documentation

AI Tool Alternatives

SpecWeave works with any AI coding assistant. Each has different integration depths:

ToolIntegrationHooksSkillsNotes
Claude CodeNativeYesYesFull SpecWeave support
CursorInstructionsNoPartialVia CLAUDE.md
WindsurfInstructionsNoPartialVia CLAUDE.md
GitHub CopilotInstructionsNoNoManual workflow
ClineMCPPartialNoVia MCP servers

Key Takeaway

Think of it like this:

  • Plugins = Packages (containers for everything)
  • Skills = Expertise (auto-loads when relevant)
  • Agents = Workers (spawn for isolated tasks)
  • Commands = Actions (user explicitly invokes)
  • Hooks = Reactions (auto-fire on events)
    • Global: Cross-cutting (always fire)
    • Skill-scoped: Command-specific (~50% fewer invocations)

SpecWeave bundles 24 plugins with 136 skills, 68 agents, 53 commands, and 65+ hybrid hooks - all working together to make AI-assisted development seamless.