Anthropic Managed Agents
Track every Anthropic Managed Agents session with cost, latency, token usage, and tool call data. AgentMetricsSessionTracker wraps the session event stream. Your event handling code stays the same.
Requirements
- Python 3.10 or later
anthropicSDK 0.49 or later (Managed Agents beta)
1. Install
Shell
pip install agentmetrics-anthropic
2. Wrap the session stream
Replace the direct client.beta.sessions.events.stream() call with tracker.stream(). The context manager and event loop stay the same.
Sync
Python
import os
import anthropic
from agentmetrics_anthropic import AgentMetricsSessionTracker
client = anthropic.Anthropic()
tracker = AgentMetricsSessionTracker(
api_key=os.environ["AGENTMETRICS_API_KEY"],
agent_id="my-agent",
base_url="http://localhost:8099",
)
with tracker.stream(client, session_id="sess_...") as stream:
for event in stream:
print(event)
Async
Python
async with tracker.astream(client, session_id="sess_...") as stream:
async for event in stream:
print(event)
What gets tracked
AgentMetrics listens to the SSE event stream and extracts:
| Event type | What is captured |
|---|---|
span.model_request_end | Input tokens, output tokens, cache tokens, model name |
agent.tool_use / agent.mcp_tool_use | Tool call count and tool names |
agent.tool_result (is_error) | Tool error count |
session.error | Failure status and error message |
session.status_terminated | Run complete signal |
A run summary is emitted when the session terminates or when the context manager exits (whichever comes first).
Configuration
| Parameter | Default | Description |
|---|---|---|
api_key | required | Your AgentMetrics API key |
agent_id | "anthropic-agent" | Label shown in the dashboard |
base_url | "http://localhost:8099" | AgentMetrics backend URL |