Gemini CLI Hooks Skill
Gemini CLI Hooks allow you to customize the behavior of the CLI at specific points in its lifecycle. Hooks can be used to set up environments, perform cleanup, log activity, or intercept tool calls.
Hooks are an experimental feature and must be explicitly enabled in your settings.json:
{
"experimental": {
"enableAgents": true
},
"hooks": {
"enabled": true
},
"tools": {
"enableHooks": true
}
}
| Event Name | Description |
|---|---|
SessionStart |
Triggered when the CLI session begins. |
SessionEnd |
Triggered when the CLI session ends. |
BeforeAgent |
After user prompt submission, before planning. |
AfterAgent |
After the agent loop completes. |
BeforeModel |
Before sending a request to the LLM. |
AfterModel |
After receiving a response from the LLM. |
BeforeTool |
Before a tool (e.g., run_shell_command) executes. |
AfterTool |
After a tool has executed. |
Notification |
Triggered for system notifications (e.g., tool permissions). |
Hooks are defined in the hooks array within your settings.json (User or Project scope).
{
"hooks": {
"enabled": true,
"hooks": [
{
"name": "startup-script",
"type": "command",
"command": "~/.gemini/hooks/session-start.sh",
"description": "Run custom logic on session start"
}
]
}
}
Use a matcher to target specific tools or events:
{
"name": "log-shell-commands",
"type": "command",
"command": "./scripts/log-tool.sh",
"matcher": "run_shell_command"
}
Hooks communicate using standard input/output:
stdin containing event details.stdout/stderr for logging.You can manage hooks directly from the Gemini prompt:
/hooks list: Show all configured hooks and their status./hooks enable-all: Enable all hooks./hooks disable-all: Disable all hooks./hooks enable <name>: Enable a specific hook./hooks disable <name>: Disable a specific hook.SessionStart for repo-specific envs) in
.gemini/settings.json.0 unless you intend to stop the
session/tool.This skill provides the foundation for automating your Gemini CLI environment using lifecycle hooks.