[FEATURE] PreCommand / PostCommand hook types for slash command lifecycle telemetry
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
There is no way to hook into the lifecycle of a specific slash command invocation. The existing PreToolUse / PostToolUse hooks fire on every tool call within a session — they are tool-scoped, not command-scoped. The Stop hook fires at session turn end, not at command start or completion.
This means operators who deploy custom slash commands have no native way to:
- Know when a specific command was invoked
- Know when it completed
- Capture command-scoped runtime context at completion time
For observability, adoption tracking, and cost attribution at the command level — all legitimate operator needs — there is currently no supported path.
Proposed Solution
Add two new hook event types to the hooks configuration:
"hooks": {
"PreCommand": [
{
"matcher": "my-command",
"hooks": [{ "type": "command", "command": "my-telemetry-script invoked" }]
}
],
"PostCommand": [
{
"matcher": "my-command",
"hooks": [{ "type": "command", "command": "my-telemetry-script completed" }]
}
]
}
PreCommand — fires when a slash command is invoked, before execution begins.
PostCommand — fires when a slash command finishes, after the final response.
Alternative Solutions
_No response_
Priority
Medium - Would be very helpful
Feature Category
CLI commands and flags
Use Case Example
An operator deploys a custom slash command (e.g. an automated code review tool). The platform team wants to track invocation counts, completion rates, and per-run metrics across their developer base — by user, by repo, by week.
Without command lifecycle hooks, none of this is attributable to a specific command. Session-level OTEL metrics (claude_code.token.usage etc.) provide aggregate session data but cannot be cleanly scoped to a single command invocation within that session.
Additional Context
To achieve command-scoped telemetry today, operators are forced to add explicit wiring inside every command's .md file and build workarounds that piggyback on existing hook types not designed for this purpose. These workarounds introduce overhead on every tool call in the session — not just during command execution — and require per-command boilerplate that scales poorly as the number of deployed commands grows.
PreCommand/PostCommand hooks would eliminate this entirely. The invocation event becomes zero-wiring. The completion event would still require one line per command to write command-specific runtime context (metrics only knowable during execution) — but that is an intrinsic constraint, not a framework gap.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗