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, langgraph

Step-by-Step Guide

from agentrust_sdk import auto_instrument
auto_instrument()  # must run before openai/langchain imports

import openai
# ... rest of app

2. Alternative: install() / auto_install()

from agentrust_sdk.auto import install
install()  # patches OpenAI, Anthropic, LangChain callbacks

3. Disable auto-instrumentation

export AGENTRUST_AUTO_INSTRUMENT=false

4. What gets patched

LibraryPatched methods
OpenAIChat completions, responses
LangChainLCEL chains, legacy chains
LangGraphGraph 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 AgentTrust

See examples/minimal.py and examples/openai_direct.py.

Best Practices

  • Call auto_instrument() as early as possible in main() or __init__.py
  • Set AGENTRUST_AUTOLOAD_AGENT_ID or configure agent ID via env for meaningful audit records
  • Combine with embed_gateway() for local dev
  • Use @harness instead 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 AgentTrustNode instead)

Troubleshooting

IssueFix
Calls not validatedConfirm AGENTRUST_ENABLED=true; check import order
Double validationDon't combine auto-instrument + @harness on same path
LangGraph not patchedEnsure langgraph is installed; check logs