Gemini CLI Hooks: Block, Rewrite and Report Tool Calls
Gemini CLI hooks are commands that Gemini CLI runs at set points in the agent loop, configured under a hooks key in settings.json. A BeforeTool hook can block a tool call or rewrite its arguments, an AfterTool hook can append text to the tool result, and an AfterAgent hook can reject a finished answer and make the agent try again. Hooks are on by default from Gemini CLI v0.26.0. This guide walks through the events, the matcher, the settings layers and the trust model for project hooks, then shows how to use them when Gemini CLI shares a repository with other agents.
One thing first. Gemini CLI's documentation now carries a banner: "Unpaid tier and Google One users: Gemini CLI was replaced by Antigravity CLI on June 18th, 2026." Antigravity CLI has its own, different hook format. Everything below is about Gemini CLI, which paid tiers and existing installs still run. Facts come from Google's pages, read on 27 Nov 2026 and listed at the end.
Where Gemini CLI hooks are configured
Hooks live in settings.json, and several layers merge, highest precedence first:
| Layer | File |
|---|---|
| Project | .gemini/settings.json in the current directory |
| User | ~/.gemini/settings.json |
| System | /etc/gemini-cli/settings.json |
| Extensions | Hooks defined by installed extensions |
A hook entry has a matcher and a list of commands:
{
"hooks": {
"BeforeTool": [
{
"matcher": "write_file|replace",
"hooks": [
{ "name": "claims", "type": "command", "command": "./scripts/check-claims.sh", "timeout": 5000 }
]
}
]
}
}
Note the unit: Gemini CLI's timeout is in milliseconds, default 60000. Cursor and Copilot use seconds. Copying a value between agents without converting gives you either a 5-millisecond hook or a 5,000-second one.
The events
The docs list eleven: SessionStart, SessionEnd, BeforeAgent, AfterAgent, BeforeModel, AfterModel, BeforeToolSelection, BeforeTool, AfterTool, PreCompress and Notification. Four matter most when agents share work.
BeforeTool: block or rewrite. Input has tool_name and tool_input. Return decision: "deny" with a reason, which is required when denying and "goes to the agent as a tool error". Return hookSpecificOutput.tool_input to merge new arguments over the model's before the tool runs.
AfterTool: add to the result. hookSpecificOutput.additionalContext is "appended to the tool result for the agent". A deny here hides the real output and replaces it with the reason.
AfterAgent: make it try again. Input includes prompt, prompt_response and stop_hook_active. A deny "rejects the response and forces a retry", and the reason is "sent to the agent as a new prompt to request a correction". continue: false stops the session without a retry.
BeforeAgent: a prompt was submitted. Useful for recording what the person asked.
Matchers
For tool events, matchers are regular expressions on the tool name; for lifecycle events they are exact strings; "*" or an empty string matches everything. The file-writing tools are write_file and replace, so write_file|replace catches edits. The shell tool is run_shell_command. MCP tools are named mcp_<server_name>_<tool_name>.
Exit codes
| Code | Meaning |
|---|---|
| 0 | Success; stdout is parsed as JSON. Preferred, including for deliberate blocks |
| 2 | System block; the action is aborted and stderr is the reason |
| Other | A non-fatal warning; the action proceeds with its original arguments |
Hooks run synchronously: keep them fast
Gemini CLI waits for every matching hook to finish before going on. A hook that makes a network call on every tool use adds that call's latency to every tool use. Google's announcement post recommends that you "use parallel operations and caching for expensive tasks" and use the matcher to limit hooks to the tools they care about. In practice:
- Match
write_file|replacefor a file guard, not*. - Keep a short local cache of claims instead of asking a server on every call.
- Set a timeout you would accept the agent waiting for, and decide what the hook does when its server is down.
Project hooks and trust
A project hook is a command that runs on your machine because someone committed it, so Gemini CLI is careful with them. It fingerprints project hooks, and when a hook's name or command changes (for example after a git pull) it treats it as "a new, untrusted hook" and warns you before it runs. Google's post adds: "always review the source of project-level hooks before enabling them."
What this means for a hook your team commits:
- Each teammate is asked once, and again whenever the hook line changes. Keep the hook line stable and change the script it calls instead, or expect a round of warnings after every edit.
- An attacker who can change
.gemini/settings.jsonin a pull request cannot run a new command silently; the person is warned first. They can still change the script an existing, trusted line calls, so review those files as you would CI configuration.
Use case: refuse writes to another agent's files
When Gemini CLI works next to Claude Code or Codex, the most useful hook is a BeforeTool guard on write_file|replace. It reads tool_input.file_path, checks whether another agent holds that path, and if so answers:
{ "decision": "deny", "reason": "src/billing/invoice.ts is held by codex-2 until 14:30. Work on the tests instead, or message codex-2." }
Because the reason reaches the agent as a tool error, Gemini reads it and can choose other work. See file locking for AI agents for why claims should expire rather than lock forever.
Use case: report changed files to a shared place
An AfterTool hook on the same matcher can post each changed path to whatever your team watches, so a person, or another agent, can see that Gemini touched package.json before two branches collide. Report the path, not the content, and send it in the background if the call is slow.
Wiring Gemini CLI into Forkit
Forkit's hook script supports Gemini CLI as of 26 Nov 2026:
- It detects Gemini CLI by its
GEMINI_*environment variables, converts the payload to one internal shape, and answers in Gemini's words (decision: deny). npx forkit-cli initwrites Forkit's hooks into~/.gemini/settings.jsonwhen it finds thegeminicommand, forSessionStart,BeforeAgent,BeforeTool(matched onwrite_file|replace),AfterTool,AfterAgentandSessionEnd, after a backup.- For a repository,
npx forkit-cli init --sandbox --project --agents geminiwrites.gemini/settings.jsonto commit. The key arrives at run time inFORKIT_API_KEY. - An edit under another agent's exclusive claim is refused, naming the holder. Changed files are reported to the session's thread, so they show up next to the work of the Claude Code and Codex agents in the same workspace.
Gemini CLI has no permission-request hook (its Notification event of type ToolPermission only observes), so Forkit cannot hold a Gemini approval prompt and answer it remotely. Forkit's command rules and grouped approvals therefore do not apply to Gemini CLI; what Forkit's hook does on BeforeTool is refuse an edit under another agent's file claim. A command you want blocked in Gemini has to be blocked by your own BeforeTool hook. For how permission rules work where the agent does support them, see agent permission rules and the Claude Code PermissionRequest hook.
What is not covered
- Not yet run against a real Gemini CLI. The support was built from Gemini CLI's documentation and tested with its documented payloads, not yet against the real agent.
- Antigravity CLI is not covered by hooks. Its hooks live in a separate
hooks.jsonwith different event names; Forkit does not write that file yet. Run it underforkit run -- agy …, which watches the checkout instead (see coordinating agents that have no hooks).
Checklist
- Confirm you are on Gemini CLI v0.26.0 or later (hooks on by default).
- Put team hooks in
.gemini/settings.json, personal ones in~/.gemini/settings.json. - Use millisecond timeouts.
- Narrow matchers to the tools you guard.
- Keep committed hook lines stable so teammates are not re-prompted constantly.
- Give every deny a reason that tells the agent what to do next.
- Check whether your users have moved to Antigravity CLI.
Forkit has a Free plan; Pro is $20 a month and Team $40 per seat a month, billed per person, never per agent. More at forkit-mcp.com.
Sources
Read on 27 Nov 2026:
- Gemini CLI, Hooks: https://geminicli.com/docs/hooks/
- Gemini CLI, Hooks reference: https://geminicli.com/docs/hooks/reference/
- Google for Developers Blog, "Tailor Gemini CLI to your workflow with hooks", 28 Jan 2026: https://developers.googleblog.com/tailor-gemini-cli-to-your-workflow-with-hooks/