GC-101 · Module 4
Hooks & Lifecycle Events
3 min read
Hooks let you intercept Gemini CLI behavior at specific lifecycle events. Think of them as middleware for the AI agent — you can run custom logic before or after specific actions. This enables patterns like auto-formatting code after every write, running linters after edits, logging all commands for audit trails, or triggering notifications when tasks complete.
# List all configured hooks
/hooks list
# Enable a specific hook
/hooks enable post-write-lint
# Disable a hook
/hooks disable post-write-lint
Hooks are defined in your settings and fire at lifecycle points: session start, before tool execution, after tool execution, session end, and others. The hook configuration specifies which event to intercept and what action to take. Actions can be shell commands, which makes hooks extremely flexible — anything you can script, you can hook.
Do This
- Use hooks for consistent enforcement — formatting, linting, testing
- Start with a few high-value hooks and add more as needed
- Log hook failures so you know when enforcement breaks down
Avoid This
- Add so many hooks that every operation takes 10 seconds
- Use hooks for complex logic that belongs in proper scripts
- Ignore hook failures — they indicate your standards aren't being met