Production & reliability
Guardrails layer
An input/output safety layer: PII redaction, jailbreak and prompt-injection detection, output schema validation, and moderation.
Copy or download the full plan and paste it into your AI coding agent to build it.
Why build it
A raw LLM will happily leak personal data, follow a malicious instruction hidden in its input, return malformed JSON that crashes your app, or produce unsafe content. In production you never send user text straight to a model or model output straight to a user. A guardrails layer (checks that run on the way in and on the way out) redacts PII, blocks jailbreaks and prompt injection, validates the output against a schema, and moderates content. Every company deploying AI needs this, and knowing how to build it shows you can ship AI safely, which is often the difference between a project that launches and one that gets blocked.
Who it's for: You have an LLM app going near real users or real data and need it to be safe and well-formed. You want to prove you can ship AI responsibly, not just make it work.
What you'll build
Core (MVP)
- Input PII detection and redaction before text reaches the model
- Jailbreak and prompt-injection detection on user input
- Output validation against a strict schema, with a repair or reject path
- Content moderation on the model's output
- A configurable policy: block, redact, or flag per rule
- A log of every check, what it caught, and the action taken
Stretch
- A model-based classifier for subtle injection attempts
- Per-tenant policies and allow/deny lists
- A human-review queue for flagged (not blocked) items
- Streaming-safe output checks that catch violations mid-stream
- A small red-team test set that the layer must pass
Step-by-step build
- 1
Define the policy
List the rules you will enforce and the action for each: redact PII, block injection, validate schema, moderate output. Make the policy a config so rules can be turned on, off, or set to flag-only per deployment. This policy is the contract the rest of the layer implements.
- 2
Redact PII on input
Before any text reaches the model, run it through Presidio or regex rules to find emails, phone numbers, and other identifiers, and replace them with placeholders. Keep a reversible map only if you genuinely need to restore values later. Nothing sensitive should reach the model that does not have to.
- 3
Detect jailbreaks and injection
Screen user input for prompt-injection and jailbreak patterns, starting with known phrasings and escalating to a free LLM-based classifier for subtle cases. Prompt injection is when input text tries to override your system instructions, so treat retrieved and user content as untrusted. Block or flag per policy.
- 4
Call the model safely
Only after input checks pass do you call the model, with a hardened system prompt that reasserts its rules. Pass the sanitized, redacted input, never the raw text. This keeps the model boundary clean.
- 5
Validate the output
Parse the model's output against a strict Pydantic or Zod schema. On a mismatch, attempt one automatic repair pass and reject if it still fails, rather than passing malformed data downstream. Well-formed output is a safety property, not just a nicety.
- 6
Moderate the output
Run the validated output through a moderation check for unsafe content before it reaches the user. Apply the policy action: block, redact, or flag for review. This is the last gate between the model and the person.
- 7
Log every decision
Write an audit record for each request: which checks ran, what they caught, and the action taken. Store it in Postgres or Atlas so you can prove what the layer did and tune the rules. This log is essential for both debugging and compliance.
- 8
Red-team, deploy, document
Build a small test set of PII leaks, injection attempts, and malformed responses, and confirm the layer catches all of them. Deploy the service in front of a real app and write a README covering the policy, the checks, and the red-team results. Show a blocked attack, because that is the proof.
Done when
- ✓PII in the input is redacted before it ever reaches the model.
- ✓A known prompt-injection or jailbreak attempt is blocked or flagged per policy.
- ✓Malformed model output is repaired or rejected, never passed downstream.
- ✓Every request has an audit log row showing the checks and the action taken.
Ship it
Run the guardrails layer as a FastAPI or NestJS service in front of your app on the AWS free tier, with the audit log in Postgres or Atlas. Put a real app behind it and run your red-team test set against it. Put the policy, the list of checks, and the red-team results in the README, and show a blocked attack.
What it proves: You can ship AI safely: redacting PII, blocking injection, validating output, and moderating content with a full audit trail. That safety layer is what lets an AI project actually launch.
Hand it to your AI agent
Paste this into Cursor, Claude, or ChatGPT and build it step by step.
You are my senior AI engineer pair. Help me build "Guardrails layer" step by step. Stack: FastAPI or NestJS as the guardrails service wrapping every model call, Groq free tier for the app model and a free LLM-based check for subtle cases, Presidio or regex for PII detection, Pydantic or Zod for output schema validation, and Postgres or MongoDB Atlas (free M0) for the audit log. Guardrails are checks that run on the way in and on the way out of the model. Prompt injection is when input text tries to override my system instructions, so I treat all user and retrieved text as untrusted. Requirements: 1. A config-driven policy where each rule can block, redact, or flag. 2. Input PII redaction before text reaches the model. 3. Jailbreak and prompt-injection detection on input, from known patterns up to a free LLM classifier. 4. Strict output schema validation with one repair attempt, then reject. 5. Output moderation, plus an audit log of every check and action, and a small red-team test set the layer must pass. Work in this order: policy, PII redaction, injection detection, safe model call, output validation, moderation, logging, then red-team. STOP after each step so I can test. Do not write the whole app at once.
More in Production & reliability
LLM gateway
One proxy in front of several LLM providers that handles routing, retries, fallback, caching, and cost tracking.
Eval harness
A systematic way to score LLM outputs against a test set so you catch regressions before shipping a prompt or model change.
Tracing dashboard
Observability for LLM apps: log every model call and agent step with latency, tokens, and cost, and view the full trace.
Building this? I post a new AI project plan on LinkedIn most weeks. Follow along and share what you ship.