Skip to main content
JudgePolicy: verdict → JudgeAction. The judge decides what the risk is; your policy decides what to do about it. The SDK ships safe defaults — block becomes a tool error, review proceeds with a tenet_review marker until you wire a HITL pause, unavailable raises — and every handler is overridable per tool and per phase.

The shape

Each handler receives a JudgeContext (phase, tool_name, tool_input, tool_output, session_id, metadata) and returns a JudgeAction.

Actions

retry_nudge is the action that makes guardrails feel like a collaborator instead of a wall: the model gets told what to change (“remove the age constraint and re-run the search”), retries, and usually succeeds. retry_nudge_from_verdict(verdict) builds the guidance from the judge’s own findings. The middleware escalates to a hard tool_error after 3 consecutive nudges (configurable) so a stubborn model can’t loop.

A production policy

per_tool and per_phase are full nested policies — the most specific handler wins.

Fail open vs. fail closed

The tradeoff: fail-closed protects compliance at the cost of availability; fail-open inverts it. Don’t pick one globally — pick per tool:
  • Side effects (send, write, post): fail closed. A missed email is recoverable; a discriminatory one is not.
  • Read-only lookups: fail open is defensible — but tag the action’s metadata so skipped evaluations are visible in your logs, not silent.
  • Never fail open on agent_output if the response reaches an end user.

Human in the loop

A review verdict is a recommendation, not a pause. To actually stop and ask a human, your on_review callback suspends the run itself — in a LangGraph harness, with interrupt():
The middleware caches the verdict before dispatching your callback, so the interrupt() exception doesn’t lose it — on resume, the same verdict is reused instead of re-judging. Pair with turn_approval=True so one approval covers repeat hits in the same turn.
Two requirements for interrupt()-based review: your agent needs a checkpointer (resume requires persisted graph state — an in-memory checkpointer loses paused turns on restart, so don’t ship that to multi-worker production), and resume requests must carry the same thread_id as the paused turn.
The full wiring — rendering the review over SSE, reading the judge’s streamed context inside the callback, resume semantics — is in the HITL guide.

on_caveat

Reserved — the current judge doesn’t return a top-level include_with_caveats verdict or the caveat field. The default handler allows, and a caveat is never dispatched through on_review.

on_unresolved

With resolution_detection on (the middleware default), the judge classifies whether a tool’s output actually resolved the request. on_unresolved fires when it didn’t — the hook for “the search returned nothing because the only compliant interpretation was empty,” where you may want to inform the user rather than let the agent spin.