Skip to main content
Four ways to protect LangChain in Python. For LangChain.js, the callback handler is in the Node.js SDK guide.

Auto-instrumentation

LangChain’s chat models call the provider SDKs underneath, so auto-instrumentation scans them with no LangChain-specific code. ChatOpenAI goes through the openai package; ChatAnthropic through anthropic.

Inline guard in a chain

PromptGuardRunnable is a step you compose into an LCEL chain with |. Because it sits in the data flow, it can do what a callback cannot: on a redact decision it passes the redacted text on to the next step. On a block it raises PromptGuardBlockedError.
A string, a dict with an input, query, question, text, content or prompt field, or a list of message dicts can be redacted in place. For any other value, a redact decision blocks in enforce mode rather than pass on content it could not rewrite. mode="monitor" logs instead of blocking or redacting.

Callback handler

PromptGuardCallbackHandler scans each model call’s prompt before it is sent and, unless you pass scan_responses=False, its response afterwards. It needs promptguard-sdk 2.4.0 or later: earlier releases failed under LangChain’s callback manager.
Pass it in config={"callbacks": [...]} on invoke() to cover every model call in a chain or agent run. A callback observes content but cannot change it, so in enforce mode a redact decision blocks; use the inline guard above when you want the redacted text passed on. A thread_id, session_id or conversation_id in the run’s metadata is sent as the session id.

Proxy

Point the chat model at the proxy instead. Nothing runs in your process, and it works the same in any language.

Next steps

LangChain agents cookbook

Scan an agent’s prompts, tool calls and chains

LangGraph

Scan the model calls inside a graph

Auto-instrumentation

Which provider calls init() patches, and which it does not
Last modified on October 31, 2026