Zero-Code Setup
sitecustomize.py and sidecar patterns for governance without code changes.
Overview
Configure AgentTrust governance without modifying application source code using environment variables, PYTHONSTARTUP, and autoload. Ideal for third-party tools, legacy systems, and containerized deployments.
Why It Matters
Some environments prohibit code changes. Zero-code setup injects governance at the Python interpreter level.
Prerequisites
pip install "agentrust-py[embedded,autoload]"Step-by-Step Guide
Option 1: PYTHONSTARTUP + env vars
export AGENTRUST_ENABLED=true
export AGENTRUST_AUTOLOAD_EMBED=true
export AGENTRUST_AUTOLOAD_AGENT_ID=my-agent
export AGENTRUST_AUTO_INSTRUMENT=true
export PYTHONSTARTUP=$(python -c "import agentrust_sdk, os; print(os.path.join(os.path.dirname(agentrust_sdk.__file__), 'autoload.py'))")
python your_app.pyOption 2: sitecustomize.py
Place in your virtualenv's site-packages or PYTHONPATH:
agentrust_sdk.autoload has no bootstrap() function — instrumentation runs as a
side effect of importing the module. Import it and nothing else:
# sitecustomize.py
import agentrust_sdk.autoload # noqa: F401 — patching happens on importThe module already honours AGENTRUST_ENABLED=false internally and never raises: its
whole body is wrapped in a try/except that logs and continues, so it cannot break the host
application. Do not wrap it in your own try: ... except ImportError: pass — that
silently hides a missing install and leaves you with no governance and no warning.
Option 3: Docker/K8s env injection
env:
- name: AGENTRUST_ENABLED
value: "true"
- name: AGENTRUST_GATEWAY_URL
value: "http://localhost:8765"
- name: AGENTRUST_AUTOLOAD_EMBED
value: "true"
- name: AGENTRUST_AUTOLOAD_AGENT_ID
value: "payment-agent"Rollback
export AGENTRUST_ENABLED=false
# No code change; restart processExamples
Sidecar pattern (K8s):
# App container: AGENTRUST_GATEWAY_URL=http://localhost:8765
# Sidecar: embedded or full gateway on port 8765Best Practices
- Document env vars in your deployment runbook
- Test zero-code setup in staging before production
- Use meaningful
AGENTRUST_AUTOLOAD_AGENT_IDvalues - Monitor with
agentrust statusafter deploy
Common Mistakes
- Forgetting
[embedded]extra when usingAGENTRUST_AUTOLOAD_EMBED - PYTHONSTARTUP not executing in some IDE debug configurations
- Zero-code setup in production without gateway monitoring
Troubleshooting
| Issue | Fix |
|---|---|
| No governance activity | agentrust status; check AGENTRUST_ENABLED |
| PYTHONSTARTUP ignored | Use -S flag check; some embedders skip it |
| Wrong agent ID in audit | Set AGENTRUST_AUTOLOAD_AGENT_ID explicitly |