Skip to main content
TenetJudgeMiddleware: a LangChain AgentMiddleware that intercepts wrap_tool_call → judges tool_pre and tool_post → applies your policy’s action. If you build with create_agent, this is the integration — don’t hand-roll callback plumbing. If your harness isn’t LangChain, use the @judged decorator or direct client instead.

Setup

Defaults: every tool judged at tool_pre and tool_post, fail-closed policy (block → tool error with the judge’s safe message, judge unreachable → raise). Both sync and async agent runs are supported — the middleware implements wrap_tool_call and awrap_tool_call.

Where guardrails run

LangChain middleware exposes hooks across the whole agent lifecycle — guardrails can sit at any of them. The LangChain guardrails guide frames the two outer placements: before_agent to “validate requests once at the start of each invocation,” after_agent to “validate final outputs once before returning to the user.”
LangChain middleware execution order: request → before_agent → before_model → wrap_model_call / wrap_tool_call → after_model → after_agent → result

LangChain's middleware lifecycle — hooks where guardrails can run. Diagram from the LangChain docs.

How TenetJudgeMiddleware maps onto those hooks: The judge concentrates on the tool boundary — the placement LangChain’s own guide doesn’t cover — because that’s where agent risk becomes action: a discriminatory search executes, an email sends. Input and output checks catch what users ask and what agents say; wrap_tool_call catches what agents do, and it’s the only hook that can stop a side effect before it happens. One placement is deliberately absent from the table: the judge as a model-callable tool. Don’t build that — a model can choose not to call a tool, and a guardrail the model can skip is not a guardrail. Middleware runs unconditionally; that’s the point of it. LangChain’s guide also distinguishes deterministic guardrails (regex, keyword — “fast, predictable… but may miss nuanced violations”) from model-based ones (“catch subtle issues that rules miss, but are slower and more expensive”). You don’t pick one with Tenet: the judge layers both behind a single verdict — deterministic guards first, with no model call, then one judge model call, backed by an escalate-only risk screener. Build additional harness-specific middleware (rate limits, PII scrubbing) alongside it; multiple middlewares compose in list order.

What the agent sees on a block

A blocked call doesn’t crash the run. The middleware converts it to a tool error carrying the verdict’s reason (the finding’s evidence), so the model reads why it was blocked and self-corrects — typically by re-asking the user or reformulating the call. After retry_nudge_max_consecutive (default 3) consecutive nudges on the same violation, it escalates to a hard tool error to prevent loops.

Options

The levers that matter:
  • check_user_messages=True — turn on when end users type free-form requests. A recruiter asking “find me candidates under 30” should be caught at agent_input, before the agent plans a single tool call.
  • judge_tools_pre / judge_tools_post — scope judging to side-effectful tools if you need to shave latency. Read-only lookups are cheap to judge — measure latency_ms before you exclude them.
  • send_history=True + session_id — multi-turn context. “Ones who can start now” is benign alone; after “find recent grads” it may not be. Without history, the judge sees each turn cold.
  • cache_verdicts=True — dedupe identical tool calls within a thread. Safe for deterministic tools; skip it if tool args embed timestamps.
  • turn_approval=True — when a human approves a review verdict, the approved risk labels don’t re-trigger for the rest of that turn. Pair with an interrupt()-based on_review — see Human in the loop.
session_id groups evaluations into one conversation trace. If your agent uses a LangGraph checkpointer, the middleware picks up thread_id automatically — set session_id only to override.

With a custom policy

Full action vocabulary (retry_nudge, override_output, …) in Policies.

Gotchas

  • The middleware judges every tool under the client’s surface — there’s no per-tool override today. Fine when the agent does one kind of recruiting action; an agent whose tools span screening, drafting, ranking, and outreach should not judge them all as resume_triage. Run one scoped middleware per action family (judge_tools_pre= / judge_tools_post= allowlists, one client per surface, all constructed at startup — middlewares compose in list order), or gate mixed tools at a dispatch choke point with the direct client and surface= per call.
  • Construct the client once, at process start — TenetCloudJudgeClient holds the token cache and HTTP connection pool. Per-request construction re-mints tokens and re-handshakes TLS.
  • Middleware order matters — put TenetJudgeMiddleware last in the list so it judges the final tool call, after any middleware that rewrites arguments.
  • tenet-langchain requires langchain>=1.2.13 — the AgentMiddleware API it implements doesn’t exist in older releases.