Deploying agents in enterprise environments requires strict safety boundaries. Guardrails ensure that an agent cannot execute unauthorized actions or leak sensitive data via prompt injection.
Core Guardrail Strategies
- Input Validation: Scanning user prompts for jailbreak attempts before they reach the reasoning agent.
- Output Verification: Using a secondary LLM as a "Judge" to verify the agent's plan before execution.
- Human-in-the-Loop (HITL): High-risk actions (e.g., executing SQL, sending emails) must pause and require human approval.
def execute_action(action):
if action.risk_level == 'HIGH':
approval = request_human_approval(action)
if not approval:
return "Action denied by operator."
return run_tool(action)Prompt Injection: Direct vs. Indirect
Direct prompt injection is a user typing "ignore previous instructions" into a chat box. Indirect prompt injection is more dangerous: malicious instructions embedded in a web page, PDF, or email that an agent retrieves as part of a tool call, then unknowingly executes as if they came from the operator.
Layered Defense: Guardrails at Every Stage
| Layer | Technique | What It Catches |
|---|---|---|
| Input | Classifier / regex scan for jailbreak patterns | Direct prompt injection, known attack templates |
| Planning | Constrained tool schemas, allow-listed actions | Agent attempting an action outside its authorized scope |
| Execution | Sandboxing, rate limits, HITL approval gates | Destructive or high-cost actions before they run |
| Output | LLM-as-judge, PII redaction, citation checking | Hallucinated claims, leaked secrets, unsupported answers |
Reducing Hallucination in Agent Outputs
Hallucination in an agentic context is worse than in a chatbot, because the agent may act on its own fabricated belief — calling a nonexistent API, or citing a policy that doesn't exist to justify a decision.
- Grounding: Force the agent to cite the specific tool output or document chunk that supports each claim; reject ungrounded assertions at the verification layer.
- Constrained Decoding: For structured actions (tool calls, JSON output), use schema-constrained generation so the model cannot emit a malformed or invented function name.
- Self-Consistency Checks: Sample the same reasoning step multiple times and flag disagreement as a signal the model is uncertain rather than confidently wrong.
Defining Risk Tiers
Not every action needs a human in the loop — that would defeat the purpose of automation. Effective guardrail design starts with classifying actions by blast radius: read-only operations proceed automatically, reversible writes proceed with logging and post-hoc review, and irreversible or high-cost actions (financial transactions, production deploys, external communications) require explicit approval before execution.