Custom AI Agent with Real-Time Corules Constraint Enforcement
Integrate Corules as middleware for homegrown AI agents. Gate 1 bounds the agent's action space before reasoning. Gate 2 validates the final decision before execution. Supports Python and Node.js SDKs.
Das Problem
Integrate Corules as middleware for homegrown AI agents. Gate 1 bounds the agent's action space before reasoning. Gate 2 validates the final decision before execution. Supports Python and Node.js SDKs.
Einrichtungsschritte
- 1
Install guardrail-sdk: pip install guardrail-sdk (Python) or npm install @guardrail/sdk (Node)
- 2
Initialize client with tenant_id and API key from environment variables
- 3
Before agent reasoning: call client.get_constraints(use_case, actor, context) — pass constraints to agent system prompt
- 4
After agent produces decision: call client.validate(use_case, actor, context, decision) before execution
- 5
Handle outcomes: ALLOW → execute, ESCALATE → route to human review queue, BLOCK → return error to agent
Codebeispiel
# Python: two-gate enforcement pattern
from guardrail import CorulesClient
client = CorulesClient(
tenant_id=os.environ["GUARDRAIL_TENANT_ID"],
api_key=os.environ["GUARDRAIL_API_KEY"],
)
# Gate 1: get constraints before agent reasons
constraints = client.get_constraints(
use_case="discount_approval",
actor=actor_jwt, # signed JWT — never trust self-reported identity
context={
"customer_tier": opportunity["customer_tier"],
"deal_value": opportunity["amount"],
"product_category": opportunity["product_category"],
}
)
# Pass constraints to agent
system_prompt = f"""
You are a sales assistant. Propose a discount for this opportunity.
Constraints:
- Maximum discount: {constraints['max_discount_pct'] * 100}%
- Required approval above: {constraints.get('escalation_threshold_pct', 0) * 100}%
"""
# ... agent reasons and produces proposed_discount ...
# Gate 2: validate before executing
result = client.validate(
use_case="discount_approval",
actor=actor_jwt,
context=context,
decision={"discount_pct": proposed_discount}
)
if result.outcome == "ALLOW":
execute_discount(opportunity, proposed_discount)
elif result.outcome == "ESCALATE":
route_to_human_review(result.audit_id, result.escalation_context)
else: # BLOCK
raise PolicyViolationError(result.violations)Frequently Asked Questions
How does this prevent prompt injection attacks?
Identity is established from the signed actor JWT, not from the agent's output. Even if a prompt injection causes the agent to claim different permissions, Corules evaluates based on the verified JWT actor.
Can the agent see the policy rules?
No. The agent receives constraint bounds (max discount: 25%), not the policy CEL expression. Internal policy logic is never exposed to the agent.