AI guardrails: how every answer gets checked

Guardrails are automatic checks that sit around an AI model, like seatbelts and traffic rules. They check what goes in (is this request allowed and safe?) and what comes out (is the answer in the right format, complete, on topic and safe to share?). If an answer fails, the guardrail can fix it, ask the AI again, or block it.

Everyday analogy

A quality inspector at the end of a factory line. Every product is checked against a specification; faulty items are repaired, sent back to be remade or thrown away, and only good ones reach the customer.

Example

A travel company's chatbot must return a booking summary as JSON with a date, a price and a city. One reply forgets the price. The guardrail spots it and re-asks the model, which then returns a complete answer. Another reply contains a customer's passport number, so the guardrail filters it out before anyone sees it.

🙋Request🛡️Input checksinjection, PII,topic🤖LLMraw answer🛡️Output checksformat, facts,safety✅Pass → replyfail → re-ask,fix, block
Guardrails check what goes in and what comes out, and decide what to do when a check fails.

Without guardrails, AI can…

  • Go off-topic or give nonsense
  • Share unsafe or harmful advice
  • Leak personal or secret data
  • Produce the wrong format and break your app

Three places to put guardrails

  • Input: block prompt injection, personal data and off-topic requests
  • Output: check format, facts against sources, safety and tone
  • Actions: allow-lists of tools, limits and human approval for risky steps

How a guard works (the Guardrails AI library, for example)

  1. Choose a specification: describe the expected output, e.g. a Pydantic model, a RAIL spec or simple string rules.
  2. Attach validators: small checks such as "valid JSON", "no personal data", "no competitor names".
  3. Call the guard instead of the model; behind the scenes it calls the LLM.
  4. Validate the raw answer against every check, and log the result.
  5. Pass → return the output. Fail → run the on-fail action.
On-fail actionWhat it does
Re-askSend the answer back to the AI with the error, and ask it to try again
FixRepair it automatically, e.g. trim to the allowed length
FilterRemove the bad part and keep the rest
RefrainReturn nothing rather than a bad answer
No-opLet it through, but record the failure for review
ExceptionStop and raise an error so a person or the app can handle it

Popular guardrail tools: Guardrails AI, NVIDIA NeMo Guardrails, Meta Llama Guard, Azure AI Content Safety and Amazon Bedrock Guardrails. Guardrails reduce risk but never remove it, so combine them with testing, monitoring and human review for important decisions.

Try it yourself

About 15 min

Write guardrails for a chatbot

  1. Imagine a school's homework-help chatbot.
  2. Write 3 input rules (what it must refuse) and 3 output rules (what every answer must or must not contain).
  3. For each rule, choose an on-fail action: re-ask, fix, filter or refrain.

What you should see: A clear, testable checklist, which is how real guardrails start.

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.

Build it for real · guided projectCode an AI research assistant that uses tools (a ReAct agent)About 90 min · Copy-paste code · step by step
Test what you learned

Take a quick quiz, or look up any word in the glossary.