Skip to main content
Open In Colab — run a live agent blocked mid-task (needs your own OpenAI key). LangChain calls registered tools through its callback system. CertiorCallbackHandler taps that system and runs Guard.verify(...) before each tool execution. Allowed calls proceed; blocked calls raise CertiorBlocked and the chain stops.

Wiring it in

What the handler does

CertiorCallbackHandler(guard, capabilities=...) subscribes to LangChain’s on_tool_start event. For each invocation:
  1. Resolves the tool’s name and inputs.
  2. Calls guard.verify(tool=name, params=inputs, content=..., required_capabilities=capabilities[name], cost_cents=costs[name]).
  3. On allow: lets LangChain proceed with the original inputs (or the redacted version when the policy redacts).
  4. On block: raises CertiorBlocked carrying the VerifyResult. The chain halts.

Declaring capabilities per tool

LangChain’s callback does not carry capability metadata, so the handler can’t infer what a tool needs — you declare it once in the capabilities map above. A tool whose needs are not a subset of the guard’s grant is blocked before it runs; without a map, only the content gate applies. If you’d rather keep the declaration next to the function, wrap it with @guard.wrap instead — the capability check then runs inside the tool body:
Use both together for defence in depth: the wrap raises CertiorBlocked synchronously inside the tool body; the callback enforces the same check at the framework boundary.

See also