← Documentation

Securing LangChain Agents

Use TrustGate to govern all LangChain agent traffic with a single line of code.

Install

pip install trustgate

Configure

Set your API key and patch your runtime:

import os
import trustgate

os.environ["TRUSTGATE_API_KEY"] = "tg_..."
trustgate.patch_all()

Use

Your existing LangChain code works unchanged. All requests are routed through TrustGate for cost control, PII masking, and audit logs.

from trustgate import Client
client = Client(api_key="tg_sk_...")

# 3. Execute request with Agentic Context
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are an autonomous refund agent."},
        {"role": "user", "content": "Process refund for order 9921"}
    ],
    # Inject TrustGate Agentic Context via headers
    extra_headers={
        "X-Agent-Trace-Id": "workflow_refund_9921", # Groups multi-step calls into one trace
        "X-Agent-Policy": "strict_retry_block",     # Prevents infinite hallucination loops
        "X-Vault-Decrypt": "true"                   # Required for PII redaction
    }
)

print(f"Trace Logged: {response.headers.get('X-TrustGate-Trace-Status')}")

Next steps