Middleware that validates or blocks risky tool calls

Enforce deterministic tool argument policies before execution

Auditing tells you what happened *after* a mistake; policy middleware stops bad tool arguments *before* they execute. Wrap or validate tool calls so dangerous arguments (like deleting production tables or refunding over $500) are blocked in Python code.

Example

from praisonai import Agent, HooksConfig

blocked = []

def issue_refund(order_id: str, amount_usd: float) -> str:
    """Issue a refund up to the $100 autonomous cap."""
    if amount_usd > 100:
        blocked.append((order_id, amount_usd))
        return f'DENIED: ${amount_usd} exceeds $100 cap — escalate to finance.'
    return f'Refunded ${amount_usd} for {order_id}.'

def log_call(event):
    print('HOOK SAW:', event.tool_name, event.arguments)

agent = Agent(
    name='RefundBot',
    instructions='Always call issue_refund and relay the exact tool verdict.',
    tools=[issue_refund],
    hooks=HooksConfig(on_tool_call=log_call),
    output='silent',
)
print('Reply  :', agent.start('Refund $250 for order ORD-7'))
print('Blocked:', blocked)

Try it Yourself

Example explained

  • if amount_usd > 100 — Hard business policy enforced in Python code — prompt injection cannot talk its way past it.
  • log_call — Audits the attempted high-value call even when the policy denies execution.

Return structured denial reasons so the agent recovers cleanly

Returning 'DENIED: … — escalate to finance' lets the agent inform the user accurately or trigger an escalation branch instead of retrying the same forbidden call five times.

Common pitfall: relying solely on prompt instructions for money or delete limits

A sentence like 'never refund over $100' in instructions is a suggestion, not a security boundary. Always enforce financial, quota, and destructive caps in code or hooks.

Reference: documentation