What ships out of the box
- Structured logger –
app.logger,context.logger, and everyAgentshare the same event bus, automatically enriched with trace and workflow identifiers. - TokenCounter – every AugmentedLLM records token usage, cost estimates, and parent/child relationships so you can inspect expensive branches.
- OpenTelemetry hooks – spans are emitted for workflows, tool calls, LLM requests, MCP server traffic, and Temporal activities when tracing is enabled.
- Metrics integration points –
mcp_agent.tracing.telemetry.get_meterexposes counters/histograms ready for Prometheus or any OTLP collector.
Enable OpenTelemetry
Add theotel block to mcp_agent.config.yaml (see the configuration reference for every option). The snippet below mirrors what the tracing examples ship with (multiple exporters are supported; include as many as you need):
- otlp block if you want to send the same data to multiple collectors.
Add spans and metrics in code
Use the helpers frommcp_agent.tracing.telemetry inside workflows, tools, or activities (or apply the @telemetry.traced() decorator when you want automatic span creation):
get_tracer(self.context) when you are inside mcp-agent primitives so trace data flows through the shared Context. If you are instrumenting utility code outside that context, you can fall back to standard OpenTelemetry helpers (from opentelemetry import trace; tracer = trace.get_tracer(__name__)).
For metrics, grab a meter and increment counters/histograms (the Prometheus exporter is enabled automatically when you add a metric reader):
Metrics collection & token accounting
Token summaries and trees
Every AugmentedLLM exposes a token node that mirrors its call graph. The orchestrator workflow example wraps this in a helper that prints a tree:TokenNode reports aggregate usage, per-child breakdowns, and cost estimates. You can attach the tree to your own logging, export it as JSON, or feed it into observability dashboards.
examples/tracing/agent showing spans and structured logs side by side.
examples/basic/token_counter.
TokenCounter watchers
TheTokenCounter tracks usage for every workflow, agent, and LLM node. Besides summaries and trees, you can attach real-time watchers or render live progress. The examples/basic/token_counter walkthrough demonstrates:
TokenProgressDisplayfor live terminal dashboards.- Custom watcher callbacks (e.g.,
token_counter.watch(...)) that fire when token thresholds are exceeded. - Per-model breakdowns and cost calculations stored in
TokenNode.metadata.
Export destinations
| Exporter | Use it for | Notes |
|---|---|---|
console | Quick local debugging | Emits coloured spans/logs to stdout. |
file | Persist traces/logs to disk | Combine with path_settings for per-run log files. |
otlp | OpenTelemetry collectors (e.g. Datadog, Langfuse, Jaeger) | Set the endpoint + headers; works for traces and metrics. |
Reference implementations
examples/tracing/agent– minimal tracing + human-input callback instrumentation.examples/tracing/temporal– Temporal executor with Jaeger configuration and OTLP exporters.examples/tracing/llm– shows span attributes for individual LLM calls and tool usage.examples/tracing/mcp– emits spans and structured data for MCP server traffic.examples/tracing/langfuse– exports traces and events to Langfuse with user/session metadata.examples/tracing/temporal– demonstrates how spans flow through Temporal workflows and activities.
