Home/Blog/How to Use the Claude Code Hooks Prompt to Add Guardrails and Auto Formatting Your Team Can Trust
Blog

How to Use the Claude Code Hooks Prompt to Add Guardrails and Auto Formatting Your Team Can Trust

P
promptstudio

Write Claude Code hooks a team can commit: the right settings file scope, PreToolUse guards that block destructive shell commands and edits to protected paths with exit code 2 and a reason Claude can read, PostToolUse formatting on edited files, a Stop hook that checks work without looping forever, and a plan to test each hook before trusting it.

How to Use the Claude Code Hooks Prompt to Add Guardrails and Auto Formatting Your Team Can Trust

Claude Code can edit files and run shell commands on its own, which is exactly what makes it useful. It also means teams want a few firm rules: never force push, never touch the migrations folder, always format edited files, and do not declare a task finished while the typecheck fails. Hooks let you enforce those rules with small scripts that run at specific moments, such as before a tool call or when Claude is about to stop. The Claude Code Hooks Config Writer for Team Repos: settings.json PreToolUse and PostToolUse Matchers, Exit Code 2 Blocking, stdin JSON Parsing with jq, Auto Format After Edits, Stop Hook Loop Guard, and a Hook Test Plan prompt writes the settings block, the scripts, and a test plan for your repo, so the rules are enforced by code instead of reminders.

What the prompt produces

  1. A settings file choice between user settings, a committed project settings file, or a local file that stays out of git.
  2. A hooks block in valid JSON with events, matchers for tool names, and command hooks pointing at scripts in the repo.
  3. A PreToolUse guard that reads the JSON on stdin, checks the shell command, and blocks it with exit code 2 and a reason Claude can read.
  4. A path guard for edit tools that blocks changes to protected files.
  5. PostToolUse automations such as formatting only the file that was just edited.
  6. A Stop hook that runs a final check and avoids looping forever.
  7. Every script in full, with comments on what it reads and returns.
  8. A test plan that pipes sample JSON into each script before you rely on it.
  9. A security note about reviewing hook changes like any other code.

How to fill the inputs

RepoStack lists your language, package manager, formatter, linter, and test or typecheck commands. The scripts call these directly, so use the exact commands your team runs.

BlockedActions names commands that need a human, such as force pushes, database resets, or recursive deletes outside safe folders.

ProtectedPaths lists files and folders Claude should never edit, such as environment files, applied migrations, and lockfiles.

Automations describes what should happen on its own, like formatting after edits or a typecheck before Claude finishes.

Scope tells the prompt whether hooks are just for you or shared with the team, which decides which settings file to use.

Reading the example output

The example is a TypeScript monorepo using pnpm, Prettier, ESLint, and a recursive typecheck.

  • The settings file is committed so every teammate gets the same guardrails, with personal tweaks kept in the local file.
  • The Bash guard blocks force pushes and migrate resets, and only allows recursive deletes of node_modules.
  • The path guard protects environment files, migrations, and the lockfile, and tells Claude to explain the change to the user instead.
  • The format hook runs Prettier on the edited file only and never fails the session if the formatter is missing.
  • The Stop hook runs the typecheck and sends failures back so Claude keeps working, while a loop guard stops it from running forever.
  • The test plan shows expected exit codes for each sample input.

Tips for better results

  • Make every script executable and commit them with the settings file.
  • Test each script by piping sample JSON before starting a Claude session.
  • Keep block messages short and actionable, since Claude reads them and adjusts.
  • Add timeouts to slow hooks like typechecks.
  • Check the hooks menu in Claude Code after changes to confirm they loaded.

Mistakes to avoid

  • Do not write hooks that fail the whole session when a tool is missing.
  • Do not forget the loop guard on Stop hooks.
  • Do not treat hooks as harmless configuration. They run commands with your own access.
  • Do not rely on hook fields or events you have not confirmed in the current documentation.

Who it is for

Engineering leads rolling out Claude Code to a team, developers who want safer agent sessions on their own machines, platform engineers maintaining shared tooling, and open source maintainers who want contributors' agents to follow repo rules.

Related PromptDig links

Open the Claude Code Hooks Config Writer for Team Repos: settings.json PreToolUse and PostToolUse Matchers, Exit Code 2 Blocking, stdin JSON Parsing with jq, Auto Format After Edits, Stop Hook Loop Guard, and a Hook Test Plan prompt and describe your repo to generate your hooks and scripts. For more AI coding and developer tool prompts, Browse more prompts. If you have a Claude Code setup prompt that works for your team, Share a prompt.