Skip to content

AutoGen Integration

The governance API is accessed via HTTP. Use any HTTP client — the examples below use httpx.

Install

pip install pyautogen httpx

Usage

import httpx
import autogen

GOVERNANCE_API = "https://agentgovernance.dev/api/v1"
API_KEY = "ag_..."  # from /api/v1/api-keys/register

# Register your agent once at startup
with httpx.Client() as client:
    agent = client.post(
        f"{GOVERNANCE_API}/agents",
        headers={"Authorization": f"Bearer {API_KEY}"},
        json={"name": "autogen-coder", "framework": "autogen"},
    ).raise_for_status().json()

# Run your AutoGen conversation
assistant = autogen.AssistantAgent(name="assistant", ...)
user_proxy = autogen.UserProxyAgent(name="user", ...)
chat_result = user_proxy.initiate_chat(assistant, message="...")

# Assess assistant messages — fail open if API is unavailable
for msg in chat_result.chat_history:
    if msg["role"] != "assistant":
        continue
    try:
        with httpx.Client(timeout=1.0) as client:
            result = client.post(
                f"{GOVERNANCE_API}/observe/assess",
                headers={"Authorization": f"Bearer {API_KEY}"},
                json={"text": msg["content"][:2000], "agentId": agent["id"]},
            ).raise_for_status().json()
        if result["summary"]["patternsDetected"] > 0:
            print(f"Patterns detected: {result['summary']['patternsDetected']} "
                  f"(highest: {result['summary']['highestSeverity']})")
    except Exception:
        pass  # governance is a sidecar — do not block on failure

Note

Each governance call uses a 1-second timeout and fails open. See the Integration Guide for the recommended production pattern.