The complete guide to Claude Code setup (Opus 4.6, Sonnet 4.6, Haiku 4.5). 1M token context window. 100+ hours saved. 25 hook events. Agent teams and task management. Production-tested patterns for skills, hooks, and MCP integration.
This chapter documents advanced Claude Code configuration patterns discovered through a comprehensive audit of official documentation (Entry #363). These patterns go beyond basic setup to optimize context loading, agent intelligence, skill efficiency, and code quality enforcement.
Rules files support YAML frontmatter with paths: glob patterns for conditional loading. Instead of loading ALL rules for every operation, rules only load when you’re editing files that match their patterns.
# .claude/rules/database/patterns.md
---
paths:
- "src/database/**"
- "src/sync/**"
- "scripts/*sync*"
---
# (rule content follows)
| Rule Type | Use paths: |
Example |
|---|---|---|
| Domain-specific code rules | Yes | Database patterns, API integration rules |
| Deployment rules | Yes | Only when editing Dockerfile, *.yml |
| UI/encoding rules | Yes | Only when editing public/, prompts/ |
| Git safety rules | No | Always applies regardless of file |
| Project structure reference | No | Needed for all code work |
| MCP usage patterns | No | Always relevant |
Without paths:, a project with 13 rule files loads ALL of them on every message. With paths: on domain-specific rules, only relevant rules load — reducing per-message context by 20-40%.
| Pattern | Matches |
|---|---|
src/** |
All files under src/ recursively |
*.yml |
YAML files in root |
scripts/deploy* |
Files starting with “deploy” in scripts/ |
src/**/*.js |
All .js files under src/ |
public/** |
All frontend files |
Agents support memory: for persistent learning across sessions. Without memory, agents start fresh every time they’re spawned.
| Option | Scope | Persists Across | Best For |
|---|---|---|---|
memory: project |
This project only | All sessions in this project | Project-specific patterns |
memory: local |
This machine only | All sessions on this machine | Machine-specific config |
memory: user |
All projects | All sessions everywhere | Universal patterns |
| (none) | No persistence | Nothing | Stateless agents |
---
name: database-agent
description: "Database operations specialist"
memory: project
---
With memory enabled, agents learn across sessions:
# Project-specific agents → memory: project
database-agent: memory: project # Learns schema patterns
test-engineer: memory: project # Learns test conventions
deploy-agent: memory: project # Learns deploy patterns
# Cross-project agents → memory: user
architecture-agent: memory: user # Architecture is universal
security-agent: memory: user # Security rules apply everywhere
debug-specialist: memory: user # Debugging methodology is universal
The skills: field tells agents which skills to load immediately on spawn, rather than discovering them during operation.
---
name: database-agent
description: "Database operations specialist"
memory: project
skills:
- database-master-skill
- database-column-standards-skill
---
| Agent | Preload Skills | Rationale |
|---|---|---|
| database-agent | database-master, column-standards | Schema + naming patterns |
| deploy-agent | deployment-master, safe-deployment | Deploy checklist + safety |
| test-engineer | testing-master, baseline-methodology | Test patterns + metrics |
| accuracy-agent | parity-master, field-mapping | Data validation patterns |
| debug-specialist | troubleshooting-master, pipeline-debugging | Debug methodology |
Rule: Only preload skills directly relevant to the agent’s domain. Cross-domain skills waste context.
The permissionMode: field controls how agents handle permission prompts.
| Mode | Behavior | When to Use |
|---|---|---|
| (default) | Inherits parent session mode | Most agents |
plan |
Read-only — can explore but never write | Navigators, monitors, scouts |
acceptEdits |
Auto-accepts file edits | Code-writing agents |
bypassPermissions |
No permission prompts | Fully trusted automation only |
---
name: knowledge-navigator
description: "Search and navigate project knowledge"
permissionMode: plan
---
plan for any agent that should NEVER modify filesacceptEdits for agents that write code as their core functionbypassPermissions for agents that interact with external servicesTwo mechanisms inject dynamic content into skills: !command``for live system state and$ARGUMENTS for user parameters.
command## Database Health
!`docker exec my-postgres psql -U user -d mydb -c "SELECT current_database(), pg_size_pretty(pg_database_size(current_database()))" 2>/dev/null || echo "Not running"`
## Last Test Results
!`ls -t tests/results/*.json 2>/dev/null | head -1 | xargs cat 2>/dev/null | jq '{total, passed, failed}' 2>/dev/null || echo "No results"`
## Current Git State
!`git log --oneline -3 2>/dev/null || echo "Not a git repo"`
Rules:
2>/dev/null || echo "fallback" for graceful failureFor user-invocable skills:
---
name: deploy-skill
user-invocable: true
argument-hint: "[environment]"
---
Deploy to $ARGUMENTS:
1. Run tests for $ARGUMENTS environment
2. Build for $ARGUMENTS
3. Push to $ARGUMENTS
User types /deploy staging → $ARGUMENTS becomes “staging”.
Use $ARGUMENTS[0], $ARGUMENTS[1] for positional arguments.
Beyond shell script hooks (type: "command"), Claude Code supports LLM-powered hooks (type: "prompt") for intelligent validation.
{
"PreToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "prompt",
"prompt": "Check if this code follows project patterns: (1) Uses employee_id not id, (2) No hardcoded data, (3) UTF-8 encoding. If the file is NOT in src/, always allow. Output JSON: {\"decision\":\"allow\"} or {\"decision\":\"block\",\"reason\":\"...\"}"
}
]
}
]
}
| Aspect | Command Hook | Prompt Hook |
|---|---|---|
| Speed | Fast (ms) | Slower (seconds) |
| Cost | Free | Uses LLM tokens |
| Intelligence | Pattern matching only | Semantic understanding |
| Best for | Formatting, logging, simple checks | Code quality, pattern validation |
For non-critical background operations, add "async": true:
{
"PostToolUseFailure": [
{
"hooks": [
{
"type": "command",
"command": ".claude/hooks/tool-failure-logger.sh",
"async": true
}
]
}
]
}
Rule: Use async: true for logging and monitoring. Keep synchronous for validation and blocking.
Claude Code configuration exists at two levels with clear precedence rules.
| Scope | Location | Available To | Persists Across |
|---|---|---|---|
| Project | .claude/ (in repo) |
This project only | Git branches |
| Global | ~/.claude/ (home dir) |
ALL projects | All projects |
| Component | Project (.claude/) |
Global (~/.claude/) |
|---|---|---|
| Rules | Domain-specific patterns | Universal coding standards |
| Agents | Project-specific agents | Cross-project agents |
| Skills | Project-specific workflows | Universal workflows |
| Hooks | Project-specific validation | Session management |
| Memory | Project knowledge | Cross-project knowledge |
When both project and global have the same named file, project wins:
~/.claude/agents/security-agent.md ← Generic (for other projects)
.claude/agents/security-agent.md ← Project-specific (WINS in this project)
Ask: “Does this apply to ALL my projects, or just this one?”
~/.claude/)
.claude/)
Two fields optimize skill cost and safety.
---
name: blueprint-discovery-skill
model: haiku # Cheap + fast for simple lookups
allowed-tools: [Read, Grep, Glob] # Read-only safety
---
| Model | Use For | Typical Count |
|---|---|---|
haiku |
Navigation, reference, lookups | ~30 skills (40%) |
sonnet |
Validation, workflows, integration | ~30 skills (40%) |
opus |
Architecture, debugging, complex reasoning | ~10 skills (15%) |
| (default) | Inherits session model | Remaining skills |
allowed-tools: [Read, Grep, Glob] # Read-only skill
allowed-tools: [Read, Write, Edit, Bash] # Full access skill
Combine both for maximum efficiency:
---
name: sacred-commandments-skill
description: "Navigate Sacred Commandments..."
model: haiku
allowed-tools: [Read, Grep, Glob]
---
This creates a fast, cheap, read-only reference skill.
Content-level ask now overrides tool-level allow:
{
"permissions": {
"allow": ["Bash"],
"ask": ["Bash(rm *)"]
}
}
Result: All Bash commands auto-allowed EXCEPT rm * which prompts the user. Previously, allow: ["Bash"] would override all ask rules.
Precedence order: ask > acceptEdits > plan > default
After implementing these patterns, verify your setup:
# Path-specific rules
grep -rl "^paths:" .claude/rules/ | wc -l
# Agent features
grep -rl "^memory:" .claude/agents/*.md | wc -l
grep -rl "^skills:" .claude/agents/*.md | wc -l
grep -rl "^permissionMode:" .claude/agents/*.md | wc -l
# Skill features
grep -rl "^model:" .claude/skills/*/SKILL.md | wc -l
grep -rl "^allowed-tools:" .claude/skills/*/SKILL.md | wc -l
# Hook features
grep -c '"type": "prompt"' .claude/settings.json
grep -c '"async": true' .claude/settings.json
# Global agents
ls ~/.claude/agents/*.md 2>/dev/null | wc -l