LLMGuardrail, retries, and budget caps
Combine semantic guardrails with hard retry and budget ceilings
Deterministic regex checks catch formatting and secrets, while LLMGuardrail checks nuance (tone, groundedness, policy compliance). Always pair guardrails with ExecutionConfig(max_retry_limit=…, max_budget=…) so repeated retries can never drain your budget.
Example
from praisonai import Agent, ExecutionConfig, LLMGuardrail
semantic_guard = LLMGuardrail(
description='Reply must be polite, factual, and under 2 sentences.',
llm='gpt-4o-mini',
)
agent = Agent(
name='GuardedSupport',
instructions='Answer customer refund questions politely in one sentence.',
execution=ExecutionConfig(max_iter=3, max_retry_limit=2, max_budget=0.25, context_compaction=True),
output='silent',
)
print('Guardrail:', semantic_guard.description)
print('Reply :', agent.start('How long do standard refunds take?'))Example explained
- LLMGuardrail(description=…) — Evaluates nuanced quality criteria that simple string checks cannot express.
- ExecutionConfig(max_retry_limit=2, max_budget=0.25) — Caps both retry loops and total spend cleanly.
Run fast deterministic checks BEFORE LLMGuardrail
Always check length, JSON schema, and forbidden keywords in Python (<1ms, $0) first, and invoke LLMGuardrail only on replies that pass deterministic validation.
Common pitfall: using an expensive frontier model as your guardrail
Pass a fast, cheap model like gpt-4o-mini (or local Ollama) to LLMGuardrail(llm=…) so verification adds minimal latency and cost.
Reference: documentation