> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fiddler.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Claude Code Guardrails

> Use Claude Code HTTP hooks to block prompts that contain secrets or PII before they reach the model, and ask Claude to rewrite flagged responses.

## Overview

[Claude Code](https://code.claude.com/docs/en/overview) can call Fiddler Guardrails from its
[HTTP hooks](https://code.claude.com/docs/en/hooks),
so every prompt a developer submits and every response Claude gives is checked for secrets and personally identifiable information (PII).
No gateway or proxy is involved:
each developer's Claude Code calls Fiddler directly.

* **Prompts** are checked on the `UserPromptSubmit` hook.
  A flagged prompt is blocked and never reaches the model.
* **Responses** are checked on the `Stop` and `SubagentStop` hooks, after Claude finishes its turn.
  A flagged response gets a rewrite request: Claude is told what was found and asked to answer again without it.
  The developer has already seen the original response, so this flags it after the fact rather than preventing it.

| You want to… | Use | Page |
| - | - | - |
| Observe Claude Code prompts, tool calls, tokens, and latency | Claude Code OpenTelemetry export | [Claude Code Monitoring](https://docs.fiddler.ai/integrations/agentic-ai/claude-code-integration.md) |
| Block secrets and PII in prompts, and flag them in responses | Claude Code HTTP hooks | This page |

> [!WARNING]
>
> Requires **Fiddler 26.30 or later** and **Claude Code v2.1.295 or later**.
> Earlier Fiddler releases either don't have the endpoint (it returns HTTP 404) or let content Fiddler can't check through by default; see [Failure and Oversize Modes](#failure-and-oversize-modes).

***

## How It Works

```mermaid theme={null}
graph LR
    Dev["Developer"] -->|"submits prompt"| CC["Claude Code"]
    CC -->|"POST /v3/guardrails/claude-code<br/>(UserPromptSubmit, Stop, SubagentStop)"| Fiddler["Fiddler Guardrails"]
    Fiddler -->|"{} (allow)"| CC
    Fiddler -->|"block / rewrite request / warning"| CC
    CC -->|"only allowed prompts"| Model["Claude model"]
```

Every hook posts its JSON input to the same URL.
Fiddler reads the `hook_event_name` field, checks the one piece of text that event carries,
and replies with [hook output](https://code.claude.com/docs/en/hooks) that Claude Code acts on.

***

## Prerequisites

* An **Org Admin** in Fiddler, to create the service account below.
* A Fiddler URL whose hostname **resolves to a public IP address**.
  Claude Code refuses HTTP hooks to private addresses; see [Network Requirements](#network-requirements).
* **Claude Code v2.1.295 or later** on each developer machine.

***

## Quick Start

**1. Create a service account key**

In Fiddler, [create a service account](https://docs.fiddler.ai/reference/access-control/service-accounts.md#create-a-service-account) with **Entire Org** access and only the `guardrails:invoke` capability,
then [create an API key](https://docs.fiddler.ai/reference/access-control/service-accounts.md#create-an-api-key) for it.
An Entire Org key with `guardrails:invoke` can call Fiddler Guardrails and nothing else, so it can't read your data.

Make the key available to Claude Code as an environment variable, for example:

```bash theme={null}
export FIDDLER_GUARDRAILS_API_KEY="fks_..."
```

**2. Add the hooks to your Claude Code settings**

Add this `hooks` block to `~/.claude/settings.json`, replacing `<your-fiddler-url>`.
To roll it out to everyone, put it in managed settings instead; see [Roll Out to Your Organization](#roll-out-to-your-organization).

```json theme={null}
{
  "hooks": {
    "UserPromptSubmit": [
      {
        "hooks": [
          {
            "type": "http",
            "url": "https://<your-fiddler-url>/v3/guardrails/claude-code",
            "timeout": 30,
            "headers": { "Authorization": "Bearer $FIDDLER_GUARDRAILS_API_KEY" },
            "allowedEnvVars": ["FIDDLER_GUARDRAILS_API_KEY"],
            "onFailure": "block"
          }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          {
            "type": "http",
            "url": "https://<your-fiddler-url>/v3/guardrails/claude-code",
            "timeout": 30,
            "headers": { "Authorization": "Bearer $FIDDLER_GUARDRAILS_API_KEY" },
            "allowedEnvVars": ["FIDDLER_GUARDRAILS_API_KEY"]
          }
        ]
      }
    ],
    "SubagentStop": [
      {
        "hooks": [
          {
            "type": "http",
            "url": "https://<your-fiddler-url>/v3/guardrails/claude-code",
            "timeout": 30,
            "headers": { "Authorization": "Bearer $FIDDLER_GUARDRAILS_API_KEY" },
            "allowedEnvVars": ["FIDDLER_GUARDRAILS_API_KEY"]
          }
        ]
      }
    ]
  }
}
```

Claude Code fills in `$FIDDLER_GUARDRAILS_API_KEY` only because it's listed in `allowedEnvVars`.
`"onFailure": "block"` blocks the prompt when Fiddler can't be reached or returns an error;
without it, the prompt goes to the model unchecked.
`onFailure` has no effect on `Stop` and `SubagentStop`.

**3. Verify**

Check that the key and URL work by sending the payload Claude Code would send for a prompt that contains an API key:

```bash theme={null}
curl -s "https://<your-fiddler-url>/v3/guardrails/claude-code" \
  -H "Authorization: Bearer $FIDDLER_GUARDRAILS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"hook_event_name": "UserPromptSubmit", "session_id": "verify", "prompt": "Use this key: sk-ant-api03-abcdefghijklmnopqrstuvwxyz0123456789"}'
```

Expected output:

```json theme={null}
{
  "decision": "block",
  "reason": "Blocked by Fiddler guardrails: the prompt contains secrets (Anthropic API Key), PII (secret). Remove it and try again.",
  "hookSpecificOutput": {"hookEventName": "UserPromptSubmit", "suppressOriginalPrompt": true}
}
```

Then start Claude Code and submit the same prompt.
Claude Code shows the reason and doesn't send the prompt to the model.
Right after a Fiddler deployment, the first checks can time out while the models start; see [Timeouts](#timeouts).

***

## What Gets Checked

| Hook | Text checked | Checks that run by default | When something is found |
| - | - | - | - |
| `UserPromptSubmit` | `prompt`, the prompt as submitted | PII, secrets | The prompt is blocked |
| `Stop` | `last_assistant_message`, Claude's final response in the turn | PII, secrets | Claude is asked to rewrite its response |
| `SubagentStop` | `last_assistant_message`, the subagent's final response | PII, secrets | The subagent is asked to rewrite its response |

`UserPromptSubmit` also fires on turns Claude Code starts itself, such as scheduled tasks, `/loop` iterations, a background subagent reporting back, and a message from another session.
Those prompts are checked too, and can be blocked, for example as "could not be checked" while the models start.

These aren't checked:

* Tool input and output, such as file contents Claude reads, command output, and MCP tool results.
* The expanded content of a slash command or skill. The hook receives the command as typed, such as `/deploy staging`, so the skill's content and any shell output it injects reach the model unchecked.
* The contents of files attached with `@` mentions. In Claude Code v2.1.311 the hook receives the mention, such as `@.env`, not the file contents.
* Text Claude writes between tool calls. Only the final response of a turn reaches the `Stop` hook.
* A subagent report delivered through the `SubagentHandback` tool. `last_assistant_message` holds only the subagent's closing text.
* Any other hook event. Fiddler returns `{}` for events it doesn't check, so registering the URL on them has no effect.

Hooks can't edit text, so a check whose default action is to redact (PII and secrets) blocks instead.

***

## Check Behavior

By default, the endpoint runs the PII and secrets checks with Fiddler's server-side defaults;
see [Guardrails](https://docs.fiddler.ai/protection/guardrails.md) and the [secrets tutorial](https://docs.fiddler.ai/developers/tutorials/guardrails/guardrails-secrets.md).

### Header-Based Configuration

Set these in the hook's `headers`, next to `Authorization`.
They apply to every call from that hook, so you can configure `UserPromptSubmit` and `Stop` differently.

| Header | Value | Effect |
| - | - | - |
| `x-fiddler-guardrails` | Comma-separated: `pii`, `secrets` | Runs only the named checks. Absent, blank, or naming no known check: PII and secrets. |
| `x-fiddler-pii-entities` | Comma-separated entity names, such as `email,phone number` | Checks only these PII entity types. See [supported entity types](https://docs.fiddler.ai/developers/tutorials/guardrails/guardrails-pii.md#supported-entity-types). |
| `x-fiddler-pii-threshold` | Float, such as `0.8` | Overrides the PII detection threshold. |
| `x-fiddler-failure-mode` | `open` or `closed` (default) | What happens when a check can't complete. See [Failure and Oversize Modes](#failure-and-oversize-modes). |
| `x-fiddler-oversize-mode` | `open` or `closed` (default) | What happens to text too long to check. |
| `x-fiddler-timeout` | Seconds, such as `12` | Fiddler's time budget for the checks. Default 12, maximum 60. |

`x-fiddler-pii-mode` and `x-fiddler-secrets-mode` are accepted but make no difference here,
since both redact and block become a block.

**Naming a subset turns the others off.**
`x-fiddler-guardrails: secrets` runs only the secrets check, so an email address in a prompt is allowed.
The PII override headers still apply when `x-fiddler-guardrails` is absent.

### Reduce False Positives

The PII model's default entity list includes broad types such as person names, which everyday coding prompts trip:
a first name, a UUID, or a phrase like "use a subagent to" can be flagged.
Limit PII to the entity types you care about with `x-fiddler-pii-entities`, for example:

```json theme={null}
"headers": {
  "Authorization": "Bearer $FIDDLER_GUARDRAILS_API_KEY",
  "x-fiddler-pii-entities": "email,phone number,credit card number"
}
```

The default list also includes a `secret` entity, so a key is often reported by both checks,
as in `secrets (Anthropic API Key), PII (secret)`.

***

## What Developers and Claude See

| Outcome | Hook output | What happens in Claude Code |
| - | - | - |
| Nothing found | `{}` | The prompt or response goes ahead. |
| Prompt flagged | `decision: block` with `suppressOriginalPrompt` | The prompt isn't sent. The developer sees the reason, without the prompt text echoed back. |
| Response flagged | `decision: block` | Claude gets the reason and continues the turn to rewrite its response. |
| Prompt couldn't be checked, mode `closed` | `decision: block` | The prompt isn't sent. The reason says why it couldn't be checked. |
| Response couldn't be checked, mode `closed` | `systemMessage` | The developer sees a warning. Claude isn't asked to rewrite. |

Reasons name the category and never the text that was found:

```text theme={null}
Blocked by Fiddler guardrails: the prompt contains PII (email). Remove it and try again.
Fiddler guardrails flagged your previous response for secrets (Anthropic API Key). Rewrite your answer without secrets (Anthropic API Key).
Blocked by Fiddler guardrails: the prompt could not be checked (oversize_mode=closed).
Fiddler guardrails could not check this response: the PII and secrets checks could not complete (failure_mode=closed).
```

> [!NOTE]
>
> A blocked prompt never reaches the model, but Claude Code can still keep it in local files such as the session transcript and prompt history.
> A hook keeps a secret away from the model, not off the developer's disk.

Every flagged response gets a rewrite request, including when the hook input's `stop_hook_active` is `true`.
Claude Code ends a loop of rewrite requests itself:
after 8 consecutive stop-hook continuations it ends the turn, and the count resets whenever Claude calls a tool.
`CLAUDE_CODE_STOP_HOOK_BLOCK_CAP` changes the cap.

***

## Failure and Oversize Modes

Two independent settings decide what happens to text Fiddler couldn't check.
Both default to `closed`.

* `x-fiddler-failure-mode`: a check couldn't complete, for example because it timed out or the detector returned an error.
* `x-fiddler-oversize-mode`: the text is too long to check, either over the scan limit (50,000 characters by default) or rejected by the detector as too long.

`x-fiddler-failure-mode: open` doesn't let oversized text through; set `x-fiddler-oversize-mode: open` for that.

| Situation | Setting | `closed` (default) | `open` |
| - | - | - | - |
| A check can't complete | `failure_mode` | Prompt blocked; response warned | Allowed |
| Payload names a checked event but can't be read (for example after a Claude Code update changed its shape) | `failure_mode` | Prompt blocked; response warned | Allowed |
| `Stop` payload without `last_assistant_message` | `failure_mode` | Response warned | Allowed |
| Text over 50,000 characters, with per-message scanning off (the default) | `oversize_mode` | Prompt blocked; response warned | About the first and last 25,000 characters are checked |
| With per-message scanning on: text the detector rejects as too long | `oversize_mode` | Prompt blocked; response warned | About the first and last 25,000 characters are checked |
| Body isn't JSON, or the event isn't one Fiddler checks | — | Allowed | Allowed |

A response is never blocked because Fiddler couldn't check it: the developer has already seen it, and there is nothing for Claude to fix.

**Long text under `open`.**
Developers often paste a file or a log and ask a question at the end, so for text over the limit Fiddler checks the beginning and the end.
**A secret or email address in the middle of a long paste isn't found.**
If your deployment has per-message scanning turned on, the PII and secrets checks read the whole text with no limit.
Long text then isn't blocked under `closed` just for its length.
Ask your Fiddler administrator whether it is on.

To change the defaults for every hook at once, a self-hosted administrator can set the Helm values `ingestionLlm.guardrailsFailureMode` and `ingestionLlm.guardrailsOversizeMode`.
Headers on a hook override them.

### Timeouts

Two timeouts apply:

* **Claude Code's hook `timeout`**, in seconds. `UserPromptSubmit` defaults to 30.
  A hook that times out counts as a failure: the prompt goes ahead unchecked, or is blocked with `"onFailure": "block"`.
* **Fiddler's `x-fiddler-timeout`**, 12 seconds by default. A check that doesn't finish in time follows `x-fiddler-failure-mode`.

Keep the hook `timeout` above `x-fiddler-timeout`, with room to spare: **30 seconds or more** with the default of 12.
A request can run a few seconds past `x-fiddler-timeout`,
and if your deployment has per-message scanning turned on, a check of text over 50,000 characters can take up to twice `x-fiddler-timeout`.
If Claude Code gives up first, the hook counts as a failure instead of following `x-fiddler-failure-mode`.

A warm check takes about a second.
Right after a Fiddler deployment, checks can reach `x-fiddler-timeout` while the models start, and prompts are blocked as "could not be checked" until they're ready.

***

## Roll Out to Your Organization

Put the `hooks` block in Claude Code [managed settings](https://code.claude.com/docs/en/settings) so it applies on every developer machine.
Developers can't remove hooks deployed this way, and `disableAllHooks` set outside managed settings doesn't turn them off.
These managed settings restrict which hooks run:

* `allowedHttpHookUrls`: Claude Code runs an HTTP hook only if its URL is on this list.
* `httpHookAllowedEnvVars`: Claude Code fills in only these environment variables in hook headers.
* `allowManagedHooksOnly`: blocks hooks from user, project, local, and plugin settings, so only the hooks you deploy run.

> [!WARNING]
>
> With the setup on this page (`x-fiddler-failure-mode` at its `closed` default and `"onFailure": "block"` on `UserPromptSubmit`), every developer's prompts are blocked while Fiddler can't answer: during an outage, while the models start after an upgrade, or once the shared key has expired or been revoked.
> Rotate the key well before it expires, and decide deliberately whether prompts should be blocked or allowed when Fiddler is unavailable.

**Distributing the key.**
Every developer machine needs the service account key, either in its environment or in the managed settings `env` block.
A key copied to many laptops is likely to leak eventually.
It can only call Fiddler Guardrails, but guardrail calls aren't rate limited, so a leaked key could use your deployment's guardrail capacity.
That capacity is shared with your other guardrail integrations, such as LiteLLM and AgentGateway, so heavy use of a leaked key could slow or block their traffic too.

* Use a service account and key for this purpose only, and one per team where practical.
* [Rotate the key](https://docs.fiddler.ai/reference/access-control/service-accounts.md#rotate-an-api-key) well before it expires: an expired key blocks every developer's prompts.
* If a key leaks, follow [Respond to a Leaked Key](https://docs.fiddler.ai/reference/access-control/service-accounts.md#respond-to-a-leaked-key).

### Network Requirements

Claude Code refuses to call an HTTP hook whose hostname resolves to a private or link-local address,
such as `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`, or `100.64.0.0/10`.
The hook fails with `ERR_HTTP_HOOK_BLOCKED_ADDRESS`, even over a VPN and even when the URL is in `allowedHttpHookUrls`.
Only loopback addresses are exempt.
This behavior was observed in Claude Code v2.1.309 and isn't described in Anthropic's documentation.

If your Fiddler deployment is reachable only on private addresses, give it a hostname that resolves to a public IP address.
Access can still be restricted to your corporate network by a firewall.

***

## Data Handling

The endpoint checks the text in the request and returns a decision.
It writes no prompt, response, or detection to any Fiddler datastore, and a decision produces no span or record in the Fiddler UI.
Fiddler's logs contain no prompt or response text: they record IDs, the event and the decision, plus detection counts, check names and scores.

To see Claude Code sessions in Fiddler, add [Claude Code Monitoring](https://docs.fiddler.ai/integrations/agentic-ai/claude-code-integration.md) or the [Fiddler Claude Code Plugin](https://docs.fiddler.ai/integrations/agentic-ai/fiddler-claude-code-plugin.md).

***

## Troubleshooting

**Prompts go through without being checked**

Run `/hooks` in Claude Code to confirm the hooks loaded, and check that the hostname resolves to a public IP address (see [Network Requirements](#network-requirements)).
Without `"onFailure": "block"`, any hook failure lets the prompt through: a timeout, a connection error, or an HTTP error from Fiddler.

**HTTP 403 with "API key scope does not permit this operation"**

The key belongs to an **Entire Org** service account without the `guardrails:invoke` capability.
Add the capability to the service account.

**HTTP 403 with "Guardrails is not enabled for this cluster", or HTTP 404**

Guardrails isn't enabled on your deployment, the deployment predates Fiddler 26.29, or it's a free-tier deployment.
Contact your Fiddler administrator.

**HTTP 403 with a Specific Projects key**

Keys of **Specific Projects** service accounts can't call guardrails. Use an **Entire Org** service account with `guardrails:invoke`.

**HTTP 401**

The `Authorization` header is missing or the key is wrong. Check that the variable is set where Claude Code runs and listed in `allowedEnvVars`.

**Prompts are blocked as "could not be checked"**

A check didn't finish within `x-fiddler-timeout` (`failure_mode=closed`), or the prompt was too long to check (`oversize_mode=closed`): over 50,000 characters with per-message scanning off, or rejected by the detector as too long.
Right after a deployment, wait for the models to start.
For long prompts, see [Failure and Oversize Modes](#failure-and-oversize-modes).

**Everyday prompts are blocked for PII**

See [Reduce False Positives](#reduce-false-positives).

***

## Endpoint Reference

```text theme={null}
POST /v3/guardrails/claude-code
```

Authentication: `Authorization: Bearer <api-key>`.
The key can be an **Entire Org** service account key with `guardrails:invoke` (recommended),
or the API key of a user who is an Org Admin or Org Member.
Don't put a user's API key in hook settings: it carries all of that user's access to Fiddler data.

The request body is the hook input Claude Code sends.
Fiddler reads `hook_event_name`, `prompt` (`UserPromptSubmit`), `last_assistant_message` (`Stop`, `SubagentStop`), and `session_id` and `prompt_id` for its logs.
It ignores every other field, including `cwd` and `transcript_path`.

| Status | When |
| - | - |
| 200 | Every request that passes authentication, including payloads Fiddler can't read. The body is the hook output. |
| 401 | Missing or invalid API key. |
| 403 | The key doesn't have access, or Guardrails isn't enabled. |
| 404 | Free-tier deployment, or Fiddler earlier than 26.29. |
| 500 | Unexpected server error outside the checks. |

Response bodies:

```json theme={null}
// allow
{}

// prompt blocked
{"decision": "block", "reason": "...", "hookSpecificOutput": {"hookEventName": "UserPromptSubmit", "suppressOriginalPrompt": true}}

// response flagged: rewrite request
{"decision": "block", "reason": "..."}

// response couldn't be checked
{"systemMessage": "..."}
```

***

## Known Limitations

| Limitation | Details |
| - | - |
| **Responses are flagged after they're shown** | The `Stop` hook runs after the developer has seen the response, and the response stays in the conversation and the transcript. |
| **Tool input and output aren't checked** | File contents, command output, and MCP results reach the model without a check. |
| **Skills, slash commands, and `@` mentions aren't expanded** | The hook sees the command or mention as typed, so a skill's content, its shell output, and attached file contents reach the model without a check. |
| **No redaction** | Hooks can't rewrite text, so a prompt with a secret is blocked rather than sent with the secret removed. |
| **Middle of long text** | With per-message scanning off and `x-fiddler-oversize-mode: open`, the middle of text over 50,000 characters isn't checked. |
| **Private addresses** | Claude Code won't call a hook URL that resolves to a private address. See [Network Requirements](#network-requirements). |

## Next Steps

* [Service Accounts](https://docs.fiddler.ai/reference/access-control/service-accounts.md) — create and rotate the `guardrails:invoke` key.
* [Fiddler Guardrails](https://docs.fiddler.ai/protection/guardrails.md) — the PII and secrets checks behind the endpoint.
* [Claude Code Monitoring](https://docs.fiddler.ai/integrations/agentic-ai/claude-code-integration.md) — send Claude Code traces to Fiddler.
* [Claude Code hooks reference](https://code.claude.com/docs/en/hooks) — Anthropic's documentation for hook configuration.
