Skip to main content

Automatic metrics reference

Mastra automatically extracts performance and usage metrics from traced execution. This page is the complete reference for every metric name, label, and context field that Mastra emits.

For setup instructions, see the Metrics overview.

When Mastra emits automatic metrics
Direct link to When Mastra emits automatic metrics

Metrics are extracted from spans when they end. The observability layer inspects each completed span, calculates duration, and (for model generation spans) reads token usage data. No manual instrumentation is needed.

Metrics are routed through an internal event bus to the DefaultExporter, which batches and flushes them to storage. Only DuckDB and in-memory storage backends support metrics persistence today.

What affects whether a metric is available
Direct link to What affects whether a metric is available

Two conditions must be true for a metric to reach storage:

  1. DefaultExporter is configured as an exporter.
  2. The storage backend supports metrics (DuckDB or InMemory).

If metrics aren't appearing, see troubleshooting.

Duration metrics
Direct link to Duration metrics

Duration metrics record execution time in milliseconds, calculated from the span's start and end timestamps. Each duration metric includes a status label set to ok or error, derived from the span's status.

Metric nameSpan typeDescription
mastra_agent_duration_msAGENT_RUNExecution time of an agent run
mastra_tool_duration_msTOOL_CALL, MCP_TOOL_CALLExecution time of a tool call, including MCP tool calls
mastra_workflow_duration_msWORKFLOW_RUNExecution time of a workflow run
mastra_model_duration_msMODEL_GENERATIONExecution time of a model generation
mastra_processor_duration_msPROCESSOR_RUNExecution time of a processor run

Token usage metrics
Direct link to Token usage metrics

Token metrics are emitted only from MODEL_GENERATION spans that include usage data. If the provider doesn't report usage, no token metrics are emitted for that span.

Input token metrics
Direct link to Input token metrics

Metric nameDescription
mastra_model_total_input_tokensTotal input tokens
mastra_model_input_text_tokensText tokens in the input prompt
mastra_model_input_cache_read_tokensTokens read from prompt cache (e.g. Anthropic)
mastra_model_input_cache_write_tokensTokens written to prompt cache
mastra_model_input_audio_tokensAudio tokens in the input (multimodal models)
mastra_model_input_image_tokensImage tokens in the input (vision models)

Output token metrics
Direct link to Output token metrics

Metric nameDescription
mastra_model_total_output_tokensTotal output tokens
mastra_model_output_text_tokensText tokens in the model's output
mastra_model_output_reasoning_tokensReasoning / chain-of-thought tokens (e.g. OpenAI o-series)
mastra_model_output_audio_tokensAudio tokens in the model's output
mastra_model_output_image_tokensOutput image tokens

Provider-reported detailed token categories
Direct link to Provider-reported detailed token categories

The detailed breakdown metrics (everything except total_input and total_output) are only emitted when the provider reports them. If a category has zero tokens, the metric is skipped for that span. Different providers report different levels of detail. For example, not all providers report cache or audio tokens.

When cost context is attached
Direct link to When cost context is attached

Cost context is attached to token metrics when the embedded pricing registry has a matching entry for the provider and model. The registry ships with Mastra and covers common providers and models. If no match is found, token metrics are still emitted but without cost fields.

What cost fields may be included
Direct link to What cost fields may be included

FieldDescription
providerProvider name (e.g. openai, anthropic)
modelModel identifier (e.g. gpt-4o, claude-sonnet-4-20250514)
estimatedCostEstimated cost for this metric, calculated from token count and pricing tier
costUnitCurrency unit (e.g. USD)
costMetadataAdditional pricing context (tier information, error details)

Correlation with traces
Direct link to Correlation with traces

How metrics relate to spans and trace context
Direct link to How metrics relate to spans and trace context

Each metric carries a CorrelationContext snapshot from the span that produced it. This context is stored alongside the metric value and lets you navigate from a metric to the exact span and trace.

The correlation fields are grouped into four categories:

Trace correlation

  • traceId: trace identifier
  • spanId: span identifier
  • tags: tags from the span

Entity hierarchy

  • entityType, entityId, entityName: The entity that produced the metric (e.g. agent, workflow)
  • parentEntityType, parentEntityId, parentEntityName: The parent entity
  • rootEntityType, rootEntityId, rootEntityName: The root entity in the call chain

Identity

  • userId, organizationId, resourceId: Identity context from the request
  • runId, sessionId, threadId, requestId: Correlation IDs

Deployment

  • environment: Deployment environment (e.g. production, staging)
  • source: Source identifier
  • serviceName: Service name from the observability config
  • experimentId: Experiment identifier, if applicable

Why correlation helps with debugging
Direct link to Why correlation helps with debugging

When you spot a spike in latency or token usage on the Metrics dashboard, correlation context lets you drill directly into the trace that produced the metric. From there you can inspect the individual span to find the root cause: a slow tool call, a large prompt, or an unexpected error.

Troubleshooting
Direct link to Troubleshooting

No metrics are appearing
Direct link to No metrics are appearing

  • Observability is configured: Verify that your Mastra instance has an observability config with at least one exporter.
  • DefaultExporter is present: Other exporters (Datadog, Langfuse, etc.) don't persist metrics to Mastra storage. DefaultExporter is required for the Studio dashboard.
  • Storage supports metrics: Metrics require a separate OLAP store for observability. Relational databases like PostgreSQL, LibSQL, etc. are not supported for metrics. In-memory storage resets on restart.
  • Sampling isn't 0%: If sampling probability is 0 or strategy is never, all spans become no-ops and no metrics are extracted.

Duration metrics are missing
Direct link to Duration metrics are missing

  • Span has timestamps: Duration is calculated from startTime and endTime. If either is missing, the metric is skipped.
  • Span type maps to a metric: Only AGENT_RUN, TOOL_CALL, MCP_TOOL_CALL, WORKFLOW_RUN, MODEL_GENERATION, and PROCESSOR_RUN spans produce duration metrics.

Token metrics are missing
Direct link to Token metrics are missing

  • Span is a model generation: Token metrics are only emitted from MODEL_GENERATION spans.
  • Provider reports usage: The model provider must include usage data in its response. If usage is absent, no token metrics are emitted.