Gemini CLI hooks: BeforeTool in settings.json, and why a hook is "not working"
- gemini-cli
- coding-agents
- guardrails
Yes. Gemini CLI has hooks. They live under the hooks key in settings.json. The event that fires before a tool runs is BeforeTool, not PreToolUse. It can deny the call with exit 2 or with "decision": "deny". This post is for people coming from Claude Code. I do not run Gemini CLI. Everything here comes from the docs and the v0.66.0 source, read on 31 October 2026.
The other events are AfterTool, BeforeAgent, AfterAgent, BeforeModel, AfterModel, BeforeToolSelection, SessionStart, SessionEnd, PreCompress and Notification.
{
"hooks": {
"BeforeTool": [
{
"matcher": "run_shell_command",
"hooks": [
{
"name": "block-force-push",
"type": "command",
"command": "$GEMINI_PROJECT_DIR/.gemini/hooks/block-force-push.sh",
"timeout": 5000
}
]
}
]
}
}
- Where.
.gemini/settings.jsonin the project,~/.gemini/settings.jsonfor you,/etc/gemini-cli/settings.jsonfor the machine. - Events. Eleven, with
BeforeToolas the tool gate. - Blocking. Exit 2 with the reason on stderr. Or exit 0 with
{"decision": "deny", "reason": "..."}. - Fail-open. A hook that times out or crashes is logged as a warning. The tool runs.
- How I know. Docs and code only. No session of my own.
One thing first. Per the repository README and a banner on geminicli.com, read on 31 October 2026, free-tier and Google One users moved from Gemini CLI to Antigravity CLI on 18 June 2026. Gemini CLI stays for Gemini Code Assist Standard and Enterprise and for paid API or Vertex keys. The README does not call it retired. This post covers Gemini CLI only. I did not look at Antigravity CLI hooks.
The Claude Code side, with the guardrail I run every day, is in my PreToolUse post. This is the Gemini CLI side.
Does Gemini CLI have hooks like PreToolUse in Claude Code?
Yes. Gemini CLI has eleven hook events. The hooks page defines hooks as "scripts or programs that Gemini CLI executes at specific points in the agentic loop". The match for PreToolUse is BeforeTool. The match for PostToolUse is AfterTool.
In the v0.66.0 source, packages/cli/src/commands/hooks/migrate.ts maps Claude Code names to Gemini names. Bash becomes run_shell_command. Edit becomes replace. The file defines gemini hooks migrate --from-claude. The docs do not mention it. I have not run it.
Where do I configure Gemini CLI hooks?
In settings.json, under hooks. Project settings in .gemini/settings.json win, then user, then system, then extensions. Hooks from all layers add up. An extension ships its hooks in hooks/hooks.json, see Gemini CLI extensions.
A group has a matcher and a hooks array. Each hook has type, today only "command", plus command, name and timeout. The timeout is in milliseconds, default 60000. Claude Code counts seconds. Copy "timeout": 10 across and you get ten milliseconds.
For tool events the matcher is a regex against the tool name, such as run_shell_command or write_file. MCP tools match as mcp_<server>_<tool>. Hooks see GEMINI_PROJECT_DIR, and CLAUDE_PROJECT_DIR as an alias "for compatibility". The master toggle is hooksConfig.enabled. Checkpointing has its own switch in the same file, general.checkpointing.enabled, see Gemini CLI checkpointing.
Can a Gemini CLI hook block a tool?
Yes, in two ways. Exit 0 and print {"decision": "deny", "reason": "..."}. The reason goes to the model as a tool error. Or print the reason to stderr and exit 2. Only "continue": false ends the loop.
Docs and code differ here. The docs say exit codes other than 0 and 2 are a warning and the call proceeds. In hookRunner.ts at v0.66.0, text output with exit 1 becomes allow plus a warning. Text with any other non-zero code becomes deny, with the text as the reason. So a script that writes to stderr and dies with exit 3 blocks the tool.
The quiet case is the one I watch for. A hook that times out, or cannot start, returns an error and no output. hookEventHandler.ts logs a warning. The tool runs.
Why is my Gemini CLI hook not working?
Five causes from the docs and the source. I have reproduced none.
Text on stdout. Anything but JSON on stdout breaks parsing. The CLI then defaults to allow and shows your text as a systemMessage. Log to stderr.
Untrusted folder. hookRegistry.ts skips project hooks in an untrusted folder. It writes one debug line, "Project hooks disabled because the folder is not trusted." User hooks still load.
Wrong matcher. Bash matches nothing. The tool is run_shell_command.
Disabled by name. The configuration reference lists hooksConfig.disabled. The best practices page writes hooks.disabled. I did not check which one the code reads.
Timeout too short. Milliseconds, not seconds.
Not a cause: an edited project hook. It warns, runs anyway and is then trusted.
What does a hook do in Gemini CLI YOLO mode?
A deny holds and an ask prompts. In scheduler.ts the BeforeTool hook runs before the policy check. A deny ends the call before YOLO sees it. If the hook returns "decision": "ask", the scheduler asks the user, whatever the policy said. The docs do not list ask as an output value. The type in types.ts accepts it. YOLO itself is in Gemini CLI YOLO mode.
Gemini CLI hooks vs Claude Code hooks
| Gemini CLI 0.66.0 | Claude Code | |
|---|---|---|
| Tool event | BeforeTool, AfterTool | PreToolUse, PostToolUse |
| Deny | Top-level decision: "deny", or exit 2 | hookSpecificOutput.permissionDecision: "deny", or exit 2 |
| Timeout | Milliseconds, default 60000 | Seconds, default 600 |
| Hook crashes | Warning, tool runs | Non-blocking error, unless onFailure: "block" |
The field names do not line up. Gemini CLI reads decision and reason at the top level. I found no permissionDecision in the hooks code, so a Claude Code deny script ports as a no-op. Codex, where the fields do match, is in Codex hooks. The opencode plugin route is in opencode hooks.
Frequently asked questions
Does Gemini CLI support hooks?
Yes. Eleven events under hooks in settings.json, documented on geminicli.com and in docs/hooks/ of the repository. The tool gate is BeforeTool.
Is there a PreToolUse hook in Gemini CLI?
Not by that name. BeforeTool fills the role. The source ships a migration table that maps PreToolUse to BeforeTool and Bash to run_shell_command.
Where are Gemini CLI hooks examples on GitHub?
In docs/hooks/writing-hooks.md of the google-gemini/gemini-cli repository: a logging hook, a secret scanner for write_file, a context injector and a tool filter.
What is the Gemini CLI Notification hook?
An observer. It fires on a ToolPermission alert. Per the reference it "cannot block alerts or grant permissions automatically".
Did you test this?
No. Gemini CLI is not installed on my machine. I read the hooks docs and the v0.66.0 source on 31 October 2026. Per the README, Gemini CLI now serves Code Assist Standard and Enterprise and paid API or Vertex users. Free and Google One users moved to Antigravity CLI, which I did not check.
The verdict
For the accounts that still use Gemini CLI, hooks are real enforcement for tool calls. Rename the event, rename the tool, move decision to the top level, write the timeout in milliseconds. Exit 2 is the wall. A crash is an open door. A canary command is the only proof.
Sources, read on 31 October 2026: the Gemini CLI hooks, hooks reference, writing hooks and best practices pages, and the configuration reference. The README of google-gemini/gemini-cli for who the tool now serves. Code from google-gemini/gemini-cli at tag v0.66.0: packages/core/src/hooks/hookRunner.ts, types.ts, hookRegistry.ts, hookEventHandler.ts, packages/core/src/scheduler/scheduler.ts and packages/cli/src/commands/hooks/migrate.ts. The Claude Code column comes from its hooks reference.