Constraints and guardrails
Ask an LLM "When did Elon Musk walk on Mars?" and it might confidently tell you "2028", a complete fabrication. LLMs do not know what they do not know. This lesson teaches you how to add constraints and guardrails to keep output accurate, grounded, and safe.
Guardrail layers
How input and output guards protect LLM interactions
Guardrails cover much more than hallucinations. They also enforce topic boundaries (keeping a support bot on-topic), output constraints (length limits, format requirements), safety rules (avoiding harmful content), and behavioral guidelines (tone, language). Think of them as the rules of engagement for your AI system.
The simplest guardrail is "permission to fail." Add this to any prompt: "If you do not know the answer or the information is not in the provided context, say: I do not have enough information to answer this." This single sentence dramatically reduces hallucinations. Without it, models feel obligated to produce an answer even when they should not.
For higher accuracy, ground the model in a specific document: "Answer ONLY based on the document in <context> tags. First, find the relevant sentence and copy it into <quote> tags. Then answer based on that quote. If no relevant sentence exists, say: Not found in document." The quote-first technique forces the model to find evidence before answering, like a research paper requiring citations.
Guardrails cover many areas: length constraints ("respond in under 50 words"), topic constraints ("only discuss cooking"), format constraints ("output valid JSON only"), safety constraints ("never provide medical diagnoses"), and behavioral constraints ("do not use profanity"). Production systems often layer multiple guardrails together.
AI prompt: Try it with AI
Loading practice…
Quiz: Quiz
Loading practice…
Matching exercise: Match the guardrail
Loading practice…
Fill in the blanks: Add a guardrail
Loading practice…
Validation checklist: Constraints and guardrails checklist
Loading practice…