Here, a function blocks the agent from deleting data.
Agentic AI
def guardrail_check(action):
if action == 'delete_data':
returnFalsereturnTrue
agent.set_guardrail_function(guardrail_check)
Sample Model
This simple agent blocks actions listed in guardrails. It allows safe actions and blocks dangerous ones.
Agentic AI
class SimpleAgent:
def __init__(self):
self.guardrails = []
def apply_guardrails(self, rules):
self.guardrails = rules
def run_task(self, action):
if action in self.guardrails:
return f"Action '{action}' blocked by guardrails."return f"Action '{action}' executed successfully."# Define guardrails to block 'delete_files'
guardrails = ['delete_files']
agent = SimpleAgent()
agent.apply_guardrails(guardrails)
# Try actionsprint(agent.run_task('read_files'))
print(agent.run_task('delete_files'))
OutputSuccess
Important Notes
Guardrails are like safety rules for AI agents.
Without guardrails, agents might do unexpected or harmful things.
Guardrails should be clear and tested well before use.
Summary
Guardrails keep AI agents safe by limiting harmful actions.
They are important when AI interacts with people or sensitive data.
Applying guardrails helps build trust and control in AI systems.
Practice
(1/5)
1. Why are guardrails important for AI agents when they interact with people?
easy
A. They make the AI run faster.
B. They help the AI learn without any rules.
C. They allow the AI to ignore user input.
D. They prevent the AI from making harmful or unsafe decisions.
Solution
Step 1: Understand the role of guardrails
Guardrails are safety limits set to stop AI from doing harmful actions.
Step 2: Connect guardrails to interaction with people
When AI talks to people, guardrails keep it from unsafe or harmful choices.
Final Answer:
They prevent the AI from making harmful or unsafe decisions. -> Option D
Quick Check:
Guardrails = prevent harm [OK]
Hint: Guardrails stop bad AI actions with people [OK]
Common Mistakes:
Thinking guardrails speed up AI
Believing guardrails ignore user input
Assuming guardrails remove all rules
2. Which of the following is the correct way to add a guardrail that stops an AI agent from deleting files?
easy
A. delete_file = true
B. allow action == 'delete_file'
C. if action == 'delete_file': block()
D. action = 'delete_file'
Solution
Step 1: Identify guardrail syntax to block actions
The guardrail should check if the action is 'delete_file' and then block it.
Step 2: Compare options for correct blocking
if action == 'delete_file': block() uses a condition and blocks the action, which is correct for a guardrail.
Final Answer:
if action == 'delete_file': block() -> Option C
Quick Check:
Guardrail blocks delete_file = if action == 'delete_file': block() [OK]
Hint: Guardrails use conditions to block bad actions [OK]
Common Mistakes:
Allowing the action instead of blocking
Assigning variables instead of checking conditions
Confusing action names with commands
3. Given this code snippet for an AI agent guardrail:
actions = ['read_data', 'delete_file', 'send_email']
allowed_actions = []
for a in actions:
if a != 'delete_file':
allowed_actions.append(a)
print(allowed_actions)
What will be the output?
medium
A. ['read_data', 'delete_file', 'send_email']
B. ['read_data', 'send_email']
C. ['delete_file']
D. []
Solution
Step 1: Understand the loop and condition
The loop goes through each action and adds it to allowed_actions only if it is not 'delete_file'.
Step 2: Trace the loop with given actions
'read_data' is added, 'delete_file' is skipped, 'send_email' is added.
Final Answer:
['read_data', 'send_email'] -> Option B
Quick Check:
Filtered out 'delete_file' = ['read_data', 'send_email'] [OK]
Hint: Check which actions pass the condition [OK]
Common Mistakes:
Including 'delete_file' by mistake
Empty list if loop misunderstood
Confusing append with replace
4. This AI agent code is meant to block unsafe commands but has a bug: