AutoGen
Track every AutoGen team run with latency, tool call data, and failure details. AgentMetricsRunStream wraps AutoGen's run_stream(). Your event handling code stays the same.
Requirements
- Python 3.10 or later
autogen-agentchat0.4 or later
1. Install
pip install agentmetrics-autogen
2. Wrap run_stream
Replace team.run_stream(...) with tracker.run(team, ...). The async context manager and event loop stay the same.
import os
import asyncio
from agentmetrics_autogen import AgentMetricsRunStream
tracker = AgentMetricsRunStream(
api_key=os.environ["AGENTMETRICS_API_KEY"],
agent_id="my-autogen-team",
base_url="http://localhost:8099",
)
async def main():
async with tracker.run(team, task="summarize this document") as stream:
async for event in stream:
print(event)
asyncio.run(main())
3. Flush before exit (optional)
For short-lived scripts, call flush() to wait for all in-flight requests to complete before the process exits.
tracker.flush()
What gets tracked
AgentMetricsRunStream intercepts AutoGen's event stream and extracts:
| Event type | What is captured |
|---|---|
ModelCallEvent / LLMCallEvent | LLM call count, input and output tokens (where available) |
ToolCallRequestEvent | Tool call count, tool names |
ToolCallExecutionEvent (is_error) | Tool error count |
TaskResult | Run complete signal, stop reason |
A run summary is emitted when the async with block exits: on normal completion, on exception, or on cancellation.
Token availability
AutoGen does not guarantee token counts in all streaming event types. Token counts are captured when present in ModelCallEvent or LLMCallEvent, but may be zero or partial depending on your AutoGen version and team configuration.
Cancelled runs
If the task is cancelled via asyncio.CancelledError, AgentMetrics records the run with status "cancelled" before re-raising, so cancelled runs appear in the dashboard.
Configuration
| Parameter | Default | Description |
|---|---|---|
api_key | required | Your AgentMetrics API key |
agent_id | "autogen-agent" | Label shown in the dashboard |
base_url | "http://localhost:8099" | AgentMetrics backend URL |