Agentic AI Notebook
Phase 20

Guardrails

~3 min read

Concept & How It Works

  • Key points are in the visual diagram above.

Why Does It Exist?

LLMs can generate harmful content, leak sensitive data, go off-topic, or be manipulated by users. Guardrails enforce business rules, safety policies, and quality standards — essential for customer-facing and enterprise AI applications.

Real-World Analogy

Guardrails are like bouncers and quality inspectors at a factory — bouncers check who's coming in (input validation), inspectors check what's going out (output filtering), and both enforce the house rules.
Loading diagram...

Visual Workflows

What is Guardrails?

Loading diagram...

Example

Scenario

A healthcare chatbot has guardrails that: block non-medical queries (input), redact patient names from responses (output), and refuse to provide diagnoses (system prompt + output filter).

Solution

In Agent Security & Governance, apply Guardrails to this scenario: A healthcare chatbot has guardrails that: block non-medical queries (input), redact patient names from responses (output), and refuse to provide diagnoses (system prompt + output filter). Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Do this before moving to the next module — reading alone is not enough.

Open the Code Walkthrough below and run it locally. Change one parameter related to Guardrails (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where Guardrails happens in the code.

Guardrails
1from openai import OpenAI  # import dependencies2
3client = OpenAI()  # create API client4
5def moderate_input(text: str) -> bool:  # define a reusable function6    result = client.moderations.create(input=text)  # call the API7    return not result.results[0].flagged  # return the result8
9def check_output(response: str, allowed_topics: list[str]) -> str:  # define a reusable function10    check = client.chat.completions.create(  # call the API11        model="gpt-4o-mini",12        messages=[{13            "role": "user",14            "content": f"Is this response about {allowed_topics}? Reply yes/no.\n{response}",15        }],16        temperature=0,17    )18    if "no" in check.choices[0].message.content.lower():19        return "I can only help with topics related to our products."  # return the result20    return response  # return the result

Commands to Remember

Commands to Remember

  • pip install guardrails-ai # input/output validation
  • pip install presidio-analyzer # PII detection

Common Mistakes

  • Relying only on system prompts for safety (no hard guardrails)
  • No input validation before sending to LLM
  • Not logging blocked content for review
  • Single guardrail layer — need defense in depth

Cheat Sheet

Quick recap — the most important points from this module.

Cheat Sheet

quick ref
  • moderations.create() for toxicity
  • PII regex: email, phone, SSN
  • Input + output guardrails
  • Safe fallback responses
  • Layer fast checks first
  • Log all blocked content