How to Detect Behavioral Anomalies in LangChain Agents
Not all attacks trigger pattern-based detection. Sophisticated attacks cause agents to behave subtly differently — using tools slightly more than usual, accessing data in unusual patterns, or producing responses with different characteristics. Behavioral anomaly detection catches what pattern matching misses.
Why LangChain Is Vulnerable to Behavioral Anomaly
LangChain agents develop consistent behavioral patterns in production — typical tool call frequencies, response lengths, and data access patterns. When an agent is compromised, these patterns change. Without baseline monitoring, subtle behavioral shifts go unnoticed until significant damage is done.
Attack Scenarios
Gradual Behavioral Shift
The agent's behavior changes subtly over time as injection instructions accumulate in its context. Individual interactions look normal, but the aggregate pattern deviates from baseline.
No single obvious payload — the attack works through accumulated context manipulation across many turns.
Tool Usage Pattern Change
A compromised agent starts calling tools in different proportions — reading more sensitive data, making more external calls, or using tools at unusual times.
The agent normally calls search 90% and database_query 10%. After compromise, it calls database_query 60% and send_email 30%.
How to Prevent This
Enable behavioral monitoring via Rune dashboard
Rune tracks tool call patterns, response characteristics, and data access frequency for each agent. Deviations from baseline trigger alerts.
from rune import Shield
from rune.integrations.langchain import ShieldMiddleware
shield = Shield(api_key="rune_live_xxx")
middleware = ShieldMiddleware(shield, agent_id="monitored-agent")
agent = create_react_agent(model, tools, middleware=[middleware])
# Rune builds behavioral baselines and alerts on deviations
result = agent.invoke({"input": user_input})Set up alerts for tool usage anomalies
Configure Rune alerts for when agents call tools at unusual frequencies, access data they rarely touch, or produce responses with different characteristics.
Review agent behavior dashboards regularly
Use Rune's dashboard to monitor agent behavior trends. Investigate sudden changes in tool call patterns or response characteristics.
How Rune Detects This
from rune import Shield
from rune.integrations.langchain import ShieldMiddleware
shield = Shield(api_key="rune_live_xxx")
middleware = ShieldMiddleware(shield, agent_id="monitored-agent")
agent = create_react_agent(model, tools, middleware=[middleware])
# Every interaction contributes to behavioral baselines
# Deviations trigger alerts in the Rune dashboard
result = agent.invoke({"input": user_input})What it catches:
- Changes in tool call frequency or distribution
- Unusual data access patterns (accessing data the agent rarely touches)
- Response characteristic changes (length, format, content type)
- Timing anomalies (agent taking much longer or shorter than usual)
Related Guides
Protect your LangChain agents from behavioral anomaly
Add runtime security in under 5 minutes. Free tier includes 10,000 events per month.
Start Free — 10K Events/Month