Evaluations & Scorers
Relevant source files
The following files were used as context for generating this wiki page:- packages/evals/src/index.ts
- packages/core/src/evals/run/index.ts
- packages/evals/src/scorers/llm/faithfulness/index.ts
- examples/basics/evals/faithfulness/index.ts
- packages/cli/src/commands/scorers/available-scorers.ts
- packages/evals/src/scorers/code/completeness/index.ts
- client-sdks/client-js/src/resources/scores.test.ts
- DEVELOPMENT.md
The Mastra Evaluations framework provides a system for measuring and evaluating the performance and quality of Large Language Model (LLM) applications. It is a core component of the Mastra ecosystem, designed to assess metrics such as accuracy, relevance, faithfulness, and toxicity. The framework is built around the concept of "Scorers," which are modular units that define specific evaluation logic.
This system allows developers to create both LLM-judged and code-based evaluations to ensure that AI-powered agents and workflows meet quality standards. Scores can be saved, retrieved, and analyzed, providing a mechanism for continuous monitoring and improvement of AI applications.
Sources: DEVELOPMENT.md:20-22, packages/core/src/evals/run/index.ts, client-sdks/client-js/src/resources/scores.test.ts
Core Architecture
The evaluation system is centered around the createScorer function, which uses a builder pattern to construct an evaluation unit. Each scorer follows a multi-step process to generate a final score and reasoning.
Sources: packages/evals/src/scorers/llm/faithfulness/index.ts:10, packages/evals/src/scorers/code/completeness/index.ts:92
Scorer Execution Flow
A scorer's execution is a pipeline of chained methods, typically including preprocessing, analysis, score generation, and reason generation.
The diagram below illustrates the typical execution flow of a scorer.
Scorer Types
There are two primary types of scorers:
- LLM-based Scorers: These scorers use another LLM as a "judge" to evaluate the output. They are suitable for nuanced, qualitative assessments. The
Faithfulnessscorer is an example, where an LLM checks if an agent's response is factually supported by a given context. - Code-based Scorers: These scorers use deterministic code and algorithms for evaluation. They are ideal for objective, quantitative metrics. The
Completenessscorer, which uses thecompromiseNLP library to calculate keyword coverage, is an example.
Sources: packages/cli/src/commands/scorers/available-scorers.ts:5, packages/evals/src/scorers/llm/faithfulness/index.ts:10, packages/evals/src/scorers/code/completeness/index.ts:92
Available Scorers
The framework includes a variety of pre-built scorers that can be used out-of-the-box. These are categorized by their function and type (LLM or Code).
| ID | Name | Description | Category | Type |
|---|---|---|---|---|
answer-relevancy | Answer Relevancy | Evaluates how relevant the answer is to the question | output-quality | llm |
bias | Bias Detection | Detects potential bias in generated responses | accuracy-and-reliability | llm |
context-precision | Context Precision | Measures how precisely context is used in responses | context-quality | llm |
context-relevance | Context Relevance | Evaluates relevance of retrieved context to the query | context-quality | llm |
faithfulness | Faithfulness | Measures how faithful the answer is to the given context | accuracy-and-reliability | llm |
hallucination | Hallucination Detection | Detects hallucinated content in responses | accuracy-and-reliability | llm |
llm-tool-call-accuracy | Tool Call Accuracy (LLM) | Evaluates accuracy of tool/function calls by LLM | accuracy-and-reliability | llm |
toxicity | Toxicity Detection | Detects toxic or harmful content in responses | output-quality | llm |
noise-sensitivity | Noise Sensitivity | Evaluates how sensitive the model is to noise in inputs | accuracy-and-reliability | llm |
prompt-alignment | Prompt Alignment | Evaluates how well responses align with prompt instructions | output-quality | llm |
completeness | Completeness | Evaluates completeness of output based on requirements | output-quality | code |
content-similarity | Content Similarity | Measures similarity between generated and expected content | accuracy-and-reliability | code |
keyword-coverage | Keyword Coverage | Checks coverage of required keywords in output | output-quality | code |
textual-difference | Textual Difference | Measures textual differences between outputs | accuracy-and-reliability | code |
tone | Tone Analysis | Analyzes tone and style of generated text | output-quality | code |
code-tool-call-accuracy | Tool Call Accuracy (Code) | Evaluates accuracy of code-based tool calls | accuracy-and-reliability | code |
Sources: packages/cli/src/commands/scorers/available-scorers.ts:4-180
Implementation Example: Faithfulness Scorer
The Faithfulness scorer is an LLM-based scorer that evaluates if an agent's response is factually consistent with a given context.
Configuration
It is created using createFaithfulnessScorer and requires a model to act as the judge and a context to check against.
// examples/basics/scorers/faithfulness/index.ts:11-17
import { createFaithfulnessScorer } from '@mastra/evals/scorers/prebuilt';
import { createOpenAI } from '@ai-sdk/openai';
const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// examples/basics/scorers/faithfulness/index.ts:31-36
const faithfulnessScorer = createFaithfulnessScorer({
model: openai('gpt-4o-mini'),
options: {
context: [context], // The source of truth
},
});
Execution
The scorer is executed by calling the .run() method with the input and output from an agent interaction.
// examples/basics/scorers/faithfulness/index.ts:43-50
const result = await faithfulnessScorer.run({
input: [{ role: 'user', content: 'Tell me about the Tesla Model 3.' }],
output: [
{
role: 'assistant',
content: response,
},
],
});
The scorer's internal logic first extracts claims from the agent's response and then verifies each claim against the provided context. The final score is the ratio of verified claims to the total number of claims.
Sources: examples/basics/scorers/faithfulness/index.ts, packages/evals/src/scorers/llm/faithfulness/index.ts:10-75
API for Scores
Mastra provides API endpoints to manage and retrieve evaluation scores. The JavaScript client SDK (@mastra/client) offers methods to interact with these endpoints.
This sequence diagram shows how a client application would typically interact with the Mastra API to manage scores.
Key API Methods
| Method | Endpoint | Description |
|---|---|---|
listScorers() | GET /api/scores/scorers | Fetches all available scorers. |
listScoresByRunId() | GET /api/scores/run/:runId | Fetches all scores associated with a specific agent or workflow run ID. Supports pagination. |
listScoresByEntityId() | GET /api/scores/entity/:entityType/:entityId | Fetches scores for a specific entity, such as an AGENT or WORKFLOW. Supports pagination. |
saveScore() | POST /api/scores | Saves a new score object to the storage backend. |
Sources: client-sdks/client-js/src/resources/scores.test.ts:25-155
Summary
The Evaluations & Scorers system is a vital part of the Mastra framework for ensuring the quality and reliability of AI applications. Its modular design, with a clear distinction between LLM-based and code-based scorers, offers flexibility for a wide range of evaluation needs. The builder pattern for creating scorers and the simple API for managing scores make it a powerful yet easy-to-use tool for developers to monitor and improve their AI agents and workflows.
Sources: DEVELOPMENT.md:20-22, packages/cli/src/commands/scorers/available-scorers.ts