Skip to main content

Observability API

The Observability API provides methods to retrieve traces, monitor application performance, score traces for evaluation, and record feedback linked to traces and spans.

Getting a specific trace
Direct link to Getting a specific trace

Retrieve a specific trace by its ID, including all its spans and details:

const trace = await mastraClient.getTrace('trace-id-123')

Getting traces with filtering
Direct link to Getting traces with filtering

Retrieve a paginated list of trace root spans with optional filtering:

const traces = await mastraClient.getTraces({
pagination: {
page: 1,
perPage: 20,
dateRange: {
start: new Date('2024-01-01'),
end: new Date('2024-01-31'),
},
},
filters: {
name: 'weather-agent', // Filter by trace name
spanType: 'agent', // Filter by span type
entityId: 'weather-agent-id', // Filter by entity ID
entityType: 'agent', // Filter by entity type
},
})

console.log(`Found ${traces.spans.length} root spans`)
console.log(`Total pages: ${traces.pagination.totalPages}`)

// To get the complete trace with all spans, use getTrace
const completeTrace = await mastraClient.getTrace(traces.spans[0].traceId)

Listing traces for a list view
Direct link to Listing traces for a list view

listTracesLight() returns the same rows as a trace list, without the input, output and attributes payloads. Each row carries a short inputPreview string instead, so a list can render a preview column without transferring the whole prompt.

Prefer it whenever you are rendering many traces at once, and fetch the full record only when a row is opened:

const list = await mastraClient.listTracesLight({
pagination: { page: 0, perPage: 25 },
filters: { entityType: 'agent' },
})

for (const span of list.spans) {
console.log(span.name, span.inputPreview)
}

// Fetch the full payload only for the trace the user selects
const selected = await mastraClient.getTrace(list.spans[0].traceId)

It accepts the same filtering, ordering and delta-polling arguments as listTraces(). Use listTraces() when you actually need the full span payloads.

Querying traces with recursive predicates
Direct link to Querying traces with recursive predicates

queryTraces() finds completed logical traces using trace fields and conditions over related spans or scores. Every query requires an ISO timestamp range of at most 31 days.

const result = await mastraClient.queryTraces({
timeRange: {
from: '2026-08-01T00:00:00.000Z',
to: '2026-08-08T00:00:00.000Z',
},
where: {
scores: {
some: {
op: 'and',
args: [
{ op: 'eq', left: { path: 'scorerId' }, right: { literal: 'factuality' } },
{ op: 'lt', left: { path: 'score' }, right: { literal: 0.6 } },
],
},
},
},
})

The API limits predicate depth, nodes, related clauses, set members, literal bytes, and the total literal budget before storage execution. Cursor pages are deterministic but aren't a database snapshot, so signals written between requests can change later pages. PostgreSQL and ClickHouse queries have a configurable 15-second execution timeout.

See Advanced trace queries for the complete limits, request fields, predicates, grouping, cursor pagination, response shapes, and errors.

Deleting traces
Direct link to Deleting traces

Delete traces and their associated spans, metrics, logs, scores, and feedback:

const result = await mastraClient.deleteTraces({
traceIds: ['trace-1', 'trace-2'],
})

console.log(result.success)

Each request accepts up to 1,000 trace IDs. Signals that aren't linked to a trace are preserved. Deletion also includes traces created by experiments.

Scoring traces
Direct link to Scoring traces

Score specific traces using registered scorers for evaluation:

const result = await mastraClient.score({
scorerName: 'answer-relevancy',
targets: [
{ traceId: 'trace-1', spanId: 'span-1' }, // Score specific span
{ traceId: 'trace-2' }, // Score specific span which defaults to the parent span
],
})

Getting scores by span
Direct link to Getting scores by span

Retrieve scores for a specific span within a trace:

const scores = await mastraClient.listScoresBySpan({
traceId: 'trace-123',
spanId: 'span-456',
page: 1,
perPage: 20,
})

Feedback
Direct link to Feedback

Feedback methods create, list, and query human-in-the-loop signals such as ratings, thumbs, comments, and corrections through the target Mastra runtime and its configured observability storage. They don't call the hosted Mastra Platform feedback query API. See the feedback guide for examples and the feedback reference for full schemas.

Creating feedback
Direct link to Creating feedback

Create a feedback record linked to a trace or span:

const response = await mastraClient.createFeedback({
feedback: {
traceId: 'trace-123',
spanId: 'span-456',
feedbackSource: 'user',
feedbackType: 'rating',
value: 1,
comment: 'Helpful answer.',
},
})

Listing feedback
Direct link to Listing feedback

Retrieve paginated feedback records with optional filters:

const feedback = await mastraClient.listFeedback({
filters: {
feedbackType: 'rating',
feedbackSource: 'studio',
},
pagination: { page: 0, perPage: 20 },
orderBy: { field: 'timestamp', direction: 'DESC' },
})

Aggregating feedback
Direct link to Aggregating feedback

Aggregate numeric feedback values, such as ratings or thumbs encoded as 1 and -1:

const averageRating = await mastraClient.getFeedbackAggregate({
feedbackType: 'rating',
aggregation: 'avg',
comparePeriod: 'previous_day',
})

Grouping feedback
Direct link to Grouping feedback

Group numeric feedback by observability dimensions:

const ratingsByAgent = await mastraClient.getFeedbackBreakdown({
feedbackType: 'rating',
groupBy: ['entityName'],
aggregation: 'avg',
})

Querying feedback over time
Direct link to Querying feedback over time

Bucket numeric feedback by interval:

const ratingsOverTime = await mastraClient.getFeedbackTimeSeries({
feedbackType: 'rating',
interval: '1h',
aggregation: 'avg',
groupBy: ['feedbackSource'],
})

Querying feedback percentiles
Direct link to Querying feedback percentiles

Return percentile series for numeric feedback values:

const ratingPercentiles = await mastraClient.getFeedbackPercentiles({
feedbackType: 'rating',
percentiles: [0.5, 0.95],
interval: '1d',
})