Skip to content

Error Recovery & Resilience

Learn to handle failures gracefully, recover from errors, and build resilient workflows with Claude Code. This guide covers everything from simple retries to complex recovery strategies, ensuring your AI-assisted development stays productive even when things go wrong.

Common Error Categories

Claude Code errors typically fall into these categories:

  1. Permission Errors - Tool execution blocked
  2. Context Errors - Token limits or memory issues
  3. Execution Errors - Command failures or syntax issues
  4. Connection Errors - Network or MCP server issues
  5. Model Errors - AI confusion or incorrect outputs

Claude provides clear visual feedback when errors occur:

  1. Red error messages - Direct command failures
  2. Yellow warnings - Non-critical issues
  3. Tool execution failures - Clear error output
  4. Retry suggestions - Claude often suggests fixes

By default, Claude asks permission for every potentially risky operation:

Terminal window
# This gets interrupted constantly:
claude "Refactor the authentication system"
# → "Can I edit auth/login.js?" [y/n]
# → "Can I run git add?" [y/n]
# → "Can I edit auth/session.js?" [y/n]
Section titled “Solution 1: Skip Permissions (Recommended for Development)”

YOLO Mode

Terminal window
# Skip all permission prompts for uninterrupted workflow
claude --dangerously-skip-permissions

This mode is safe for:

  • Development environments
  • Containerized workspaces
  • Projects with good backups
  1. Use /permissions command to manage allowed tools
  2. Add specific tools to allowlist: Edit, View, Bash(git:*)
  3. Save preferences in .claude/settings.json:
{
"allowedTools": [
"Edit",
"View",
"Bash(git:*)",
"Bash(npm:*)",
"mcp__git__*"
]
}
Terminal window
# Allow specific tools for this session only
claude --allowedTools "Edit,View,Bash(git:*)"

When commands fail, Claude typically:

  1. Shows the error - Full stderr output
  2. Analyzes the cause - Identifies what went wrong
  3. Suggests fixes - Proposes solutions
  4. Retries automatically - If you approve
Terminal window
# Undo the last change
/undo
# Or ask Claude to revert
"Please undo the last file edit"
# Git-based recovery
"git checkout -- filename.js"

When hitting context limits:

  1. Clear unnecessary context: /clear
  2. Start a new session with focused context
  3. Export important state to CLAUDE.md
  4. Use modular memory files

Context Management Pattern

# In CLAUDE.md
## Current Task State
- Completed: Authentication refactor
- In Progress: Session management
- Next: Testing implementation
## Key Decisions
- Using JWT for sessions
- Redis for session storage
- 24-hour expiration
Terminal window
# Before context gets too large
claude "Save our current progress to CLAUDE.md"
# Then clear and continue
/clear
claude "Continue from CLAUDE.md progress"
Terminal window
# Check MCP server status
claude mcp list
# Restart specific server
# (Restart the application providing the server)
# Debug MCP issues
claude --mcp-debug

Signs of model confusion:

  • Repeating the same errors
  • Misunderstanding requirements
  • Going in circles
  • Suggesting incorrect solutions
  1. Clear and restart with better context
  2. Break down the problem into smaller parts
  3. Provide explicit examples of desired outcome
  4. Switch models if available
  5. Use structured prompts with clear steps

Confusion Recovery Template

Let's restart. Here's what we need:
1. Current state: [describe what exists]
2. Desired outcome: [describe what you want]
3. Constraints: [any limitations]
4. Example: [provide a concrete example]
Please approach this step-by-step.

Create recovery checkpoints during complex operations:

Terminal window
# Before risky changes
git add -A && git commit -m "Checkpoint: before auth refactor"
# Or use Claude
claude "Create a git checkpoint before we proceed"

Implement error-aware workflows:

Terminal window
# Tell Claude to be defensive
claude "Implement the API client with comprehensive error handling"
# Claude will add try-catch blocks, validation, and fallbacks

Create recovery commands in .claude/commands/:

.claude/commands/recover.md
When recovering from errors:
1. Run git status to check changes
2. Run tests to verify functionality
3. Check logs for recent errors
4. Restore from last known good state if needed
Execute: $ARGUMENTS

NPM Permission Errors:

Terminal window
# Fix npm prefix
npm config set prefix ~/.npm-global
export PATH=$PATH:~/.npm-global/bin
# Or migrate to local install
claude migrate-installer

Missing Commands:

Terminal window
# Verify installation
which claude
claude doctor
# Reinstall if needed
npm install -g @anthropic-ai/claude-code

Resilient Development Flow

  1. Always work in branches - Easy rollback
  2. Commit frequently - More recovery points
  3. Test incrementally - Catch errors early
  4. Use defensive prompts - Ask for error handling
  5. Maintain backups - External to git

Configure hooks for automatic recovery:

{
"hooks": {
"postToolUse": [
{
"match": "Error",
"command": "git status && npm test"
}
]
}
}

Document common errors in CLAUDE.md:

## Common Errors and Fixes
### Module Not Found
- Cause: Missing npm install
- Fix: Run `npm install` or check package.json
### Type Errors
- Cause: TypeScript strict mode
- Fix: Add proper type annotations
### Permission Denied
- Cause: File permissions
- Fix: Check ownership with `ls -la`

When everything goes wrong:

  1. Stop Claude: Press Esc or Ctrl+C
  2. Assess damage: git status and git diff
  3. Revert if needed: git checkout .
  4. Check backups: Time Machine, snapshots, etc.
  5. Start fresh: New terminal, new session
  6. Report issues: If Claude bug, report to Anthropic

Proactive Error Prevention

  • Use containers for isolated environments
  • Set up CI/CD to catch errors early
  • Configure linters that Claude respects
  • Document gotchas in CLAUDE.md
  • Test in staging before production
  • Use version control religiously

You’ve completed the Claude Code quick-start guide! Here are your next steps:

Remember: errors are learning opportunities. Each failure teaches Claude (and you) how to handle similar situations better in the future. Embrace the iterative process and build increasingly resilient workflows.