Feature Request: PostToolUseFailed hook for bash command failures
Status Closed — not planned
Maintainer reply None cached
Workaround ✓ Mentioned in thread ↓
Activity 8 comments · opened Jan 4, 2026 · closed Mar 1, 2026
Summary
Add a PostToolUseFailed hook (or extend PostToolUse with failure context) that triggers when a bash command exits with a non-zero exit code.
Use Case
I'm building a "learning flywheel" system where command failures should be captured as lessons learned to prevent repeating mistakes. Currently:
- Bash command fails (exit code != 0)
- I want to automatically prompt/remind to run a failure capture command
- Problem:
PostToolUseonly fires on successful completion, so failures go undetected by hooks
Current Limitation
From the docs, PostToolUse runs "after a tool completes successfully." There's no hook for when tools fail.
Proposed Solution
Option A: Add PostToolUseFailed event type
{
"hooks": {
"PostToolUseFailed": [
{
"matcher": { "tool_name": "Bash" },
"hooks": [{ "type": "command", "command": "./on-failure.sh" }]
}
]
}
}
Option B: Extend PostToolUse to always fire, with exit code in context
{
"tool_name": "Bash",
"exit_code": 1,
"succeeded": false,
"output": "..."
}
Impact
This would enable:
- Automated failure logging/learning systems
- CI-style failure notifications
- Quality enforcement workflows
- Better debugging context preservation
Environment
- Claude Code CLI
- macOS/Linux
8 Comments
(Consolidating from #16599, originally tracked in #6371)
This supports Option B above—extending
PostToolUseto always fire with failure context rather than adding a separate event.Proposed Payload for Failed Tools
Current behavior:
PostToolUseonly fires on success; failures skip the hook entirely.Requested behavior: Fire for all completions. Include
"success": truefor successful calls (preserving current behavior),"success": false+failure_detailsfor failed calls.Additional Use Cases
git push --set-upstream origin branch-name)Use case: Detecting repeated tool failures to automatically spawn a "batman" agent that fixes broken tools instead of Claude giving up and trying workarounds.
Current limitation: We attempted to build this with PostToolUse hooks but discovered they only fire on success. Without failure detection, we can't implement "learning from mistakes" workflows.
Preferred solution: Option B (extend PostToolUse with
success: booleanfield) - simpler API, backwards compatible.+1 for this feature!
Wider consideration: Claude Code must have escape hatches
Claude Code will always have bugs and these escape hatches like
~/.claude)are the only fix we have other than spamming Github Issues (since we can't fork Claude Code, can't use Claude Code subscriptions with open source competitors, and the engineering capacity of Anthropic isn't infinite either).
Just a thought - the prioritization of these emergency surfaces would make the dev experience much better @bcherny
This is not a Feature Request, it should be a Bug Report
A hook named PostToolUse semantically implies "after tool use completes" — not "after tool use succeeds." If the intent were success-only, you'd expect it named OnToolSuccess or PostToolUseSuccess, with a companion failure hook from the start.
The current behavior violates the principle of least surprise. Every standard use case for post-event hooks (logging, cleanup, analytics, error handling) requires firing on all completions. A "post" hook that only fires on success isn't really a post-hook — it's a pre-hook for "what to do next."
I ran into this limitation today. My use case is prompting the model to investigate errors before proceeding.
The earlier comments suggest Option B is backwards compatible:
I think there's a risk here. Existing
PostToolUsehooks were written assuming they only fire on success. If they suddenly start firing on failures too, that could cause problems.For example, a hook that logs successful commands to a file and then syncs that file to a remote server. If it starts firing on failures, it would sync incomplete logs or trigger unnecessary network requests.
A more concerning example would be a hook that auto-commits after successful test runs. If it suddenly fires on test failures, it could commit broken code.
Two approaches avoid this:
PostToolUseFailedhook) sidesteps the problem entirely. Existing hooks remain unchanged.PostToolUsewith afilterfield and a safe default would work:``
json
``{
"matcher": "Bash",
"trigger": "failure",
"hooks": [{ "command": "./on-failure.sh" }]
}
The trigger field would accept "success" (the default, preserving current behavior), "failure", or "both". Existing configurations without the field would continue working as they do today.
@anthropicbot Yes this issue is still occuring
Closing for now — inactive for too long. Please open a new issue if this is still relevant.
This discussion has been moved to https://github.com/anthropics/claude-code/issues/29799
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.