Skip to main content
Reliable agents need first-class visibility. mcp-agent ships with structured logging, OpenTelemetry instrumentation, and a token counter that works across every AugmentedLLM. This page shows how to wire everything together and where to find reference implementations.

What ships out of the box

  • Structured loggerapp.logger, context.logger, and every Agent share 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 pointsmcp_agent.tracing.telemetry.get_meter exposes counters/histograms ready for Prometheus or any OTLP collector.

Enable OpenTelemetry

Add the otel 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):
Once enabled, spans automatically propagate through AugmentedLLMs, MCP server calls, and Temporal workflows. Point the OTLP exporter at your tracing backend and repeat the - otlp block if you want to send the same data to multiple collectors.

Add spans and metrics in code

Use the helpers from mcp_agent.tracing.telemetry inside workflows, tools, or activities (or apply the @telemetry.traced() decorator when you want automatic span creation):
Prefer 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:
The 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. Image Screenshot from examples/tracing/agent showing spans and structured logs side by side.
Sample output taken from examples/basic/token_counter.

TokenCounter watchers

The TokenCounter 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:
  • TokenProgressDisplay for 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

Reference implementations

Combine the tracing data with the structured logger (see the Logging guide) to correlate events, spans, and MCP tool calls in one place.