Overview
Hooks allow you to execute scripts at specific points in the GemKit session lifecycle. They're defined in .gemini/hooks/ and configured in settings.json.
Hook Lifecycle Stages
GemKit provides four lifecycle stages:
| Stage | When It Runs | Use Case |
|---|---|---|
SessionStart | When a new session begins | Load context, initialize resources |
BeforeAgent | Before an agent processes | Inject rules, add context |
BeforeModel | Before sending to AI model | Capture state, validate |
SessionEnd | When session completes | Notifications, cleanup, metrics |
Hook Files Location
Hooks are stored in .gemini/hooks/:
markdown
Configuration in settings.json
Hooks are configured in .gemini/settings.json:
json
Hook Object Properties
| Property | Type | Description |
|---|---|---|
command | string | Command to execute |
timeout | number | Timeout in milliseconds |
matcher | string | Pattern to match (* for all) |
Available Hook Scripts
gk-session-init.cjs
Runs at session start and before agent/model calls. Use for:
- Loading project context
- Setting environment variables
- Capturing project state
gk-session-end.cjs
Runs when a session completes. Use for:
- Sending notifications (Discord, Slack)
- Updating metrics
- Cleanup tasks
gk-dev-rules-reminder.cjs
Runs before agent processing. Use for:
- Injecting coding standards
- Adding project conventions
- Enforcing development rules
gk-scout-block.cjs
Security guardrails. Use for:
- Blocking sensitive file access
- Preventing dangerous commands
- Enforcing security policies
Hook Examples
Example 1: Session Init Hook
javascript
Example 2: Session End Hook with Discord
javascript
Example 3: Dev Rules Reminder
javascript
Example 4: Scout Block for Security
javascript
Enabling Hooks
Hooks must be enabled in settings.json:
json
Hook Subdirectories
lib/
Shared utilities for hooks:
javascript
notifications/
Notification handlers:
javascript
gk-scout-block/
Additional scout block configurations.
Best Practices
- Keep hooks fast - Use appropriate timeouts
- Handle errors gracefully - Wrap in try-catch
- Use the lib/ folder - Share common utilities
- Log hook activity - Add logging for debugging
- Test hooks locally - Verify before relying on them
Debugging Hooks
Enable verbose mode to see hook execution:
bash
Related Documentation
- Configuration Reference - Full settings.json reference
gk init- Initialize GemKit with hooks