Feature request: OnError hook for API failures
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:
PostToolUsedoesn't fire (tool didn't complete)Stopdoesn't fire (conversation didn't end)UserPromptSubmitdoesn'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
- External watchdog - Currently using this. Polls tmux sessions for error text. Works but adds 5+ minute latency and is fragile.
- Stop hook - Doesn't fire on errors, only clean exits.
- 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.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗