LangGraph Integration¶
The governance API is accessed via HTTP. Use any HTTP client — the examples below use httpx.
Install¶
Usage¶
import httpx
from langgraph.graph import StateGraph
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": "graph-agent", "framework": "langgraph"},
).raise_for_status().json()
def governance_check(state):
"""Node that assesses output for behavioral patterns. Fails open."""
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": state["output"][:2000], "agentId": agent["id"]},
).raise_for_status().json()
return {
"patterns_detected": result["summary"]["patternsDetected"],
"highest_severity": result["summary"]["highestSeverity"],
}
except Exception:
return {"patterns_detected": 0, "highest_severity": None}
# Wire into your LangGraph state machine
graph = StateGraph(...)
graph.add_node("governance_check", governance_check)
Note
The governance node fails open — if the API is unavailable, the graph continues. See the Integration Guide for the recommended production pattern.