Feature request: OnError hook for API failures

Resolved 💬 3 comments Opened Jan 27, 2026 by Harrison97 Closed Jan 27, 2026

Summary

Add an OnError hook that fires when Claude Code encounters API errors, allowing automated recovery in autonomous agent workflows.

Problem

When Claude Code hits an API error (e.g., 400 "tool use concurrency issues"), the session stops mid-execution and waits for user input. No existing hook fires:

  • PostToolUse doesn't fire (tool didn't complete)
  • Stop doesn't fire (conversation didn't end)
  • UserPromptSubmit doesn't fire (user hasn't typed)

This leaves autonomous agents stuck indefinitely until a human notices.

Use Case

In multi-agent orchestration systems (like Gas Town), polecats (worker agents) run autonomously. When they hit API errors, they need to auto-recover rather than wait for human intervention that may never come.

Current workaround: External watchdog script that polls tmux sessions every 5 minutes looking for error text. This is fragile and adds latency.

Proposed Solution

Add an OnError hook type:

{
  "hooks": {
    "OnError": [
      {
        "matcher": "API Error*",
        "hooks": [
          {
            "type": "command", 
            "command": "my-error-handler.sh"
          }
        ]
      }
    ]
  }
}

The hook could receive context like:

  • Error type/code
  • Error message
  • What tool was being called
  • Conversation state

Alternatives Considered

  1. External watchdog - Currently using this. Polls tmux sessions for error text. Works but adds 5+ minute latency and is fragile.
  1. Stop hook - Doesn't fire on errors, only clean exits.
  1. Retry logic in Claude itself - Would help but hooks allow custom recovery logic per deployment.

Additional Context

Error encountered in the wild:

API Error: 400 due to tool use concurrency issues. Run /rewind to recover the conversation.

The agent had successfully made code changes but couldn't commit them because the error occurred mid-tool-call.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗