Introducing Custom Metrics for Mastra Observability

Emit your own metrics for tools, workflows, processors, and scorers.

Paul ScanlonPaul Scanlon·

Dec 1, 2026

·

3 min read

You can now emit custom metrics from tools, workflows, processors, scorers, or anywhere else in your application using metrics.emit(), and passing in a name, a value, optional labels, cost details, or specific metadata.

Custom metrics work alongside Mastra's automatic metrics, which record duration, tokens, and cost for every agent run, tool call, and model call. Anything else you'd like to measure can now be recorded with labels and payloads that work for you.

Custom metrics emitted from a tool, workflow step, processor, and scorer, each shown next to its exported record, then Mastra Code using the Mastra CLI to list the five custom metrics, total this week's refunds, and break them down by reason.

Automatically collected metrics have been visible on the Mastra platform for a while, but now with custom metrics you can create your own measures and set alerts when a value crosses a threshold.

Custom metrics work on the Mastra platform with MastraPlatformExporter, and on self-hosted Mastra projects with MastraStorageExporter and a ClickHouse, DuckDB, or PostgreSQL observability store.

Get started

Install the latest Mastra core and observability packages:

GNU BashTerminal
npm install @mastra/core@latest @mastra/observability@latest
note
Requires @mastra/core@1.76.0 or later, added in PR #26461.

Custom metrics are sent to the Mastra platform when MastraPlatformExporter is configured:

TypeScriptsrc/mastra/index.ts
export const mastra = new Mastra({
  // ...
  observability: new Observability({
    configs: {
      default: {
        // ...
        exporters: [new MastraPlatformExporter()]
      }
    }
  })
});

Call .emit() from a tool to record a custom metric:

TypeScriptsrc/mastra/tools/issue-refund.ts
import { createTool } from "@mastra/core/tools";
 
export const issueRefund = createTool({
  // ...
  execute: async (inputData, context) => {
    context.metrics?.emit(
      "refund_amount_usd",
      84,
      { reason: "damaged", channel: "chat" },
      {
        costContext: {
          provider: "openai",
          model: "gpt-5-mini",
          estimatedCost: 0.0021,
          costUnit: "USD",
          costMetadata: { check: "fraud" }
        },
        usageId: "re_4417"
      }
    );
  }
});

You can also emit metrics from:

Typing custom metrics

.emit() accepts a name, a value, and optional labels and options:

emit(name: string, value: number, labels?: Record<string, string>, options?: MetricEmitOptions): void
  • name (required): The metric name, such as refund_amount_usd.
  • value (required): The number to record.
  • labels (optional): Key-value pairs to filter and group by.
  • options (optional): A cost to attach to the value.

MetricEmitOptions is exported from @mastra/core/observability. Every field is optional, but the shape is fixed. Additional details can be added to costContext.costMetadata, which accepts any object:

export interface MetricEmitOptions {
  /** Canonical model/cost context for this specific metric row */
  costContext?: CostContext;
  /** Shared by every token/cost row emitted from the same model usage observation */
  usageId?: string;
}
 
export interface CostContext {
  provider?: string;
  model?: string;
  estimatedCost?: number;
  costUnit?: string;
  costMetadata?: Record<string, unknown>;
}

Query custom metrics

Custom metrics can be queried the same way as automatic metrics, using any of the following:

For more information and full configuration options, see:

Share on X or LinkedIn
Paul Scanlon
Paul ScanlonTechnical Product Marketing Manager

Paul Scanlon sits between Developer Education and Product Marketing at Mastra. Previously, he was a Technical Product Marketing Manager at Neon and worked in Developer Relations at Gatsby, where he created educational content and developer experiences.

All articles by Paul Scanlon →
All posts