Skip to Content
ReferenceAgentsAgent

Agent Class

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

Usage example

src/mastra/agents/test-agent.ts
import { openai } from "@ai-sdk/openai"; import { Agent } from "@mastra/core/agent"; export const agent = new Agent({ name: "test-agent", instructions: 'message for agent', model: openai("gpt-4o") });

Constructor parameters

id?:

string
Optional unique identifier for the agent. Defaults to `name` if not provided.

name:

string
Unique identifier for the agent.

description?:

string
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 dynamically.

model:

MastraLanguageModel | ({ runtimeContext: RuntimeContext }) => MastraLanguageModel | Promise<MastraLanguageModel>
The language model used by the agent. Can be provided statically or resolved at runtime.

tools?:

ToolsInput | ({ runtimeContext: RuntimeContext }) => ToolsInput | Promise<ToolsInput>
Tools that the agent can access. Can be provided statically or resolved dynamically.

workflows?:

Record<string, Workflow> | ({ runtimeContext: RuntimeContext }) => Record<string, Workflow> | Promise<Record<string, Workflow>>
Workflows that the agent can execute. Can be static or dynamically resolved.

defaultGenerateOptions?:

AgentGenerateOptions | ({ runtimeContext: RuntimeContext }) => AgentGenerateOptions | Promise<AgentGenerateOptions>
Default options used when calling `generate()`.

defaultStreamOptions?:

AgentStreamOptions | ({ runtimeContext: RuntimeContext }) => AgentStreamOptions | Promise<AgentStreamOptions>
Default options used when calling `stream()`.

defaultVNextStreamOptions?:

AgentExecutionOptions | ({ runtimeContext: RuntimeContext }) => AgentExecutionOptions | Promise<AgentExecutionOptions>
Default options used when calling `stream()` in vNext mode.

mastra?:

Mastra
Reference to the Mastra runtime instance (injected automatically).

scorers?:

MastraScorers | ({ runtimeContext: RuntimeContext }) => MastraScorers | Promise<MastraScorers>
Scoring configuration for runtime evaluation and telemetry. Can be static or dynamically provided.

evals?:

Record<string, Metric>
Evaluation metrics for scoring agent responses.

memory?:

MastraMemory | ({ runtimeContext: RuntimeContext }) => MastraMemory | Promise<MastraMemory>
Memory module used for storing and retrieving stateful context.

voice?:

CompositeVoice
Voice settings for speech input and output.

inputProcessors?:

Processor[] | ({ runtimeContext: RuntimeContext }) => Processor[] | Promise<Processor[]>
Input processors that can modify or validate messages before they are processed by the agent. Must implement the `processInput` function.

outputProcessors?:

Processor[] | ({ runtimeContext: RuntimeContext }) => Processor[] | Promise<Processor[]>
Output processors that can modify or validate messages from the agent, before it is sent to the client. Must implement either (or both) of the `processOutputResult` and `processOutputStream` functions.

Returns

agent:

Agent<TAgentId, TTools, TMetrics>
A new Agent instance with the specified configuration.