Emit your own metrics for tools, workflows, processors, and scorers.
Paul 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.
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:
npm install @mastra/core@latest @mastra/observability@latest@mastra/core@1.76.0 or later, added in PR #26461.
Custom metrics are sent to the Mastra platform when MastraPlatformExporter is configured:
export const mastra = new Mastra({
// ...
observability: new Observability({
configs: {
default: {
// ...
exporters: [new MastraPlatformExporter()]
}
}
})
});Call .emit() from a tool to record a custom metric:
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): voidname(required): The metric name, such asrefund_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:
- Emit custom metrics
- MetricsContext reference
- Metric queries reference
- Automatic metrics reference
- Observability alerts
- Observability on Mastra platform
Paul ScanlonTechnical Product Marketing ManagerPaul 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 →More on Observability
All postsBlogNov 19, 2026Introducing Observability Alerts for Mastra Platform
Set thresholds for cost, tool failures, model latency, token usage, and eval scores.
BlogNov 5, 2026Introducing Latency Metrics for Mastra Observability
Track user experience through TTFT and TTFC.
BlogOct 28, 2026Introducing Span Queries
Find the most common failures for your coding agent to fix.
AI Trends