Skip to content
Unpaid tier and Google One users: Gemini CLI was replaced by Antigravity CLI on June 18th, 2026. To learn more, see our blog post.

Gemini CLI hooks

Hooks are scripts or programs that Gemini CLI executes at specific points in the agentic loop, allowing you to intercept and customize behavior without modifying the CLI’s source code.

Hooks run synchronously as part of the agent loop—when a hook event fires, Gemini CLI waits for all matching hooks to complete before continuing.

With hooks, you can:

  • Add context: Inject relevant information (like git history) before the model processes a request.
  • Validate actions: Review tool arguments and block potentially dangerous operations.
  • Enforce policies: Implement security scanners and compliance checks.
  • Log interactions: Track tool usage and model responses for auditing.
  • Optimize behavior: Dynamically filter available tools or adjust model parameters.
  • Writing hooks guide: A tutorial on creating your first hook with comprehensive examples.
  • Best practices: Guidelines on security, performance, and debugging.
  • Hooks reference: The definitive technical specification of I/O schemas and exit codes.

Hooks are triggered by specific events in Gemini CLI’s lifecycle.

EventWhen It FiresImpactCommon Use Cases
SessionStartWhen a session begins (startup, resume, clear)Inject ContextInitialize resources, load context
SessionEndWhen a session ends (exit, clear)AdvisoryClean up, save state
BeforeAgentAfter user submits prompt, before planningBlock Turn / ContextAdd context, validate prompts, block turns
AfterAgentWhen agent loop endsRetry / HaltReview output, force retry or halt execution
BeforeModelBefore sending request to LLMBlock Turn / MockModify prompts, swap models, mock responses
AfterModelAfter receiving LLM responseBlock Turn / RedactFilter/redact responses, log interactions
BeforeToolSelectionBefore LLM selects toolsFilter ToolsFilter available tools, optimize selection
BeforeToolBefore a tool executesBlock Tool / RewriteValidate arguments, block dangerous ops
AfterToolAfter a tool executesBlock Result / ContextProcess results, run tests, hide results
PreCompressBefore context compressionAdvisorySave state, notify user
NotificationWhen a system notification occursAdvisoryForward to desktop alerts, logging

Understanding these core principles is essential for building robust hooks.

Strict JSON requirements (The “Golden Rule”)

Section titled “Strict JSON requirements (The “Golden Rule”)”

Hooks communicate via stdin (Input) and stdout (Output).

  1. Silence is Mandatory: Your script must not print any plain text to stdout other than the final JSON object. Even a single echo or print call before the JSON will break parsing.
  2. Pollution = Failure: If stdout contains non-JSON text, parsing will fail. The CLI will default to “Allow” and treat the entire output as a systemMessage.
  3. Debug via Stderr: Use stderr for all logging and debugging (for example, echo "debug" >&2). Gemini CLI captures stderr but never attempts to parse it as JSON.

Gemini CLI uses exit codes to determine the high-level outcome of a hook execution:

Exit CodeLabelBehavioral Impact
0SuccessThe stdout is parsed as JSON. Preferred code for all logic, including intentional blocks (for example, {"decision": "deny"}).
2System BlockCritical Block. The target action (tool, turn, or stop) is aborted. stderr is used as the rejection reason. High severity; used for security stops or script failures.
Other