Skip to Content
ReferenceAgentsAgent

Agent

The Agent class is the foundation for creating AI agents in Mastra. It provides methods for generating responses, streaming interactions, and handling voice capabilities.

Importing

import { Agent } from "@mastra/core/agent";

Constructor

Creates a new Agent instance with the specified configuration.

constructor(config: AgentConfig<TAgentId, TTools, TMetrics>)

Parameters


name:

string
Unique identifier for the agent.

description?:

string
An optional description of the agent's purpose and capabilities.

instructions:

string | ({ runtimeContext: RuntimeContext }) => string | Promise<string>
Instructions that guide the agent's behavior. Can be a static string or a function that returns a string.

model:

MastraLanguageModel | ({ runtimeContext: RuntimeContext }) => MastraLanguageModel | Promise<MastraLanguageModel>
The language model to use for generating responses. Can be a model instance or a function that returns a model.

tools?:

ToolsInput | ({ runtimeContext: RuntimeContext }) => ToolsInput | Promise<ToolsInput>
Tools that the agent can use. Can be a static object or a function that returns tools.

inputProcessors?:

InputProcessor[] | ({ runtimeContext: RuntimeContext }) => InputProcessor[] | Promise<InputProcessor[]>
Input processors that run sequentially before messages are sent to the language model. These middleware components can intercept, modify, validate, or filter messages. Each processor receives an array of MastraMessageV2 objects and an abort function for early termination. Can be a static array or a function that returns processors based on runtime context.

defaultGenerateOptions?:

AgentGenerateOptions
Default options to use when calling generate().

defaultStreamOptions?:

AgentStreamOptions
Default options to use when calling stream().

workflows?:

Record<string, NewWorkflow> | ({ runtimeContext: RuntimeContext }) => Record<string, NewWorkflow> | Promise<Record<string, NewWorkflow>>
Workflows that the agent can execute. Can be a static object or a function that returns workflows.

evals?:

Record<string, Metric>
Evaluation metrics for assessing agent performance.

memory?:

MastraMemory | ({ runtimeContext: RuntimeContext }) => MastraMemory | Promise<MastraMemory>
Memory system for the agent to store and retrieve information. Can be a static memory instance or a function that returns a memory instance based on runtime context.

voice?:

CompositeVoice
Voice capabilities for speech-to-text and text-to-speech functionality.