Skip to content

Claude Code SDK hooks do not work #193

@smoorjani

Description

@smoorjani

Hello,

I am trying to use hooks from the claude code sdk from the docs here, but am not seeing hooks getting called. I have a simple repro here:

from claude_agent_sdk import query, ClaudeAgentOptions, HookMatcher, HookContext
from typing import Any
import asyncio

# Track hook calls
hook_calls = []

async def log_hook(
    input_data: dict[str, Any],
    tool_use_id: str | None,
    context: HookContext
) -> dict[str, Any]:
    """Log all tool usage for auditing."""
    print(f"HOOK CALLED: {input_data}")
    return {}

async def main():
    options = ClaudeAgentOptions(
        hooks={
            'PreToolUse': [
                HookMatcher(hooks=[log_hook])
            ],
            'Stop': [
                HookMatcher(hooks=[log_hook])
            ]
        },
        max_turns=3  # Limit turns to speed up test
    )

    message_count = 0
    async for message in query(
        prompt="List all files in ths directory.",
        options=options
    ):
        message_count += 1
        print(f"Message {message_count}: {type(message).__name__}")

    print(f"\nTotal hook calls: {len(hook_calls)}")
    print(f"Hook calls: {hook_calls}")

    if not hook_calls:
        print("\nERROR: No hooks were called!")
    else:
        print("\nSUCCESS: Hooks were called!")

if __name__ == "__main__":
    asyncio.run(main())

I'm on the latest version of the SDK claude-agent-sdk==0.1.0. Am I missing something obvious? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions