Skip to main content

Enforce Mode — Active Runtime Control

Enforce mode moves from observation to action. Budget caps, tool call limits, and compliance rules become hard constraints that shape agent behavior in real time.
1

Switch to enforce

import cascadeflow

cascadeflow.init(mode="enforce")  # Changed from "observe" to "enforce"
2

Add a budget cap

Wrap any block of agent work with cascadeflow.run() and set a budget:
cascadeflow.init(mode="enforce")

with cascadeflow.run(budget=0.50) as session:
    result = await agent.run("Research and summarize this topic")

    summary = session.summary()
    print(f"Cost: ${summary['cost_total']:.4f}")
    print(f"Budget remaining: ${summary['budget_remaining']:.4f}")
    print(f"Steps completed: {summary['steps']}")
If the agent exceeds $0.50, cascadeflow issues a stop action. The agent halts cleanly — no runaway spend.
3

Combine budget with tool call limits

with cascadeflow.run(budget=1.00, max_tool_calls=5) as session:
    result = await agent.run("Search and analyze this dataset")
    # Stops when either budget OR tool call limit is hit first
4

Add compliance gating

Restrict which models can process sensitive data:
with cascadeflow.run(budget=1.00, compliance="gdpr") as session:
    result = await agent.run("Process EU customer feedback")
    # Only GDPR-approved models are allowed — non-compliant models are switched
5

See what happened

The decision trace shows every enforcement action:
for record in session.trace():
    print(f"Step {record['step']}: {record['action']}{record['reason']}")
    # Step 1: allow — budget ok, compliance passed
    # Step 3: switch_model — model not in GDPR allowlist
    # Step 7: stop — budget exceeded ($0.50/$0.50)
In enforce mode, record['applied'] is True — actions are executed, not just logged.

Gradual Rollout

You do not need to jump from observe to full enforcement. Start with one constraint:
# Week 1: Just budget — see if anything would stop
cascadeflow.init(mode="enforce")
with cascadeflow.run(budget=5.00) as session:  # Generous cap
    ...

# Week 2: Add tool call limits
with cascadeflow.run(budget=2.00, max_tool_calls=20) as session:
    ...

# Week 3: Add compliance
with cascadeflow.run(budget=1.00, max_tool_calls=10, compliance="gdpr") as session:
    ...

TypeScript

import { CascadeAgent } from '@cascadeflow/core';

const agent = new CascadeAgent({
  models: [
    { name: 'gpt-4o-mini', provider: 'openai', cost: 0.000375 },
    { name: 'gpt-4o', provider: 'openai', cost: 0.00625 },
  ],
  quality: {
    threshold: 0.8,
    useSemanticValidation: true,
  },
});

const result = await agent.run('Explain quantum computing');
console.log(`Model: ${result.modelUsed}, Saved: ${result.savingsPercentage}%`);

Next Step

Ready to attach policy to individual agents? Use the @agent decorator →