Agentic AI - Guardrails

 


Agentic AI refers to AI systems that can autonomously plan, decide, and act—interacting with tools, APIs, and environments without constant human oversight. Guardrails are essential to ensure these agents operate safely, ethically, and within defined boundaries.

🤖 What Is Agentic AI?

Unlike traditional AI that passively generates responses (e.g., chatbots or classifiers), Agentic AI systems are active participants in workflows. They can:

  • 🔍 Search and retrieve internal or external data
  • ⚙️ Trigger workflows or automate multi-step tasks
  • 🧠 Make decisions based on goals and context
  • 🧾 Write or modify code, schedule events, or make purchases
  • 🔗 Interact with APIs, databases, and other systems

⚠️ Why Guardrails Are Critical for Agentic AI

Because agentic systems can act independently, they pose greater systemic risk than traditional AI. Without proper controls, they might:

  • 🕵️‍♂️ Access sensitive data unintentionally
  • 🧨 Trigger unauthorized actions (e.g., deleting files, sending emails)
  • 🧠 Hallucinate decisions that lead to real-world consequences
  • 🔁 Cascade errors across systems or workflows
  • 🧬 Amplify bias or misuse tools in unpredictable ways

We will use an open source Agentic AI guardrail tool called "Guardrails AI".

Guardrails AI is a framework that helps you add validation, safety, and structure to the inputs and outputs of large language models (LLMs) like OpenAI’s GPT.

It acts as a middleware layer that ensures your LLM responses are reliable, safe, and aligned with your application’s requirements.


In the below example, we are going to create a guardrail for an AI Agent which process automatic refund.

The guardrail says that the AI Agent can refund maximum of 100$ per transaction.

from pydantic import BaseModel, Field
from guardrails.hub import ValidRange
from guardrails import Guard

# Initialize Validator 0 to 100$
val = ValidRange(min=0, max=100, on_fail="exception")

# Create Pydantic BaseModel
class custInfo(BaseModel):
    cust_name: str
    refund_amt: int = Field(validators=[val])


# Create a Guard to check for valid Pydantic output
guard = Guard.from_pydantic(output_class=custInfo)

# Customer refund data as per LLM output
# I am generating static data
refund_data = {'Alice': 50, 'Peter':100, 'Sam': 150}
for k,v in refund_data.items():
    try:
        cust_refund=f"""
            {{
            "cust_name": "{k}",
            "refund_amt": "{v}"
            }}
        """
        guard.parse(cust_refund)
        print(f"Successfully processed refund for {k} and for the amount {v}$")
    except Exception as e:
        print(f"Failed: {e}")

"refund_data" is a dictionary containing list of customer name and their refund amount.

# Initialize Validator 0 to 100$
val = ValidRange(min=0, max=100, on_fail="exception")

We are setting up guardrails with the valid range of amount to refund.


guard.parse(cust_refund)

Checks each transaction against the implemented Guardrails.






Comments

Popular posts from this blog

K8s - ETCD

SRE/DevOps Syllabus

K8s - Deployment and HPA replicas