Skip to main content

Why Guardrails?

Once AI applications go to production, they handle real user data and — in the case of agents — call external tools on their own. Things can go wrong fast:
  • A customer support chatbot leaks a user’s credit card number because PII wasn’t stripped from the context.
  • A coding agent runs rm -rf / through an MCP tool after hallucinating a shell command — and nothing stopped it.
  • A healthcare assistant makes up drug dosage numbers. The response reaches the patient unchecked.
  • An internal Q&A bot gets jailbroken through prompt injection, leaking confidential company data.
Guardrails prevent these scenarios. They sit between your application and the LLM (or MCP tool), inspecting and — when needed — blocking or rewriting data before it causes damage. You can attach them to LLM requests (check the prompt going in, check the response coming out) and to MCP tool calls (check the arguments before the tool runs, check the results after it returns).

How a TrueFoundry Guardrail Works

Each guardrail has two settings you configure: what it does with the data, and how strictly it enforces its decisions.

Operation Mode

Request Traces vs. this table: Traces show each guardrail as its own span with start/end times. LLM Input Validation spans often overlap the model span because validation runs alongside the in-flight model request. Output and MCP guardrail spans typically appear after the model or tool span finishes — that ordering reflects synchronous evaluation, not a contradiction with the execution model above.

Enforcement Strategy

This decides what happens when a guardrail catches a violation — and also what happens if the guardrail itself has a problem (like a timeout or a provider outage).
How to roll out safely:
  • Start with Audit so you can see what guardrails would catch without affecting users.
  • Once things look right, switch to Enforce But Ignore On Error — you get protection, but a guardrail provider outage won’t take your app down.
  • Move to Enforce when you need strict compliance.

How TrueFoundry AI Gateway Runs Guardrails

Where guardrails run depends on whether you’re making an LLM call or invoking an MCP tool.
LLM requests have two hooks — Input (before the model sees the prompt) and Output (after the model responds):

LLM Input

Runs before the prompt reaches the LLM:
  • PII masking and redaction
  • Prompt injection detection
  • Content moderation

LLM Output

Runs after the LLM responds:
  • Hallucination detection
  • Secrets detection
  • Content filtering
Diagram showing the flow of LLM requests through input and output guardrails

LLM Request Guardrail Flow

Here’s the order of operations when a request hits the AI Gateway:
  1. Input Mutation guardrails run first and block until they finish (e.g., redacting PII from the prompt).
  2. Input Validation kicks off in the background — it checks for things like prompt injection while the model request is already in flight.
  3. The model request starts with the mutated prompt.
  4. If Input Validation fails while the model is still running, the AI Gateway cancels the model request right away so you don’t pay for it.
  5. Once the model responds, Output Mutation guardrails process the response (e.g., stripping secrets).
  6. Output Validation checks the final result. If it fails, the response is blocked — though model costs have already been incurred at this point.
  7. The clean response goes back to the client.
Streaming (stream: true) and LLM output guardrails: Output guardrails are not applied when the response is streamed ("stream": true). This is because output guardrails need the complete response text to evaluate, but streaming sends the response in chunks as they are generated. If you need output guardrails to run, set "stream": false in your request. Input guardrails work the same way regardless of streaming mode — they always run before the request is sent to the model. See the FAQ for more details.

Latency Impact of Guardrails

Guardrails add processing time — but the AI Gateway is designed to keep that impact small.
  • Input Validation runs in parallel with the model request, so in the happy path, it adds no extra wait time before you see the first token.
  • Input Mutation runs before the model request, so its processing time is added directly.
  • When Input Validation fails, the model request gets cancelled immediately — you don’t pay for a response you were going to throw away.
Execution Flow Examples
Input validation runs in parallel with the model request. Output guardrails process the response before it’s returned.
Input validation fails while the model is running — the model request gets cancelled immediately to save costs.
The model finishes, but output validation fails — the response is rejected. Model costs are already incurred at this point.
You can track the latency impact of each guardrail in AI Gateway → Monitor → Request Traces. Each guardrail span shows its execution time, result, scope, and which entity it was applied on.
Request Traces view showing a selected Guardrail span with latency, result, scope, input, and output details

Guardrail latency visible in Request Traces

How to Apply Guardrails

You can attach guardrails in two ways:
Pass the X-TFY-GUARDRAILS header to apply guardrails on a single request. Handy for testing or when different requests need different guardrails.
First register your guardrails in AI Gateway → Guardrails, then set up guardrail policies in AI Gateway → Policies → Guardrails to apply them automatically based on who’s making the request, which model they’re calling, or which MCP tool is being used. This is the way to go for org-wide enforcement.For a step-by-step walkthrough, see the Getting Started guide. For the full policy guide, see Configure Guardrail Policies.

Supported Guardrails

The AI Gateway ships with built-in guardrails and integrates with a range of external providers — all managed through a single interface.
Built-in guardrails that run on TrueFoundry-managed infrastructure — no external credentials required. The three guardrails powered by Azure services are available only on TrueFoundry SaaS deployments:

Secrets Detection

Catches and redacts credentials like API keys and tokens.

Code Safety Linter

Flags unsafe code patterns and dangerous shell commands.

SQL Sanitizer

Catches risky SQL like DROP or DELETE without WHERE.

Regex Pattern Matching

Matches and redacts sensitive patterns with custom regex.

Prompt Injection (SaaS only)

Detects prompt injection and jailbreaks. Powered by Azure Prompt Shield.

PII Detection (SaaS only)

Finds and redacts PII/PHI. Powered by Azure AI Language.

Content Moderation (SaaS only)

Blocks harmful content. Powered by Azure AI Content Safety.

Metadata Validation

Enforces required metadata keys and values on requests.

Cedar Guardrails

Access control for MCP tools using Cedar policies.

OPA Guardrails

Access control using Open Policy Agent policies.
We also integrate with third-party guardrail providers. Don’t see yours? Reach out — we’re happy to add it.

OpenAI Moderations

AWS Bedrock Guardrail

Azure PII

Azure Content Safety

Azure Prompt Shield

Enkrypt AI

Palo Alto Prisma AIRS

Fiddler

CrowdStrike

Cisco AI Defense

F5 AI Security

Patronus AI

Google Model Armor

Gray Swan Cygnal

Akto

TrojAI

Noma Security

Pillar Security

These providers integrate through a deployable HTTP wrapper and the Custom Guardrail contract (same dashboard flow as bring-your-own guardrails). Clone, deploy the FastAPI service, register URLs in AI Gateway → Guardrails.

NVIDIA NeMo

Guardrails AI

CoreWeave Weave

Lasso Security

Arthur AI

Verra

HiddenLayer

Bring Your Own Guardrail / Plugin

If the built-in and provider integrations don’t cover your use case, you can write your own. Build a custom guardrail with Guardrails.AI, a plain Python function, or any framework you prefer, and plug it into the AI Gateway.

Custom Guardrails

Build and integrate your own guardrail using the template repo, or start from the reference integrations above.

FAQ

By default, guardrails look at all messages in the conversation. If you only care about the latest message, set the X-TFY-GUARDRAILS-SCOPE header:
  • all (default): Checks the full conversation history
  • last: Checks only the most recent message
Using last is faster when you don’t need to scan the whole conversation.
Depends on the enforcement strategy you picked:
  • Enforce: Request gets blocked.
  • Enforce But Ignore On Error: Request goes through anyway. This is the safest default — you stay protected when guardrails work, but a provider outage won’t break your app.
  • Audit: Request always goes through.
Go to AI Gateway → Monitor → Request Traces. You’ll see:
  • Which guardrails ran on each hook
  • Whether they passed or failed, and how long they took
  • What they found (secrets, SQL issues, unsafe patterns, etc.)
  • What mutations were applied
Traces are logged for both successful and blocked requests.
No. Output guardrails are skipped when the response is streamed ("stream": true). Output guardrails need the complete response text to evaluate, but streaming sends tokens to the client as they are generated — so there is no full response to check before delivery begins.What you can do:
  • Set "stream": false if you need output guardrails to run. This ensures the AI Gateway receives the full response, evaluates it against your output guardrails, and only then returns it to the client.
  • Handle checks client-side — assemble the full streamed response on the client and run your own output validation after the stream completes.
  • Use input-only guardrails with streaming — input guardrails always run regardless of streaming mode, so you still get protection on the prompt side.
No. System prompts are excluded from guardrails by default — the AI Gateway strips them before sending content to any guardrail, so they are never inspected, blocked, or redacted.The one exception is CrowdStrike AIDR, which sees the system prompt for analysis but never modifies it.