Skip to main content
This tutorial wraps one LangChain chat model with Anthale instead of a full agent middleware stack. This integration raises AnthalePolicyViolationError when Anthale blocks the guarded model call. If you already have a LangChain chat model, the change is small: import the wrapper and wrap the model.
Before you start, create an Anthale API key and a policy, and make sure you already have an OpenAI API key. Use Create an API Key for the Anthale key and First Policy for the policy setup.

Install

Guard the chat model

Add metadata

This integration already extracts the LangChain request and response messages from the wrapped model call. Use metadata only for application context Anthale cannot infer on its own, such as tenant, user, conversation, or request ID. The keys below are examples. metadata can contain any fields your team uses for tracing and investigation.
The metadata object you pass to guard_chat_model is sent with every Anthale enforcement request created by that wrapper. For guidance on what belongs there, read Metadata & Logs.

What to expect

Use guard_chat_model when your LangChain flow is a runnable or chat model and you do not need agent middleware around tools. Anthale guards both the input and output path for the wrapped model.

Verify the integration

  • Send one benign model call. The wrapped model should complete normally.
  • Send one request that your policy should block. The wrapped model should raise AnthalePolicyViolationError.
  • If the policy is configured to redact, send content that should be sanitized and confirm the wrapped model continues with sanitized content instead of the original value.
The wrapper is wired correctly when normal model calls still complete, blocked calls raise AnthalePolicyViolationError, and redacted calls continue with sanitized content.

anthale-python

Python SDK source, integrations, and examples.

anthale-openapi

OpenAPI contract used to generate Anthale API clients.

Next steps

Next, compare this with LangChain Agent Middleware for Python, OpenAI Integration for Python, or Prompt injection protection.
Last modified on March 19, 2026