Hooks

Hooks run your own shell commands at fixed points in an agent's session: before and after each tool call, when you send a message, when a session starts and when the agent finishes. Use them to block commands your team never wants run, to add context the agent should always have, or to run a formatter or linter after every edit. They work the same in the terminal (codity agent) and in the VS Code extension.

Where hooks come from

Source Where it is set Who it applies to
Organization Settings → Policies → Agent policy → Hooks Every agent in the organization.
Team Teams → select the team → Settings → Agent policy → Hooks Agents following that team's policy.
You ~/.codity/hooks.json on your machine Agents you start on that machine.

All three apply together, in that order. A repository's files never add hooks, so cloning a project cannot make your machine run its commands.

Your own file is loaded only when it is a regular file you own and nobody else can write to, in a folder only you can write to (chmod 600 ~/.codity/hooks.json and chmod 700 ~/.codity). Otherwise it is skipped and the agent's log says why. Set CODITY_CONFIG_HOME to use a folder other than ~/.codity.

Events

Event Runs Takes a matcher Exit code 2 or a block decision
PreToolUse Before a tool call. Yes Refuses the tool call; the agent sees your reason.
PostToolUse After a tool call. Yes Adds your reason to the tool's result as feedback for the agent.
UserPromptSubmit When a message is sent, before the model sees it. No Refuses the message; it is not sent to the model.
SessionStart When a session starts. No Logged; the session still starts.
Stop When the agent finishes its reply. No Sends the agent back to work with your reason, at most 3 times in a row.

Helper agents that the main agent starts get PreToolUse and PostToolUse too. SessionStart and Stop run for the session you are talking to.

Configuration format

The format follows Claude Code's. In ~/.codity/hooks.json:

{
  "hooks": {
    "PreToolUse": [
      { "matcher": "bash", "hooks": [{ "command": "python3 ~/.codity/guard.py" }] }
    ],
    "PostToolUse": [
      { "matcher": "edit|write|apply_patch", "hooks": [{ "command": "~/.codity/format.sh", "timeout": 120 }] }
    ],
    "UserPromptSubmit": [
      { "hooks": [{ "command": "echo 'Open tickets live in Linear project OPS.'" }] }
    ]
  }
}

Organization and team hooks in the dashboard

Administrators add organization and team hooks on the Hooks tab of the agent policy: choose Add hook, pick the event, enter the command, and for tool events the matcher, then set the timeout and what happens if the hook fails. Select Review changes, then confirm. Organization hooks run on every member's machine, as the person running the agent, so write commands that work on everyone's machines or that do nothing where their script is missing:

if [ -f /opt/company/agent-guard ]; then /opt/company/agent-guard; fi

A plain python3 /path/to/missing.py exits with code 2 when the file is missing, which blocks. A member's own ~/.codity/hooks.json adds hooks but cannot remove the organization's or team's.

The policy YAML on the Rules tab shows the same hooks under a top-level hooks: key:

version: 1
permissions: []
hooks:
  PreToolUse:
    - matcher: bash
      hooks:
        - command: /opt/company/agent-guard
          timeout: 10
          on_failure: closed
  Stop:
    - hooks:
        - command: /opt/company/check-tests
Field Meaning
matcher PreToolUse and PostToolUse only. Tool names separated by |, with * as a wildcard, matched without regard to case. Leave it out to match every tool.
command The shell command to run, up to 4,096 characters.
type Optional. Only command is supported.
timeout Whole seconds from 1 to 600. Defaults to 60.
on_failure open (the default) lets the action continue when the hook crashes or times out. closed blocks it instead.

Each source can define at most 50 hooks. A malformed file or policy is rejected with a message naming the problem: an organization or team policy cannot be saved, and a malformed personal file is skipped.

Tool names you can match include bash, read, edit, write, glob, grep, webfetch and task, and MCP tools by their server and tool name. OpenAI models edit files with apply_patch, so a matcher meant for every file change should be edit|write|apply_patch.

What a hook receives

The event arrives as JSON on the command's standard input:

{
  "hook_event_name": "PreToolUse",
  "session_id": "ses_...",
  "tool_name": "bash",
  "tool_input": { "command": "rm -rf build" },
  "source": "user",
  "cwd": "/home/you/project"
}

tool_response is added for PostToolUse, prompt for UserPromptSubmit, and stop_hook_active for Stop (true when the agent is already continuing because of a Stop hook). source is organization, team or user.

The command starts in a temporary folder, not in the project, so a relative path can never run a file from the repository. The project folder is in the payload's cwd and in CODITY_PROJECT_DIR. The environment is the one you started the agent with, plus CODITY_HOOK_EVENT and CODITY_SESSION_ID. The run's own credentials are never passed to hooks.

What a hook returns

The command Result
Exits 0 with no output The action continues.
Exits 0 and prints plain text, for UserPromptSubmit or SessionStart The text is added as context for the model.
Exits 0 and prints JSON with additionalContext The text is added as context. For tool events it reaches the agent with the tool's result.
Exits 0 and prints JSON with "decision": "block" and a reason, or hookSpecificOutput.permissionDecision: "deny" Blocks, as exit code 2 does.
Exits 2 Blocks. Standard error is the reason the agent sees.
Exits with any other code, crashes or times out Treated as a failure: the action continues, unless the hook has on_failure: closed.

Matching hooks for an event run at the same time. If any of them blocks, the action is blocked, and the reason comes from the first blocking hook in order (organization, team, you). Context from every hook is combined, up to 10,000 characters. Output beyond 64 KB is ignored.

A blocking hook stops the action whatever your permissions or mode. It applies in Auto mode, and a tool call approved with Allow once still goes through PreToolUse.

Example: refuse rm -rf

~/.codity/guard.py:

import json, sys

event = json.load(sys.stdin)
command = (event.get("tool_input") or {}).get("command", "")
if "rm -rf" in command:
    print("rm -rf is not allowed here. Delete specific files instead.", file=sys.stderr)
    sys.exit(2)

With the PreToolUse entry above, asking the agent to run rm -rf build is refused, and the agent's reply quotes your message.

See what hooks did

When a session starts, the agent's log and its timeline on the dashboard say which hooks are active, for example Hooks active: 1 from your organization, 2 from ~/.codity/hooks.json. Every hook that blocks, adds context, fails or times out appears in the run's timeline with its event, source, command, outcome and duration.

Changing hooks

Hooks are read when a session starts. Saving an organization or team policy applies to new and resumed sessions. Switching team policy during a session, with /team-policy in the terminal or the team button in VS Code, applies the new team's hooks straight away.

If an organization or team defines hooks and the agent's engine is too old to enforce them, the session refuses to start and tells you to update, rather than running without them.