Custom Agents (Python / Node)

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.

Overview

This integration connects Custom Agents (Python / Node) with Corules's policy enforcement runtime. Your existing Custom Agents (Python / Node) workflows call Corules's REST API (or MCP server) to enforce policy constraints before any business action completes.

All policy logic stays in Corules — your Custom Agents (Python / Node) configuration only needs to call the endpoint and route based on the ALLOW / BLOCK / ESCALATE response.

Setup steps

  1. 1

    Install guardrail-sdk: pip install guardrail-sdk (Python) or npm install @guardrail/sdk (Node)

  2. 2

    Initialize client with tenant_id and API key from environment variables

  3. 3

    Before agent reasoning: call client.get_constraints(use_case, actor, context) — pass constraints to agent system prompt

  4. 4

    After agent produces decision: call client.validate(use_case, actor, context, decision) before execution

  5. 5

    Handle outcomes: ALLOW → execute, ESCALATE → route to human review queue, BLOCK → return error to agent

Code example

The following snippet shows how to call Corules from Custom Agents (Python / Node). Replace YOUR_USE_CASE_ID and YOUR_API_KEY with your tenant credentials.

# 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)

Language: python

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.

Connect Custom Agents (Python / Node) to Corules

Start free — integration templates are available on every plan.

Get started free