Skip to main content
This example shows how to protect LangChain agents, chains, and tool calls from prompt injection and other attacks.

Overview

LangChain agents are powerful but vulnerable to prompt injection through:
  • User inputs that manipulate agent behavior
  • Malicious content in retrieved documents
  • Tool outputs that contain injection payloads
PromptGuard protects at every step of the agent execution.

Setup

Basic Protection

The simplest approach - one line protects all LLM calls:

Callback Handler (Deeper Integration)

For richer context and per-chain configuration:

Protecting Tool Inputs/Outputs

Tools can be vectors for injection. Wrap them with PromptGuard:

ReAct Agent Example

Conversational Agent with Memory

RAG Agent

Error Handling

Monitoring Agent Security

Best Practices

  1. Use auto-instrumentation - Catches all LLM calls including internal agent reasoning
  2. Enable response scanning - Tool outputs can contain injections
  3. Fail gracefully - Don’t expose error details to users
  4. Monitor blocked requests - Track attack patterns
  5. Test with adversarial inputs - Validate protection before production

Common Attack Patterns Blocked

Next Steps

Python SDK

Full SDK reference

Threat Detection

Detection capabilities