Embedded Gateway
Auto-Instrumentation Overview
Zero-code instrumentation for existing agent codebases.
Auto-Instrumentation Overview
Overview
auto_instrument() monkey-patches popular AI SDKs at runtime to automatically send validations to AgentTrust — no decorator or client code required. Supported targets: OpenAI SDK, LangChain, LangGraph.
Why It Matters
The lowest-friction integration for existing codebases. Add two lines at startup and all LLM calls are governed.
Prerequisites
pip install agentrust-sdk
# Plus your framework: openai, langchain, langgraphStep-by-Step Guide
1. Call before framework imports (recommended)
from agentrust_sdk import auto_instrument
auto_instrument() # must run before openai/langchain imports
import openai
# ... rest of app2. Alternative: install() / auto_install()
from agentrust_sdk.auto import install
install() # patches OpenAI, Anthropic, LangChain callbacks3. Disable auto-instrumentation
export AGENTRUST_AUTO_INSTRUMENT=false4. What gets patched
| Library | Patched methods |
|---|---|
| OpenAI | Chat completions, responses |
| LangChain | LCEL chains, legacy chains |
| LangGraph | Graph invocations |
Integration pattern A
From examples/README.md — Pattern A is auto-instrument for zero application code change.
Examples
from agentrust_sdk import auto_instrument, embed_gateway
embed_gateway()
auto_instrument()
# Existing code unchanged:
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
)
# ↑ automatically validated by AgentTrustSee examples/minimal.py and examples/openai_direct.py.
Best Practices
- Call
auto_instrument()as early as possible inmain()or__init__.py - Set
AGENTRUST_AUTOLOAD_AGENT_IDor configure agent ID via env for meaningful audit records - Combine with
embed_gateway()for local dev - Use
@harnessinstead when you need explicit agent boundaries
Common Mistakes
- Importing OpenAI before
auto_instrument()(patch may not apply) - Expecting auto-instrument to set meaningful
agent_id(defaults to generic ID) - Using auto-instrument with Team-tier graph adapters (use
AgentTrustNodeinstead)
Troubleshooting
| Issue | Fix |
|---|---|
| Calls not validated | Confirm AGENTRUST_ENABLED=true; check import order |
| Double validation | Don't combine auto-instrument + @harness on same path |
| LangGraph not patched | Ensure langgraph is installed; check logs |