Skip to main content
This tutorial adds Anthale to a Python LangChain agent flow. This path guards agent model execution and raises AnthalePolicyViolationError when Anthale blocks the run. If you already have an agent, the change is small: import the middleware, create it, and pass it to create_agent.
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

Add the middleware

Add metadata

This middleware already extracts the LangChain request and response messages it sends to Anthale. 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 AnthaleLangchainMiddleware is sent with every Anthale enforcement request created by that middleware. For guidance on what belongs there, read Metadata & Logs.

What to expect

This integration protects the agent path itself. Use it when you want Anthale around model execution inside the agent, not only around one standalone chat model call.

Verify the integration

  • Send one benign agent request. The run should complete normally through the middleware.
  • Send one request that your policy should block. The agent should raise AnthalePolicyViolationError.
  • If the policy is configured to redact, send content that should be sanitized and confirm the guarded run continues with sanitized content instead of the original value.
The middleware is wired correctly when normal agent runs still complete, blocked runs raise AnthalePolicyViolationError, and redacted runs 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 Chat Model Guard for Python, OpenAI Integration for Python, or How Anthale Works.
Last modified on March 19, 2026