DS DevShelfHub Projects · AI tools
Guardrails Page 21 of 29

CrewAI Guardrails: Validate Agent Outputs Before They Ship

By DevShelfHub

Enforce output contracts at the task level — fail fast and retry rather than letting bad outputs propagate.

Series progress21 / 29
CrewAI guardrails tutorial — CrewAI Guardrails: Validate Agent Outputs Before They Ship

Callable Guardrails

A plain function that takes a TaskOutput and returns a bool. Return True to accept; False triggers a retry.

python
def has_sources(out):
    return "Sources:" in out.raw

Task(description="Research X", agent=r, guardrail=has_sources, guardrail_max_retries=3)

LLMGuardrail

Declarative: describe what valid output looks like and let an evaluator LLM enforce it. Best for fuzzy criteria like tone, safety, or schema conformance.

python
from crewai import LLMGuardrail

guard = LLMGuardrail(
    description="Output is valid JSON with title and body",
    criteria=[
        "Must be valid JSON",
        "Must contain a title and body key",
        "Title must be under 80 characters",
    ],
)
Task(description="...", guardrail=guard)

API: LLMGuardrail.

Multiple Guardrails

Pass a list via guardrails=[...] — all must pass. guardrail_max_retries caps total retry attempts; default is 3.

Events

The runtime emits LLMGuardrailStartedEvent, LLMGuardrailCompletedEvent, and LLMGuardrailFailedEvent — subscribe via a listener to log failures and tune your criteria.

When to Use Which

  • Callable: cheap, deterministic checks (regex, JSON parse, schema validation).
  • LLMGuardrail: subjective or compound criteria (tone, completeness, safety).
  • Don't use LLMGuardrail for regex — it's wasteful.

Notes

LLM-as-judge is a second billable call

Semantic validators are flexible but add latency and variance. Cache judge prompts, cap rationale length, and downgrade to deterministic checks when the schema is strict.

Warn versus block is a product decision

Blocking saves quality but can stall user flows. Warnings help internal ops but can train users to ignore alerts. Align defaults with the risk tier of the workflow, not only engineering taste.

Order guardrails from cheap to expensive

Run regex and JSON schema checks before invoking heavier LLM validators. Early exits save money and reduce the chance that a flaky judge masks a simple formatting bug.

Retries can amplify unsafe content

Blindly retrying failed generations sometimes walks models into weirder corners. Pair retries with tightened prompts, temperature adjustments, or human review on repeated failures.

CrewAI guardrails FAQ

What is a CrewAI guardrail?

A guardrail is a validation step that inspects agent outputs or tool results before the next step proceeds, returning either success, a warning, or a hard failure.

When should I use LLM-based guardrails?

Use LLM guardrails when rules are fuzzy—tone, policy compliance, or qualitative rubrics— and pair them with cheaper deterministic checks when possible.

Can I chain multiple CrewAI guardrails?

Yes. Chain lightweight deterministic checks first, then escalate to heavier LLM reviews only when necessary to control latency and spend.

How do guardrails interact with retries?

Guardrails can trigger retries with tightened prompts or alternate tools, but cap retry counts so a pathological loop cannot burn your budget.

Are guardrails a replacement for human review?

No. They reduce risk and automate first-pass QA, but high-stakes decisions still belong to humans or audited workflows, especially in regulated domains.

See also: DevShelfHub's CrewAI tool review for a product-level comparison, pricing notes, and links back into this tutorial series.

Quick jump: API Reference