# A free risk hook for Claude Code: make the agent flag prod, data and money commands before it runs them

> Source: <https://dev.to/syncoders/a-free-risk-hook-for-claude-code-make-the-agent-flag-prod-data-and-money-commands-before-it-runs-1pf7>
> Published: 2026-11-22 04:30:14+00:00

**Key takeaway:** A PreToolUse hook on Bash plus a UserPromptSubmit rule can deny high-risk commands like kubectl delete or terraform apply until the agent emits a risk/blast/undo/gate flag and re-runs with RISK_ACK=high.

It's late, the agent has been working well for an hour, and you have started approving things on reflex. The next command in the queue is this:

```
kubectl delete deployment api -n production
```

Maybe that is what you asked for. Maybe the agent read "clean up the old deployment" more broadly than you meant. Either way, the moment to notice is before it runs, and by hour two your attention is the weakest part of the setup.

The risk hook is a small Node script that makes that moment hard to miss. It reads every Bash command Claude Code is about to run, sorts risky ones into three tiers, and denies them until the agent has written a plain risk flag in the chat and re-run the command with an explicit acknowledgement. This post shows how to install it, what a flag looks like, how to tune it, and where it stops.

The hook sits on two Claude Code events:

`RISK_ACK=<tier>`.
The three tiers:

| Tier | Ack value | What it covers | What the agent must write first | 
|---|---|---|---|
| 🟥 HIGH RISK | `high` | Prod or staging changes, data writes, DDL and migrations, killing machines, services, containers or processes, `sudo` , disk ops, deploys and publishes | A `diff` block with`risk` ,`blast` ,`undo` ,`gate` . The gate defaults to waiting for your OK. | 
| 🟧 WARNING | `warn` | Money or credits: paid APIs, cloud, payment processors, enrichment, backfill and scrape runs | A `diff` block with`cost` ,`cap` ,`gate` , then a`spent:` line after the run | 
| 🟨 CAUTION | `caution` | Local but recoverable only with effort ( `rm -r` ,`reset --hard` ,`branch -D` , rebase, force-push to a feature branch,`.env` exposure) or outward-facing (merge, post, message) | One line with what, why, and how to undo | 

Some examples of what lands where:

`kubectl delete ...`, `terraform apply`, `vercel --prod`, `npm publish`: HIGH.` psql "$DATABASE_URL" -c "DROP TABLE users"`: HIGH, as destructive DDL.` curl` POST to `api.openai.com`: WARNING, as a mutating call to a paid service.` git reset --hard HEAD~3`: CAUTION.` rm -rf node_modules` or `rm -rf /tmp/build`: allowed with no flag, because build output and temp folders are cheap to lose.
A hit on something environment-touching escalates to HIGH when the command carries a prod or staging marker. A local operation on a file that merely has "prod" in its name is not escalated.

The parser is quote-aware. It follows `bash -c`, `eval` and `$(...)` up to two levels down, normalises the command name (path, case, quotes), and skips the body of a heredoc that only writes a file.

You need Node and Claude Code.

`risk-flag.js` from `~/.claude/hooks/risk-flag.js`.`~/.claude/settings.json` and add the two entries below under `"hooks"`. If you already have `PreToolUse` or `UserPromptSubmit` arrays, add these objects to them instead of replacing them.

```
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "node ~/.claude/hooks/risk-flag.js",
            "timeout": 5,
            "statusMessage": "Checking risk flags..."
          }
        ]
      }
    ],
    "UserPromptSubmit": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "node ~/.claude/hooks/risk-flag.js",
            "timeout": 5,
            "statusMessage": "Injecting risk-flag rule..."
          }
        ]
      }
    ]
  }
}
```

`git reset --hard HEAD~1` in a scratch repo. It should come back with a CAUTION flag instead of running it straight away.
When the agent tries the `kubectl` command from the top of this post, the hook denies it and Claude Code shows the agent this reason:

```
🟥 risk-flag HIGH RISK [infra]: kubectl mutates a cluster. Flag it FIRST with
the HIGH RISK block (🟥 **HIGH RISK** line + ```

diff with '- ' lines:
risk / blast / undo / gate). gate defaults to 'waiting for your OK': re-run
with RISK_ACK=high as the leading assignment ONLY if the user explicitly
asked for exactly this step; otherwise stop here. (A trailing '# RISK_ACK=<tier>'
comment also works.)
```

The agent then writes something like this in the chat:

🟥 **HIGH RISK** delete the `api` deployment in the `production` namespace

```
diff
- risk : removes the live api deployment; traffic to it fails until redeployed
- blast: every client of the production api
- undo : kubectl apply -f k8s/api.yaml, minutes of downtime
- gate : waiting for your OK
```

The `diff` fence is there for colour: lines starting with `-` render red in most viewers, so the block stands out from normal output. You read four lines and decide. That is the whole point.

A paid API call gets the WARNING version:

🟧 **WARNING** run the enrichment backfill against the OpenAI API

```
diff
! cost : one call per row, total not estimated yet
! cap  : none set
! gate : waiting for your OK
```

A CAUTION is a single line, for example: 🟨 **CAUTION** `git reset --hard HEAD~3` drops the last three commits · undo: `git reflog` then reset back.

Once the flag is written and you have said yes (or you asked for exactly that step), the agent re-runs the command with the tier as a leading assignment:

```
bash
RISK_ACK=high kubectl delete deployment api -n production
```

A trailing comment works too, which is handy for multi-line commands:

```
bash
kubectl delete deployment api -n production # RISK_ACK=high
```

The hook then lets it through and shows `🟥 HIGH RISK acknowledged [infra]: kubectl mutates a cluster`.

The ack must be at or above the detected tier. `RISK_ACK=caution` on a HIGH command is denied again with a message saying it was acked too low. The values are `high`, `warn` and `caution`.

Everything lives in the one file, as plain regex tables near the top:

`INFRA_RULES``[tier, command, arguments, reason]` rows. For example, the `kubectl` row matches `delete`, `apply`, `scale`, `rollout` and similar. Add a row for a CLI your team uses, or drop a row to `caution` if it is too noisy for you.`PROD_MARKERS`` MONEY_MARKERS``backfill` and `scrape` that mean credits are being spent. Add the vendors you pay for.`RULE_TEXT`
After an edit, run the hook against a command by piping a payload into it, for example `{"hook_event_name":"PreToolUse","tool_name":"Bash","tool_input":{"command":"git reset --hard"}}`. An empty output means allowed; a JSON `deny` means flagged.

This is a guardrail against inattention, not a security boundary.

`bash -c`, `eval` and `$(...)`, not deeper.` RISK_ACK=high` without a flag. The rule tells it not to, and the flag in the chat is what you review, so keep reading them.
The risk hook stops the dangerous command. It can't stop your API key landing in the model's context.

Most key leaks are not attacks. They are ordinary debugging steps that print a value where the model reads it:

`.env` reads.`cat .env`, or opens it with a file tool, to "check the config". Every value in the file is now in the conversation.`echo $STRIPE_KEY` to confirm it is set, or `printenv` to find a missing variable, prints the values.`curl -v` prints the `Authorization` header. Some CLIs print their config or the full request when they fail.
Once a value is in context it can end up in transcripts and logs too. None of these is a risky command, so no risk rule fires.

`[ -n "$STRIPE_KEY" ] && echo set || echo missing`. To list what exists, `printenv | cut -d= -f1` shows names only.`.env.example` with names and no values and point the agent at that. Add `"deny": ["Read(./.env)", "Read(./.env.*)"]` under `"permissions"` in your settings so file tools can't open the real one. That rule does not cover `-v` off Sometimes the agent needs to use a real key, not only check it exists. That is the case we built EnvSecure for. It is a Claude Code mod for macOS: the value sits in Keychain, the agent writes `$NAME` in a Bash command, you approve the command in a side pane, and the model gets back `envsecure: ok` or `envsecure: failed`. The output goes to the pane, not the chat.

Who it helps:

If the habits above don't cover your setup, it's at [https://envsecure.syncoderslabs.com/](https://envsecure.syncoderslabs.com/).
