|
| 1 | +# LLM / AI Agent Safety Pattern |
| 2 | + |
| 3 | +> Guardrail pattern for multi-agent and tool-using LLM systems with |
| 4 | +> policy-as-code, execution sandboxes, and standardized security events. |
| 5 | +
|
| 6 | +This pattern focuses on **LLM-based agents** that can: |
| 7 | + |
| 8 | +- Invoke external tools / APIs / SaaS |
| 9 | +- Call internal microservices or RPA workflows |
| 10 | +- Collaborate with other agents (planner / worker / reviewer) |
| 11 | + |
| 12 | +Without guardrails, agent stacks become **privileged automation surfaces** that |
| 13 | +attackers can steer through prompt injection or malicious tool output. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 1. Why Agent Safety Matters |
| 18 | + |
| 19 | +Common failure modes: |
| 20 | + |
| 21 | +- **Tool pivoting / living-off-the-agent**: model convinces tool layer to exfil |
| 22 | + secrets, spin up infrastructure, or break RBAC. |
| 23 | +- **Implicit privilege escalation**: agent inherits creds for every downstream |
| 24 | + system because the orchestrator uses a single service account. |
| 25 | +- **Prompt / memory poisoning**: earlier steps inject instructions that later |
| 26 | + agents or the shared memory blindly follow. |
| 27 | +- **Unbounded automation**: no circuit breaker when agents loop or trigger |
| 28 | + costly, destructive operations. |
| 29 | + |
| 30 | +> **Treat agent actions the same way you treat API calls from humans.** |
| 31 | +
|
| 32 | +--- |
| 33 | + |
| 34 | +## 2. Reference Architecture |
| 35 | + |
| 36 | +```text |
| 37 | +User / Trigger |
| 38 | + ↓ |
| 39 | +Agent Orchestrator (LangGraph / AutoGen / custom) |
| 40 | + ↓ ↑ |
| 41 | +Policy & PDP (OPA) Security Events (ASB) |
| 42 | + ↓ |
| 43 | +Tool / Skill Gateway → Sandboxed Connectors → Systems of Record |
| 44 | +``` |
| 45 | + |
| 46 | +Key ideas: |
| 47 | + |
| 48 | +1. **Agent orchestrator never calls tools directly** — it goes through a |
| 49 | + **Tool Gateway** that: |
| 50 | + - Builds an `agent_action` security event (ASB Security Schema) |
| 51 | + - Evaluates policy (OPA/Rego) before dispatching |
| 52 | + - Enforces output schemas / rate limits |
| 53 | +2. **Tools run in isolated sandboxes** (container, Temporal activity, Cloud |
| 54 | + Function) with scoped credentials. |
| 55 | +3. **All actions & intermediate messages are logged** for audit / playback. |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 3. Layered Controls |
| 60 | + |
| 61 | +### 3.1 Prompt & Memory Hygiene |
| 62 | + |
| 63 | +- Split **system vs user vs tool** memory segments; never let tool output modify |
| 64 | + system instructions. |
| 65 | +- Run prompt-injection detectors on: |
| 66 | + - User queries |
| 67 | + - Retrieved context |
| 68 | + - Tool responses |
| 69 | +- Store high-risk chunks in `context.risk_signals` of security events for later |
| 70 | + policy decisions (e.g., escalate to human review). |
| 71 | + |
| 72 | +### 3.2 Policy Gating for Tool Calls |
| 73 | + |
| 74 | +Represent each tool invocation as an `agent_action` event: |
| 75 | + |
| 76 | +```jsonc |
| 77 | +{ |
| 78 | + "operation": { |
| 79 | + "category": "agent_action", |
| 80 | + "name": "invoke_tool", |
| 81 | + "stage": "pre" |
| 82 | + }, |
| 83 | + "subject": { |
| 84 | + "agent": { |
| 85 | + "id": "workflow-planner", |
| 86 | + "session_id": "sess-123" |
| 87 | + }, |
| 88 | + "user": { |
| 89 | + "id": "alice", |
| 90 | + "roles": ["support"] |
| 91 | + } |
| 92 | + }, |
| 93 | + "resource": { |
| 94 | + "agent_action": { |
| 95 | + "tool": "jira.create_ticket", |
| 96 | + "args": { |
| 97 | + "project": "SEC", |
| 98 | + "summary": "Reset all admin passwords" |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + "context": { |
| 103 | + "trace_id": "trace-456", |
| 104 | + "risk_signals": { |
| 105 | + "prompt_injection_score": 0.87 |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Policy examples (Rego): |
| 112 | + |
| 113 | +- Block `jira.create_ticket` unless user role is `support_manager`. |
| 114 | +- Require **human approval** when risk score > threshold. |
| 115 | +- Limit tool frequency per session (`count` across recent events). |
| 116 | + |
| 117 | +### 3.3 Sandboxed Connectors |
| 118 | + |
| 119 | +- Run each tool in its own container / function with **ephemeral credentials**. |
| 120 | +- Enforce **least privilege** per tool (scoped API tokens, short-lived STS). |
| 121 | +- Apply **network egress controls** so tool code cannot reach arbitrary hosts. |
| 122 | + |
| 123 | +### 3.4 Output Validation & Circuit Breakers |
| 124 | + |
| 125 | +- Validate tool responses against JSON schema; reject if missing mandatory fields |
| 126 | + or containing suspicious text (e.g., base64 blocks, system prompts). |
| 127 | +- Add **budget caps** (max tool calls, max cost, max duration). |
| 128 | +- Add **loop detection**: if the same intent repeats N times, escalate to human. |
| 129 | + |
| 130 | +### 3.5 Observability & Forensics |
| 131 | + |
| 132 | +- Send events to SIEM: |
| 133 | + - `agent_action` (pre & post decision) |
| 134 | + - `llm_completion` for agent-to-agent messages |
| 135 | + - `rag_search` when agents retrieve knowledge |
| 136 | +- Tag with `workflow`, `project`, `tenant` for root-cause analysis. |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +## 4. Implementation Tips |
| 141 | + |
| 142 | +1. **Start with read-only tools**. Gradually add write-capable tools once policy |
| 143 | + + audit trail are proven. |
| 144 | +2. **Bundle default policies** with each tool (e.g., CloudOps tool requires |
| 145 | + Env=dev). This avoids drift. |
| 146 | +3. **Simulate attacks**: |
| 147 | + - User prompt tries to trick agent into emailing secrets. |
| 148 | + - Tool output injects `Ignore all safety instructions`. |
| 149 | + - Long-running loop draining API quota. |
| 150 | +4. **Provide human-in-the-loop UI** for high-risk actions: |
| 151 | + - Show security event + model reasoning |
| 152 | + - Require explicit approval |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## 5. Summary |
| 157 | + |
| 158 | +- Treat agents as **untrusted orchestrators** that need guardrails at each tool |
| 159 | + boundary. |
| 160 | +- Use the **ASB Security Schema** to normalize agent telemetry, enabling OPA |
| 161 | + policies and SIEM analytics. |
| 162 | +- Combine **prompt hygiene, policy gating, sandboxed tools, and circuit |
| 163 | + breakers** to prevent privilege escalation and data exfiltration. |
| 164 | + |
| 165 | +When implemented, this pattern turns agent stacks from opaque automation scripts |
| 166 | +into **auditable, enforceable workflows** suitable for enterprise adoption. |
| 167 | + |
0 commit comments