SDK
Async Client
AsyncAgentTrustClient for async frameworks and high-concurrency workloads.
Async Client
Overview
AsyncAgentTrustClient provides the same validation API as AgentTrustClient for async Python applications — FastAPI, asyncio agents, and async LangChain/LangGraph flows.
Why It Matters
Async web services and agent loops must not block the event loop on governance HTTP calls. The async client uses httpx.AsyncClient under the hood.
Prerequisites
pip install agentrust-sdkStep-by-Step Guide
1. Basic async validation
from agentrust_sdk import AsyncAgentTrustClient
async def govern_output():
async with AsyncAgentTrustClient() as client:
result = await client.validate(
agent_id="async-agent",
user="alice",
input="query",
output={"answer": "response"},
)
return result2. With FastAPI
from fastapi import FastAPI
from agentrust_sdk import AsyncAgentTrustClient, harness
app = FastAPI()
client = AsyncAgentTrustClient()
@app.post("/validate")
async def validate_endpoint(body: dict):
result = await client.validate(
agent_id="api-agent",
user=body["user"],
input=body["input"],
output=body["output"],
)
return {"outcome": result.decision.outcome, "envelope_id": result.envelope_id}3. Async decorator
The @harness decorator automatically handles async functions:
from agentrust_sdk import harness
@harness
async def async_agent(user: str, input: str) -> dict:
return {"result": await llm_call(input)}Examples
Concurrent validations:
import asyncio
from agentrust_sdk import AsyncAgentTrustClient
async def validate_batch(outputs):
async with AsyncAgentTrustClient() as client:
tasks = [
client.validate(agent_id="batch", user="sys", input="", output=o)
for o in outputs
]
return await asyncio.gather(*tasks)Best Practices
- Prefer
@harnesson async agent functions for simplicity - Use
AsyncAgentTrustClientdirectly when you need the raw response in middleware - Share one client instance across the app lifespan (FastAPI
lifespanhandler) - Always
async withor explicitlyawait client.aclose()
Common Mistakes
- Using sync
AgentTrustClientinside async routes (blocks event loop) - Not awaiting
client.validate() - Creating new async client per request without closing
Troubleshooting
| Issue | Fix |
|---|---|
| Event loop errors | Ensure all validate calls are awaited |
| Connection pool exhaustion | Use single shared client instance |
| Same errors as sync client | See Sync Client troubleshooting |