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-sdk

Step-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 result

2. 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 @harness on async agent functions for simplicity
  • Use AsyncAgentTrustClient directly when you need the raw response in middleware
  • Share one client instance across the app lifespan (FastAPI lifespan handler)
  • Always async with or explicitly await client.aclose()

Common Mistakes

  • Using sync AgentTrustClient inside async routes (blocks event loop)
  • Not awaiting client.validate()
  • Creating new async client per request without closing

Troubleshooting

IssueFix
Event loop errorsEnsure all validate calls are awaited
Connection pool exhaustionUse single shared client instance
Same errors as sync clientSee Sync Client troubleshooting