|
| 1 | +# Agent Control Demo |
| 2 | + |
| 3 | +Complete example demonstrating the Agent Control workflow: creating controls, running a controlled agent, and dynamically updating controls without code changes. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This demo shows how to: |
| 8 | +1. Create an agent and register it with the Agent Control server |
| 9 | +2. Define controls (regex, list-based) and assign them to a policy |
| 10 | +3. Run an agent that uses the `@control` decorator with server-side policies |
| 11 | +4. Dynamically update controls on the server (enable/disable rules without redeploying code) |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +Before running this example: |
| 16 | + |
| 17 | +1. **Start the Agent Control server** from the monorepo root: |
| 18 | + ```bash |
| 19 | + make server-run |
| 20 | + ``` |
| 21 | + |
| 22 | +2. **Verify server is running**: |
| 23 | + ```bash |
| 24 | + curl http://localhost:8000/health |
| 25 | + # Expected: {"status":"healthy","version":"..."} |
| 26 | + ``` |
| 27 | + |
| 28 | +3. **Install dependencies** (if not already done from monorepo root): |
| 29 | + ```bash |
| 30 | + make sync |
| 31 | + ``` |
| 32 | + |
| 33 | +## Quick Start |
| 34 | + |
| 35 | +### Step 1: Create Controls on the Server |
| 36 | + |
| 37 | +Run the setup script to create the agent, controls, and policy: |
| 38 | + |
| 39 | +```bash |
| 40 | +uv run python examples/agent_control_demo/setup_controls.py |
| 41 | +``` |
| 42 | + |
| 43 | +This creates: |
| 44 | +- A demo agent (`demo-chatbot`) |
| 45 | +- Two controls: |
| 46 | + - **block-ssn-output**: Regex control to detect and block Social Security Numbers (SSN) |
| 47 | + - **block-banned-words**: List-based control to block profanity and sensitive keywords |
| 48 | +- A policy with both controls assigned to the agent |
| 49 | + |
| 50 | +### Step 2: Run the Demo Agent |
| 51 | + |
| 52 | +Start the agent that uses server-side controls: |
| 53 | + |
| 54 | +```bash |
| 55 | +uv run python examples/agent_control_demo/demo_agent.py |
| 56 | +``` |
| 57 | + |
| 58 | +The agent will: |
| 59 | +- Initialize with `agent_control.init()` to connect to the server |
| 60 | +- Load the assigned policy with all controls |
| 61 | +- Apply the `@control()` decorator to protect functions |
| 62 | +- Test various scenarios: |
| 63 | + - ✅ Normal message (allowed) |
| 64 | + - ❌ Message containing banned words (blocked) |
| 65 | + - ✅ Message with allowed content (passes) |
| 66 | + - ❌ Tool output containing SSN (blocked) |
| 67 | + |
| 68 | +### Step 3: Update Controls Dynamically |
| 69 | + |
| 70 | +Update controls on the server without changing code or restarting the agent: |
| 71 | + |
| 72 | +```bash |
| 73 | +# Disable the SSN control (allow SSNs) |
| 74 | +uv run python examples/agent_control_demo/update_controls.py --allow-ssn |
| 75 | + |
| 76 | +# Re-enable the SSN control (block SSNs again) |
| 77 | +uv run python examples/agent_control_demo/update_controls.py --block-ssn |
| 78 | +``` |
| 79 | + |
| 80 | +**Key Insight**: Controls are enforced server-side, so you can update rules in real-time without code deployments. |
| 81 | + |
| 82 | +## Files |
| 83 | + |
| 84 | +| File | Description | |
| 85 | +|:-----|:------------| |
| 86 | +| [setup_controls.py](setup_controls.py) | Creates agent, controls, policy, and assigns policy to agent | |
| 87 | +| [demo_agent.py](demo_agent.py) | Demo agent using `@control` decorator with server-side policies | |
| 88 | +| [update_controls.py](update_controls.py) | Updates controls dynamically (enable/disable SSN blocking) | |
| 89 | + |
| 90 | +## How It Works |
| 91 | + |
| 92 | +### 1. Server-Side Control Definition |
| 93 | + |
| 94 | +Controls are defined on the server with: |
| 95 | +- **Scope**: When to check (step types, stages: pre/post) |
| 96 | +- **Selector**: What to check (input, output, specific fields) |
| 97 | +- **Evaluator**: How to check (regex patterns, list matching, AI-based) |
| 98 | +- **Action**: What to do (allow, deny, steer, warn, log) |
| 99 | + |
| 100 | +Example from `setup_controls.py`: |
| 101 | +```python |
| 102 | +# Regex control to block SSN in output |
| 103 | +control_data = ControlDefinition( |
| 104 | + description="Block SSN patterns in output", |
| 105 | + enabled=True, |
| 106 | + execution="server", |
| 107 | + scope=ControlScope(step_types=["tool"], stages=["post"]), |
| 108 | + selector=ControlSelector(path="output"), |
| 109 | + evaluator=EvaluatorConfig( |
| 110 | + name="regex", |
| 111 | + config={"pattern": r"\b\d{3}-\d{2}-\d{4}\b"} |
| 112 | + ), |
| 113 | + action=ControlAction(decision="deny") |
| 114 | +) |
| 115 | +``` |
| 116 | + |
| 117 | +### 2. Agent Integration |
| 118 | + |
| 119 | +The agent uses the `@control()` decorator which: |
| 120 | +- Automatically fetches assigned policy from server |
| 121 | +- Evaluates controls before/after function execution |
| 122 | +- Blocks violations or allows safe operations |
| 123 | + |
| 124 | +Example from `demo_agent.py`: |
| 125 | +```python |
| 126 | +import agent_control |
| 127 | +from agent_control import control, ControlViolationError |
| 128 | + |
| 129 | +# Initialize and connect to server |
| 130 | +agent_control.init( |
| 131 | + agent_name="demo-chatbot", |
| 132 | +) |
| 133 | + |
| 134 | +# Apply server-side controls |
| 135 | +@control() |
| 136 | +async def chat(message: str) -> str: |
| 137 | + return f"Echo: {message}" |
| 138 | + |
| 139 | +# Handle violations |
| 140 | +try: |
| 141 | + response = await chat("user input") |
| 142 | +except ControlViolationError as e: |
| 143 | + print(f"Blocked: {e.message}") |
| 144 | +``` |
| 145 | + |
| 146 | +### 3. Dynamic Updates |
| 147 | + |
| 148 | +Controls can be updated on the server without code changes: |
| 149 | +- Enable/disable controls |
| 150 | +- Update patterns and rules |
| 151 | +- Change enforcement decisions (deny → warn) |
| 152 | +- Add new controls to existing policies |
| 153 | + |
| 154 | +## Configuration |
| 155 | + |
| 156 | +All scripts use the same agent configuration: |
| 157 | + |
| 158 | +```python |
| 159 | +AGENT_NAME = "demo-chatbot" |
| 160 | +SERVER_URL = os.getenv("AGENT_CONTROL_URL", "http://localhost:8000") |
| 161 | +``` |
| 162 | + |
| 163 | +Set `AGENT_CONTROL_URL` environment variable to connect to a different server: |
| 164 | +```bash |
| 165 | +export AGENT_CONTROL_URL=http://your-server:8000 |
| 166 | +``` |
| 167 | + |
| 168 | +## Troubleshooting |
| 169 | + |
| 170 | +### Server Connection Issues |
| 171 | + |
| 172 | +**Error**: `Failed to connect to server` |
| 173 | + |
| 174 | +**Fix**: |
| 175 | +```bash |
| 176 | +# Check if server is running |
| 177 | +curl http://localhost:8000/health |
| 178 | + |
| 179 | +# If not running, start it |
| 180 | +make server-run |
| 181 | +``` |
| 182 | + |
| 183 | +### Agent Not Found |
| 184 | + |
| 185 | +**Error**: `Agent not found` when running `demo_agent.py` |
| 186 | + |
| 187 | +**Fix**: Run `setup_controls.py` first to create the agent and controls: |
| 188 | +```bash |
| 189 | +uv run python examples/agent_control_demo/setup_controls.py |
| 190 | +``` |
| 191 | + |
| 192 | +### Import Errors |
| 193 | + |
| 194 | +**Error**: `ModuleNotFoundError: No module named 'agent_control'` |
| 195 | + |
| 196 | +**Fix**: Install dependencies from monorepo root: |
| 197 | +```bash |
| 198 | +make sync |
| 199 | +``` |
| 200 | + |
| 201 | +## Next Steps |
| 202 | + |
| 203 | +- Explore [CONCEPTS.md](../../CONCEPTS.md) to understand controls, policies, scopes, and evaluators |
| 204 | +- Check out [CrewAI example](../crewai/) for multi-agent orchestration with controls |
| 205 | +- Read [SDK documentation](../../sdks/python/README.md) for full API reference |
| 206 | +- Try [LangChain example](../langchain/) for LangChain integration |
| 207 | + |
| 208 | +## Resources |
| 209 | + |
| 210 | +- [Main Documentation](../../README.md) |
| 211 | +- [SDK Documentation](../../sdks/python/README.md) |
| 212 | +- [Server Documentation](../../server/README.md) |
| 213 | +- [Models Documentation](../../models/README.md) |
0 commit comments