Connect OpenAI Agents SDK to Anoman AI
Swap the base URL on AsyncOpenAI to add guardrails, batch routing, and observability to every agent run. No changes to your agent or tool definitions required.
Prerequisites
- An Anoman AI account with an active API key
- Python 3.9+
- OpenAI Agents SDK installed
Step 1: Install dependencies
pip install openai-agentsStep 2: Configure the client with Anoman
Pass a custom AsyncOpenAI client with Anoman's base URL to set_default_openai_client. All agents created after this call use Anoman automatically.
from openai import AsyncOpenAI
from agents import set_default_openai_client, Agent, Runner
# Point the SDK at Anoman
client = AsyncOpenAI(
api_key="anm-sk-your-key-here",
base_url="https://api.anoman.io/v1",
)
set_default_openai_client(client)Step 3: Define and run agents
Your agent and tool definitions stay exactly as they are. Every LLM call in a run passes through Anoman's guardrail pipeline.
import asyncio
from agents import Agent, Runner
agent = Agent(
name="Research assistant",
instructions="You are a helpful research assistant.",
model="gpt-4o-mini",
)
async def main():
result = await Runner.run(
agent,
"Summarize the key provisions of Indonesia's UU PDP law.",
)
print(result.final_output)
asyncio.run(main())Agents with tools
Tool definitions are unchanged. Anoman's MCP governance layer can apply per-tool allow/deny policies if an MCP policy is attached to your API key.
from agents import Agent, Runner, function_tool
@function_tool
def get_weather(location: str) -> str:
"""Get the current weather for a location."""
return f"The weather in {location} is sunny, 28°C."
agent = Agent(
name="Weather assistant",
instructions="Answer weather questions.",
model="gpt-4o-mini",
tools=[get_weather],
)
async def main():
result = await Runner.run(agent, "What is the weather in Jakarta?")
print(result.final_output)
asyncio.run(main())When a guardrail triggers
If the guardrail pipeline blocks a request, Anoman returns a 403. The SDK raises an openai.APIStatusError which surfaces in Runner.run.
import openai
try:
result = await Runner.run(agent, user_message)
except openai.APIStatusError as e:
if e.status_code == 403:
print("Blocked by guardrail:", e.body)
raiseCheck docs/openapi/ for the current error schema.
Guardrail transparency headers
Every response includes guardrail result headers. Check these in your API logs to verify the pipeline is running on each agent step.
X-Anoman-Guardrail-Injection: pass score=0.06
X-Anoman-Guardrail-Pii: pass
X-Anoman-Guardrail-Content: pass
X-Anoman-Cache: noneNeed help with your integration?
Join the Founding 50 for early access and direct support from the Anoman team.
Join Founding 50