Context: Working on claude-code-source-code-full (backup)
Claude Code Hook Event Telemetry
How hook telemetry works in the CLI
- Hook lifecycle events are emitted as
SDKSystemMessageentries insession.jsonlwith three subtypes:hook_started,hook_progress,hook_response - Gated by
setAllHookEventsEnabled()insrc/utils/hooks/hookEvents.ts. Without it, onlySessionStartandSetuphook events are emitted. - Enabled via
--include-hook-eventsCLI flag (requires--output-format=stream-json) or automatically whenCLAUDE_CODE_REMOTE=1
Python SDK consumer access
ClaudeAgentOptionsinclaude-agent-sdk-pythonhas no first-classinclude_hook_eventsfield- Workaround: use
extra_args={"include-hook-events": None}to pass the CLI flag - The Python SDK’s primary hook mechanism is different: register
HookCallbackPython functions via thehooksfield onClaudeAgentOptions, which uses a bidirectional control protocol (SDKControlInitializeRequest/SDKHookCallbackRequest) rather than passively receiving stream events - Hook callbacks are defined per-event via
HookMatcherwith typedHookInput/HookJSONOutput
Key source files
src/utils/hooks.ts- main hook execution and telemetry (logEvent,startHookSpan)src/utils/hooks/hookEvents.ts- event emission gating and lifecycle emitterssrc/utils/telemetry/sessionTracing.ts- OpenTelemetry span creation (startHookSpan/endHookSpan)src/entrypoints/sdk/coreSchemas.ts- SDK message schemas for hook eventssrc/cli/print.ts:628-registerHookEventHandlerconverts internal events to SDK stream messages- Python SDK:
src/claude_agent_sdk/types.py(ClaudeAgentOptions,HookMatcher,HookCallback) - Python SDK:
src/claude_agent_sdk/_internal/transport/subprocess_cli.py(_build_commandmaps options to CLI args)