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 examplesDirect link to Usage examples
Basic string instructionsDirect link to Basic string instructions
Passing instructions as a string or array of strings is the simplest way to set up an agent. This is useful for straightforward use cases where you need to provide a prompt without additional configuration.
import { Agent } from '@mastra/core/agent'
// String instructions
export const agent = new Agent({
id: 'test-agent',
name: 'Test Agent',
instructions: 'You are a helpful assistant that provides concise answers.',
model: 'openai/gpt-5.4',
})
// System message object
export const agent2 = new Agent({
id: 'test-agent-2',
name: 'Test Agent 2',
instructions: {
role: 'system',
content: 'You are an expert programmer',
},
model: 'openai/gpt-5.4',
})
// Array of system messages
export const agent3 = new Agent({
id: 'test-agent-3',
name: 'Test Agent 3',
instructions: [
{ role: 'system', content: 'You are a helpful assistant' },
{ role: 'system', content: 'You have expertise in TypeScript' },
],
model: 'openai/gpt-5.4',
})
Provider-specific configurationsDirect link to Provider-specific configurations
Each model provider also enables a few different options, including prompt caching and configuring reasoning. You can set providerOptions on the instruction level to set different caching strategy per system instruction/prompt.
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'core-message-agent',
name: 'Core Message Agent',
instructions: {
role: 'system',
content: 'You are a helpful assistant specialized in technical documentation.',
providerOptions: {
openai: {
reasoningEffort: 'low',
},
},
},
model: 'openai/gpt-5.4',
})
Mixed instruction formatsDirect link to Mixed instruction formats
import { Agent } from '@mastra/core/agent'
// This could be customizable based on the user
const preferredTone = {
role: 'system',
content: 'Always maintain a professional and empathetic tone.',
}
export const agent = new Agent({
id: 'multi-message-agent',
name: 'Multi Message Agent',
instructions: [
{ role: 'system', content: 'You are a customer service representative.' },
preferredTone,
{
role: 'system',
content: 'Escalate complex issues to human agents when needed.',
providerOptions: {
anthropic: { cacheControl: { type: 'ephemeral' } },
},
},
],
model: 'anthropic/claude-sonnet-4-6',
})
Thread signalsDirect link to Thread signals
Use Agent signals to send real-time context into a memory thread. Signals are useful when a user adds input while an agent is already streaming, or when another process needs to add structured context to the thread.
A user-message signal represents user input. When the target thread is running, Mastra delivers the signal into the active agent loop. When the thread is idle, Mastra starts a stream with the signal as the first input by default.
const subscription = await agent.subscribeToThread({
resourceId: 'user-123',
threadId: 'thread-abc',
})
void (async () => {
for await (const chunk of subscription.stream) {
console.log(chunk)
}
})()
agent.sendSignal(
{ type: 'user-message', contents: 'Use the latest customer note too.' },
{
resourceId: 'user-123',
threadId: 'thread-abc',
ifIdle: {
streamOptions: {
maxSteps: 3,
},
},
},
)
Use a custom signal type for contextual messages that should reach the model as XML-wrapped context instead of normal user input:
agent.sendSignal(
{
type: 'system-reminder',
contents: 'Continue from the previous tool result.',
attributes: { reminderType: 'tool-result' },
},
{ resourceId: 'user-123', threadId: 'thread-abc' },
)
sendSignal(signal, options)Direct link to sendsignalsignal-options
Sends a signal to an active run or memory thread.
signal:
options.runId?:
options.resourceId?:
options.threadId?:
options.ifActive.behavior?:
options.ifIdle.behavior?:
options.ifIdle.streamOptions?:
Returns { accepted: true, runId: string, signal: CreatedAgentSignal, persisted?: Promise<void> }. persisted is only present for persist behavior and resolves when Mastra finishes writing the signal to memory.
subscribeToThread(options)Direct link to subscribetothreadoptions
Subscribes to raw stream chunks for a memory thread. Use this before calling sendSignal() when you need to render stream output, observe signal echoes, or abort the active run.
options.resourceId?:
options.threadId:
Returns an AgentThreadSubscription object with these members: