Langfuse launches AI Gateway Learn more
DocsGuardrails

Guardrails

Beta. The Langfuse AI Gateway is in public beta and can still change.

Guardrails inspect model requests and responses against pattern rules or external providers to block, redact, or log sensitive content such as PII and secrets. Organization owners and admins manage guardrails under AI Gateway → Guardrails; other members with gateway access have a read-only view.

Guardrails page with the flagged requests chart over time and a table of PII, secrets, keyword, and custom regex guardrails showing their scope, action, targets, and 7-day flagged counts

Create a guardrail

Select Add guardrail under AI Gateway → Guardrails, pick a built-in rule or Amazon Bedrock, and choose its action, evaluation scope, and targets.

An organization can have up to 20 guardrails. Creating, editing, reassigning, and deleting guardrails are recorded in the audit log.

Actions

Choose what happens On match:

ActionBehavior
BlockRejects the request or response with 400 (guardrail_blocked). Blocked requests never reach the provider.
RedactReplaces matched text with your Replacement label (default [REDACTED]) before forwarding the request or returning output.
LogPasses the request or response through unchanged and records the match on the generation.

When multiple guardrails apply, all of them evaluate; Block takes precedence over Redact and Log. Rejected calls return 400 (guardrail_blocked) with the guardrail name (for example, Request blocked by guardrail "PII"). Enforcing guardrails (Block, Redact, and Bedrock Enforce) also fail closed if a payload is unreadable or exceeds scan or buffer limits; see AI Gateway errors for all guardrail error codes.

In FULL ingestion mode, redacted text is replaced before the trace is captured, so scrubbed PII or secrets aren't stored in Langfuse. Blocked responses omit their output from FULL ingestion while still recording upstream token usage against budgets.

Evaluation scopes

Choose what each guardrail inspects under Runs on:

ScopeEvaluated text
Last turn (default)Latest user turn and tool outputs since the last model reply (system prompts on the first turn)
Entire inputAll messages and system prompts in the request, including conversation history
OutputProvider responses before returning them to the client
Last turn & outputLatest user turn and tool outputs, plus provider responses
Entire input & outputAll messages and system prompts in the request, plus provider responses

Block only needs Last turn on input because every turn is checked before reaching the provider — nothing blocked is sent, so earlier turns have already passed and checking full history would permanently lock existing conversations. Use Entire input (or Entire input & output) for Redact rules so earlier turns aren't resent to the provider unredacted.

When any guardrail checks Output, the gateway buffers the full provider response (including streamed SSE deltas) before returning it to the client, so matches spanning chunk boundaries are caught and blocked responses never leak partial tokens. Requests with no active output guardrail stream through unchanged.

Built-in rules

GuardrailMatches
PIIEmails, US SSNs and ITINs, UK NINOs, EU VAT and German tax IDs, phone numbers, cards, IBANs, and IPs
Secrets and API keysProvider API keys, tokens, database connection URLs, JWTs, PEM private keys, and up to 20 prefixes
Prompt injection (heuristic)Instruction override, system prompt extraction, jailbreak phrases, and hidden Unicode (defaults to Log)
Keyword listUp to 500 ASCII words or phrases (one per line, max 100 chars each), whole-word and case-insensitive
Custom regexCustom Rust regular expressions in ASCII mode (up to 500 chars)

Prompt injection (heuristic) matches common prompt injection phrases rather than novel attacks and can flag legitimate security text. It defaults to Log so you can review matches before blocking.

Amazon Bedrock guardrails

You can connect an existing Amazon Bedrock guardrail from the Integrations tab. The gateway calls Bedrock's ApplyGuardrail API using your Bedrock API key (bedrock:ApplyGuardrail permission) and supports two actions: Enforce (block when Bedrock blocks, or anonymize matched text) and Log.

We recommend using Last turn (or Last turn & output) scope rather than Entire input with Amazon Bedrock. Bedrock enforces per-request size and text-unit limits on ApplyGuardrail (and bills per text unit), so sending long conversation histories can hit Bedrock limits or trigger rate limits. Enforcing Bedrock guardrails fail closed on provider errors, rate limits, or timeouts (3 seconds by default), returning 429 or 503 so unscreened traffic does not pass through.

Targets

Under Applies to, assign a guardrail to Everyone in the organization or to up to 100 specific users and gateway API keys. Select multiple rows in the table to update targets or delete guardrails in bulk.

Flagged requests and traces

The Flagged requests chart on AI Gateway → Guardrails plots matches over time; select table rows to break the chart down by guardrail. Selecting a guardrail's Flagged 7d count opens its matching traces in the ingestion project. Both require USAGE or FULL ingestion mode.

Generations that pass through with a Redact or Log match get level WARNING, and blocked calls get level ERROR. Matched generations carry these metadata keys:

Metadata keyValue
langfuse.gateway.guardrails.outcomeHighest-severity action across the exchange: block, redact, or log
langfuse.gateway.guardrails.<id>Highest-severity action for that guardrail: block, redact, or log

FAQ

Which endpoints run request and response guardrails?

Request guardrails run on every model-call POST endpoint.

Response guardrails run on successful (2xx) JSON and SSE responses from /openai/v1/responses, /openai/v1/chat/completions, and /anthropic/v1/messages, inspecting assistant text, refusals, thinking or reasoning blocks, and tool call arguments. Decisions, Embeddings, Responses compaction, and count_tokens skip response guardrails.

When does a Redact guardrail block instead of redacting?
  • Signed Anthropic thinking blocks: Modifying signed thinking blocks invalidates their provider signature on later turns, so a Redact (or Bedrock Enforce) match inside signed thinking fails closed with 400 (guardrail_blocked). Log guardrails pass through unchanged.
  • JSON-encoded tool call arguments: If replacing a matched span in tool call arguments would produce invalid JSON, the response fails closed with 400 (guardrail_blocked).
What are the size limits for guardrail scanning?
  • Built-in regex guardrails scan up to 1 MiB of extracted text per scope by default (configurable on self-hosted gateways via LANGFUSE_AI_GATEWAY_GUARDRAIL_MAX_SCAN_BYTES).
  • Response guardrails buffer up to 8 MiB for JSON responses and 32 MiB for streamed SSE responses.
  • Exceeding these limits fails closed for enforcing guardrails (413 guardrail_input_too_large on requests, 502 guardrail_response_too_large on responses); Log-only guardrails record a match and let traffic through.
✉️

Langfuse AI Gateway updates: get an email when we ship new gateway features and providers. Gateway updates only, no spam.


Was this page helpful?

Last updated on