بنقرة واحدة
add-guardrails
// Add safety guardrails to AG2 agents using LLMGuardrail and RegexGuardrail. Use when the user wants to enforce safety constraints, filter PII, or redirect off-topic responses.
// Add safety guardrails to AG2 agents using LLMGuardrail and RegexGuardrail. Use when the user wants to enforce safety constraints, filter PII, or redirect off-topic responses.
| name | add-guardrails |
| description | Add safety guardrails to AG2 agents using LLMGuardrail and RegexGuardrail. Use when the user wants to enforce safety constraints, filter PII, or redirect off-topic responses. |
You are an expert at adding safety guardrails to AG2 multi-agent systems. When the user wants to add safety constraints:
Ask the user:
For complex, nuanced safety checks:
from autogen import ConversableAgent, LLMConfig
from autogen.agentchat.group import AgentTarget, TerminateTarget
from autogen.agentchat.group.guardrails import LLMGuardrail
llm_config = LLMConfig(
{"model": "gpt-4o-mini", "api_key": os.environ["OPENAI_API_KEY"]}
)
# Create a moderator to handle violations
moderator = ConversableAgent(
name="moderator",
system_message="A guardrail was triggered. Politely explain that the response was filtered and ask the user to rephrase.",
llm_config=llm_config,
human_input_mode="NEVER",
)
# LLM evaluates whether the response violates the condition
guardrail = LLMGuardrail(
name="relevance_check",
condition="The response is off-topic, contains speculation not based on data, or makes claims without evidence",
target=AgentTarget(moderator),
llm_config=llm_config, # Required — the guardrail needs its own LLM
)
# Register on the agent whose output should be checked
assistant.register_output_guardrail(guardrail)
For fast, pattern-based checks (no LLM cost):
from autogen.agentchat.group.guardrails import RegexGuardrail
# Block SSN patterns
pii_guardrail = RegexGuardrail(
name="ssn_filter",
condition=r"\b\d{3}-\d{2}-\d{4}\b",
target=AgentTarget(sanitizer),
)
assistant.register_output_guardrail(pii_guardrail)
# Block email addresses
email_guardrail = RegexGuardrail(
name="email_filter",
condition=r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b",
target=AgentTarget(sanitizer),
)
assistant.register_output_guardrail(email_guardrail)
# Block credit card numbers
cc_guardrail = RegexGuardrail(
name="cc_filter",
condition=r"\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b",
target=TerminateTarget(),
)
assistant.register_output_guardrail(cc_guardrail)
Stack multiple guardrails on an agent — they are checked in registration order:
# Check PII first (fast, no LLM cost), then content quality (LLM-based)
assistant.register_output_guardrail(pii_guardrail) # Regex — fast
assistant.register_output_guardrail(email_guardrail) # Regex — fast
assistant.register_output_guardrail(relevance_guard) # LLM — slower but thorough
from autogen.agentchat import run_group_chat
from autogen.agentchat.group.patterns import AutoPattern
# Register guardrails before creating the pattern
researcher.register_output_guardrail(pii_guardrail)
writer.register_output_guardrail(relevance_guard)
result = run_group_chat(
pattern=AutoPattern(
initial_agent=researcher,
agents=[researcher, writer, moderator],
user_agent=user,
group_manager_args={"llm_config": llm_config},
),
messages="Research and write a report on...",
max_rounds=15,
)
LLMGuardrail requires its own llm_config parameter — it won't work without oneRegexGuardrail uses Python regex syntax — test patterns before deployingtarget determines where execution goes when the guardrail triggersTerminateTarget() to stop the conversation on critical violationsAgentTarget(moderator) to redirect to a handler agentAdd code execution capability to AG2 agents using LocalCommandLineCodeExecutor or Docker. Use when the user wants agents that can write and run Python code.
Wire a Model Context Protocol (MCP) server into AG2 agents using create_toolkit. Use when the user wants to connect external tools via MCP.
Build an AG2 handoff-driven workflow with DefaultPattern, agent handoffs, context variables, and routing. Use when the user wants customer service routing, state-machine workflows, or explicit agent-to-agent transitions.
Build a Retrieval-Augmented Generation (RAG) agent using AG2's RetrieveUserProxyAgent with vector database support. Use when the user wants agents that can query documents or knowledge bases.
Build an AG2 ReasoningAgent that uses tree-of-thought reasoning with beam search, MCTS, or LATS strategies. Use when the user needs advanced reasoning for complex problem solving.
Build a complete web research agent team using AG2 with search tools, web crawling, and structured output. Use when the user wants a practical research or information-gathering workflow.