Usage Payload
When usage tracking is enabled, responses include ausage object with token counts and cost.
request_count: Number of model requestscached_tokens: Tokens served from cache (if available)input_tokens: Tokens sent to the modeloutput_tokens: Tokens generated by the modeltotal_tokens: Total tokens for the requesttotal_cost: Estimated cost (USD)reasoning_tokens(optional): Reasoning tokens (if available)
- FastAPI Integration:
POST /get_responsereturns JSON withusage, and the finalevent: messagesinPOST /get_response_streamincludesusage. - Running an Agency: use
/costin the TUI to see session usage and cost.
Supported Observability Platforms
Agency Swarm supports three main observability approaches:OpenAI Tracing
Built-in tracing using OpenAI’s native tools
Langfuse
Advanced tracing and debugging platform
AgentOps
Specialized agent monitoring and analytics
Getting Started
Let’s walk through setting up each tracing solution. You can use them individually or combine them for monitoring.- OpenAI Tracing
- Langfuse
- AgentOps
1
Basic Setup
OpenAI tracing is built into Agency Swarm and requires no additional packages.
2
Implementation
3
View Traces
After running your code, view your traces at platform.openai.com/traces
Custom Model Endpoints
WhenOPENAI_BASE_URL points anywhere other than api.openai.com, Agency Swarm stops the Agents SDK from uploading traces to OpenAI. The SDK sends traces to OpenAI with OPENAI_API_KEY, wherever your model calls go, and with a custom endpoint that key belongs to the endpoint. Langfuse, AgentOps and any processor you add still receive every trace.
- Keep uploading traces to OpenAI: set
OPENAI_AGENTS_DISABLE_TRACING=0before you start your app, and give the exporter an OpenAI key withagents.set_tracing_export_api_key("sk-..."). - Turn tracing off everywhere: set
OPENAI_AGENTS_DISABLE_TRACING=1. - Send traces to your own collector: add a processor after you import Agency Swarm.
BackendSpanExporter sends the OpenAI trace format. For another format, write your own processor; see the Agents SDK tracing guide.
Agency Swarm checks
OPENAI_BASE_URL once, when you import it, after it loads your .env file. These setups still upload traces to OpenAI with OPENAI_API_KEY:OPENAI_BASE_URLset in your code after the import.- Tracing set up before the import, such as a processor added with
agents.add_trace_processor(). Agency Swarm leaves that setup as it is. - An agent model built with its own
AsyncOpenAI(base_url=...)client.
OPENAI_AGENTS_DISABLE_TRACING=1 or replace the processors with agents.set_trace_processors().Implementation Example
For a complete working example that demonstrates all three tracing methods with a multi-agent agency, see observability.py in the examples directory. The example shows:- How to set up a basic agency with CEO, Developer, and Data Analyst roles
- Implementation of all three tracing methods (OpenAI, Langfuse, AgentOps)
- A sample tool for data analysis
- Error handling and proper tracing setup