Tutorial¶
Learn fasthooks step by step.
Overview¶
This tutorial covers:
- Events - Understanding hook events and typed tool events
- Responses - Using
allow(),deny(), andblock() - Dependencies - Inject State and Transcript into handlers
- Transcript - Context engineering and memory editing
- Background Tasks - Spawn async work that feeds back later
- Blueprints - Organize handlers into reusable modules
- Middleware - Cross-cutting concerns (timing, logging)
- Testing - Writing tests with
MockEventandTestClient
Core Concepts¶
HookApp¶
The main class that registers and runs your handlers:
from fasthooks import HookApp
app = HookApp()
@app.pre_tool("Bash")
def my_handler(event):
...
if __name__ == "__main__":
app.run()
Decorators¶
Register handlers for different hook events:
| Decorator | When it runs |
|---|---|
@app.pre_tool("Bash") |
Before a tool executes |
@app.post_tool("Write") |
After a tool executes |
@app.on_stop() |
When Claude stops |
@app.on_session_start() |
When a session starts |
Typed Events¶
Each tool has typed event with autocomplete:
@app.pre_tool("Bash")
def check(event):
print(event.command) # str
print(event.description) # str | None
print(event.timeout) # int | None
Guards¶
Filter which events trigger your handler:
@app.pre_tool("Bash", when=lambda e: "sudo" in e.command)
def check_sudo(event):
return deny("No sudo allowed")
Advanced Topics¶
- Dependencies - Access conversation history and persistent state
- Background Tasks - Async work with Claude sub-agents
- Blueprints - Split handlers into reusable modules
- Middleware - Add timing, logging, error handling to all handlers