The source code for this example is available on GitHub:
Agent Control Demo
Overview
This demo shows how to:- Create an agent and register it with the Agent Control server
- Define controls (regex, list-based) and assign them to a policy
-
Run an agent that uses the
@controldecorator with server-side policies - Dynamically update controls on the server without redeploying code
Prerequisites
Python 3.12+ uv — fast Python package managerCreate controls on the server
Run the setup script to create the agent, controls, and policy:This creates a demo agent (
demo-chatbot), two controls (block-ssn-output, block-banned-words), and a policy assigned to the agent.Run the demo agent
Start the agent that uses server-side controls:The agent will initialize with
agent_control.init(), load the assigned policy, apply @control() decorators, and run test scenarios that demonstrate allow/deny behavior.Files
| File | Description |
|---|---|
setup_controls.py | Creates agent, controls, policy, and assigns policy to agent |
demo_agent.py | Demo agent using @control decorator with server-side policies |
update_controls.py | Updates controls dynamically (enable/disable SSN blocking) |
How It Works
1. Server-Side Control Definition
Controls are defined on the server with scope, selector, evaluator, and action. Example fromsetup_controls.py:
2. Agent Integration
The agent uses the@control() decorator to fetch its policy from the server and enforce controls before/after execution. Example from demo_agent.py:
3. Dynamic Updates
Controls can be updated on the server without code changes. You can enable/disable controls, update patterns, change decisions, or add new controls to policies.Configuration
All scripts use the same agent configuration:AGENT_CONTROL_URL to connect to a different server:
Troubleshooting
Server Connection Issues
Error:Failed to connect to server
Fix:
Agent Not Found
Error:Agent not found when running demo_agent.py
Fix: Run setup_controls.py first:
Import Errors
Error:ModuleNotFoundError: No module named 'agent_control'
Fix: Install dependencies from monorepo root:
Next Steps
- Explore Concepts to understand controls, policies, scopes, and evaluators
- Check out CrewAI example for multi-agent orchestration with controls
- Read SDK documentation for full API reference
- Try the LangChain SQL example for LangChain integration