@mastra/core 1.15.0-alpha.0 → 1.15.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/dist/agent/index.cjs +8 -8
- package/dist/agent/index.js +1 -1
- package/dist/{chunk-JBYJDZT5.js → chunk-4YKZNIK6.js} +4 -4
- package/dist/{chunk-JBYJDZT5.js.map → chunk-4YKZNIK6.js.map} +1 -1
- package/dist/{chunk-I5ON7TPA.cjs → chunk-7BM5LQHF.cjs} +82 -82
- package/dist/{chunk-I5ON7TPA.cjs.map → chunk-7BM5LQHF.cjs.map} +1 -1
- package/dist/{chunk-IST54Q37.js → chunk-ASVFCNLS.js} +7 -7
- package/dist/{chunk-IST54Q37.js.map → chunk-ASVFCNLS.js.map} +1 -1
- package/dist/{chunk-DGSXFZGZ.js → chunk-CTJLKJMO.js} +10 -6
- package/dist/chunk-CTJLKJMO.js.map +1 -0
- package/dist/{chunk-4JBWS3Y6.js → chunk-EAWVRIHS.js} +3 -3
- package/dist/{chunk-4JBWS3Y6.js.map → chunk-EAWVRIHS.js.map} +1 -1
- package/dist/{chunk-7GXY5CDK.js → chunk-ECAVJWAH.js} +4 -4
- package/dist/{chunk-7GXY5CDK.js.map → chunk-ECAVJWAH.js.map} +1 -1
- package/dist/{chunk-EG3QZTQQ.cjs → chunk-EFYYAFWD.cjs} +19 -15
- package/dist/chunk-EFYYAFWD.cjs.map +1 -0
- package/dist/{chunk-ORYC6WMY.js → chunk-EYLPPZIF.js} +4 -2
- package/dist/chunk-EYLPPZIF.js.map +1 -0
- package/dist/{chunk-FIOAZZAM.js → chunk-FNOAF2SZ.js} +3 -3
- package/dist/{chunk-FIOAZZAM.js.map → chunk-FNOAF2SZ.js.map} +1 -1
- package/dist/{chunk-GMUEV4ML.cjs → chunk-FYQXIWRH.cjs} +4 -2
- package/dist/chunk-FYQXIWRH.cjs.map +1 -0
- package/dist/{chunk-KZLBDSIF.js → chunk-HIZDAENF.js} +3 -3
- package/dist/{chunk-KZLBDSIF.js.map → chunk-HIZDAENF.js.map} +1 -1
- package/dist/{chunk-7XPMIQIK.js → chunk-JIBMK2QP.js} +8 -8
- package/dist/{chunk-7XPMIQIK.js.map → chunk-JIBMK2QP.js.map} +1 -1
- package/dist/{chunk-CBLM3UY3.js → chunk-KCZ3R5SF.js} +3 -3
- package/dist/{chunk-CBLM3UY3.js.map → chunk-KCZ3R5SF.js.map} +1 -1
- package/dist/{chunk-Y2I3C7FR.cjs → chunk-M5BDH7B4.cjs} +6 -6
- package/dist/{chunk-Y2I3C7FR.cjs.map → chunk-M5BDH7B4.cjs.map} +1 -1
- package/dist/{chunk-4CC2ZV3B.js → chunk-MHTWFVXK.js} +3 -3
- package/dist/{chunk-4CC2ZV3B.js.map → chunk-MHTWFVXK.js.map} +1 -1
- package/dist/{chunk-REVBDBHI.cjs → chunk-NWPRZZ2K.cjs} +48 -48
- package/dist/{chunk-REVBDBHI.cjs.map → chunk-NWPRZZ2K.cjs.map} +1 -1
- package/dist/{chunk-PEKFBFE2.cjs → chunk-ORHPD25N.cjs} +7 -7
- package/dist/{chunk-PEKFBFE2.cjs.map → chunk-ORHPD25N.cjs.map} +1 -1
- package/dist/{chunk-SV6VG3XO.cjs → chunk-OX63O3QG.cjs} +5 -5
- package/dist/{chunk-SV6VG3XO.cjs.map → chunk-OX63O3QG.cjs.map} +1 -1
- package/dist/{chunk-NSJS72DA.cjs → chunk-Q64Z437G.cjs} +15 -15
- package/dist/{chunk-NSJS72DA.cjs.map → chunk-Q64Z437G.cjs.map} +1 -1
- package/dist/{chunk-W4R4TA4Z.cjs → chunk-RFZB2PQE.cjs} +9 -9
- package/dist/{chunk-W4R4TA4Z.cjs.map → chunk-RFZB2PQE.cjs.map} +1 -1
- package/dist/{chunk-J7UJLVIQ.cjs → chunk-TJB7IK7N.cjs} +19 -11
- package/dist/chunk-TJB7IK7N.cjs.map +1 -0
- package/dist/{chunk-OT7UVM2Z.cjs → chunk-X4RVX77L.cjs} +185 -185
- package/dist/{chunk-OT7UVM2Z.cjs.map → chunk-X4RVX77L.cjs.map} +1 -1
- package/dist/{chunk-XVOLOB5X.cjs → chunk-YV5UMIRV.cjs} +3 -3
- package/dist/{chunk-XVOLOB5X.cjs.map → chunk-YV5UMIRV.cjs.map} +1 -1
- package/dist/{chunk-SCTBRRU3.js → chunk-Z76WT6W3.js} +18 -10
- package/dist/chunk-Z76WT6W3.js.map +1 -0
- package/dist/datasets/index.cjs +11 -11
- package/dist/datasets/index.js +1 -1
- package/dist/docs/SKILL.md +10 -11
- package/dist/docs/assets/SOURCE_MAP.json +154 -154
- package/dist/docs/references/docs-agents-agent-approval.md +114 -193
- package/dist/docs/references/docs-agents-guardrails.md +120 -167
- package/dist/docs/references/docs-agents-networks.md +88 -205
- package/dist/docs/references/docs-agents-overview.md +47 -256
- package/dist/docs/references/docs-agents-processors.md +201 -297
- package/dist/docs/references/docs-agents-structured-output.md +13 -22
- package/dist/docs/references/docs-agents-supervisor-agents.md +24 -18
- package/dist/docs/references/docs-agents-using-tools.md +81 -104
- package/dist/docs/references/docs-memory-observational-memory.md +4 -2
- package/dist/docs/references/docs-memory-overview.md +219 -24
- package/dist/docs/references/docs-memory-semantic-recall.md +1 -1
- package/dist/docs/references/docs-memory-storage.md +4 -4
- package/dist/docs/references/docs-memory-working-memory.md +1 -1
- package/dist/docs/references/docs-observability-overview.md +1 -1
- package/dist/docs/references/docs-observability-tracing-exporters-arize.md +1 -1
- package/dist/docs/references/docs-server-request-context.md +1 -1
- package/dist/docs/references/docs-workflows-overview.md +1 -1
- package/dist/docs/references/docs-workspace-overview.md +1 -1
- package/dist/docs/references/guides-concepts-multi-agent-systems.md +75 -0
- package/dist/docs/references/reference-agents-agent.md +6 -8
- package/dist/docs/references/reference-agents-generate.md +74 -23
- package/dist/docs/references/reference-agents-getMemory.md +1 -1
- package/dist/docs/references/reference-agents-network.md +2 -2
- package/dist/docs/references/reference-ai-sdk-network-route.md +1 -1
- package/dist/docs/references/reference-ai-sdk-with-mastra.md +1 -1
- package/dist/docs/references/reference-core-getMemory.md +1 -2
- package/dist/docs/references/reference-core-listMemory.md +1 -2
- package/dist/docs/references/reference-harness-harness-class.md +2 -2
- package/dist/docs/references/reference-memory-observational-memory.md +3 -1
- package/dist/docs/references/reference-processors-processor-interface.md +2 -0
- package/dist/docs/references/reference-storage-overview.md +1 -1
- package/dist/docs/references/reference-templates-overview.md +1 -1
- package/dist/docs/references/reference-tools-create-tool.md +16 -4
- package/dist/evals/index.cjs +5 -5
- package/dist/evals/index.js +2 -2
- package/dist/evals/scoreTraces/index.cjs +3 -3
- package/dist/evals/scoreTraces/index.js +1 -1
- package/dist/harness/harness.d.ts +3 -0
- package/dist/harness/harness.d.ts.map +1 -1
- package/dist/harness/index.cjs +48 -13
- package/dist/harness/index.cjs.map +1 -1
- package/dist/harness/index.js +46 -11
- package/dist/harness/index.js.map +1 -1
- package/dist/harness/types.d.ts +11 -0
- package/dist/harness/types.d.ts.map +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.js +1 -1
- package/dist/llm/index.cjs +16 -16
- package/dist/llm/index.js +5 -5
- package/dist/llm/model/model.d.ts.map +1 -1
- package/dist/llm/model/provider-types.generated.d.ts +6 -2
- package/dist/loop/index.cjs +14 -14
- package/dist/loop/index.js +1 -1
- package/dist/mastra/index.cjs +2 -2
- package/dist/mastra/index.js +1 -1
- package/dist/memory/index.cjs +14 -14
- package/dist/memory/index.js +1 -1
- package/dist/memory/types.d.ts +7 -0
- package/dist/memory/types.d.ts.map +1 -1
- package/dist/models-dev-E6FRPGHV.js +3 -0
- package/dist/{models-dev-5BT32JYX.js.map → models-dev-E6FRPGHV.js.map} +1 -1
- package/dist/models-dev-WIROJ2IM.cjs +12 -0
- package/dist/{models-dev-OTMJW4WK.cjs.map → models-dev-WIROJ2IM.cjs.map} +1 -1
- package/dist/netlify-7IRBQ2BY.cjs +12 -0
- package/dist/{netlify-DT2P2NQD.cjs.map → netlify-7IRBQ2BY.cjs.map} +1 -1
- package/dist/netlify-OAGRP6WY.js +3 -0
- package/dist/{netlify-FJCQU3OY.js.map → netlify-OAGRP6WY.js.map} +1 -1
- package/dist/processor-provider/index.cjs +10 -10
- package/dist/processor-provider/index.js +1 -1
- package/dist/processors/index.cjs +42 -42
- package/dist/processors/index.js +1 -1
- package/dist/provider-registry-2MHU2NP6.js +3 -0
- package/dist/{provider-registry-BCSAL2IQ.js.map → provider-registry-2MHU2NP6.js.map} +1 -1
- package/dist/provider-registry-FINEGQHE.cjs +40 -0
- package/dist/{provider-registry-65WCBR2K.cjs.map → provider-registry-FINEGQHE.cjs.map} +1 -1
- package/dist/provider-registry.json +14 -6
- package/dist/relevance/index.cjs +3 -3
- package/dist/relevance/index.js +1 -1
- package/dist/stream/index.cjs +8 -8
- package/dist/stream/index.js +1 -1
- package/dist/test-utils/llm-mock.cjs +4 -4
- package/dist/test-utils/llm-mock.js +1 -1
- package/dist/tool-loop-agent/index.cjs +4 -4
- package/dist/tool-loop-agent/index.js +1 -1
- package/dist/workflows/default.d.ts +2 -2
- package/dist/workflows/default.d.ts.map +1 -1
- package/dist/workflows/evented/index.cjs +10 -10
- package/dist/workflows/evented/index.js +1 -1
- package/dist/workflows/index.cjs +24 -24
- package/dist/workflows/index.js +1 -1
- package/package.json +7 -7
- package/src/llm/model/provider-types.generated.d.ts +6 -2
- package/dist/chunk-DGSXFZGZ.js.map +0 -1
- package/dist/chunk-EG3QZTQQ.cjs.map +0 -1
- package/dist/chunk-GMUEV4ML.cjs.map +0 -1
- package/dist/chunk-J7UJLVIQ.cjs.map +0 -1
- package/dist/chunk-ORYC6WMY.js.map +0 -1
- package/dist/chunk-SCTBRRU3.js.map +0 -1
- package/dist/docs/references/docs-agents-agent-memory.md +0 -209
- package/dist/docs/references/docs-agents-network-approval.md +0 -278
- package/dist/models-dev-5BT32JYX.js +0 -3
- package/dist/models-dev-OTMJW4WK.cjs +0 -12
- package/dist/netlify-DT2P2NQD.cjs +0 -12
- package/dist/netlify-FJCQU3OY.js +0 -3
- package/dist/provider-registry-65WCBR2K.cjs +0 -40
- package/dist/provider-registry-BCSAL2IQ.js +0 -3
|
@@ -1,110 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Agents overview
|
|
2
2
|
|
|
3
|
-
Agents use LLMs and tools to solve open-ended tasks. They reason about goals, decide which tools to use, retain conversation memory, and iterate internally until the model emits a final answer or an optional stop condition is met. Agents produce structured responses you can render in your UI or process programmatically. Use agents directly or compose them into workflows or agent
|
|
3
|
+
Agents use LLMs and tools to solve open-ended tasks. They reason about goals, decide which tools to use, retain conversation memory, and iterate internally until the model emits a final answer or an optional stop condition is met. Agents produce structured responses you can render in your UI or process programmatically. Use agents directly or compose them into workflows or multi-agent systems.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## When to use agents
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Use agents when the task is open-ended and the steps aren't known in advance. An agent decides which tools to call, how many times to loop, and when to stop. You provide the goal and constraints instead of defining each step. For predetermined, multi-step processes with explicit control flow, use [workflows](https://mastra.ai/docs/workflows/overview) instead.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
> **Tip:** Watch an introduction to agents, and how they compare to workflows on [YouTube (7 minutes)](https://youtu.be/0jg2g3sNvgw).
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Quickstart
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
**npm**:
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
npm install @mastra/core@latest
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
**pnpm**:
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
pnpm add @mastra/core@latest
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
**Yarn**:
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
yarn add @mastra/core@latest
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
**Bun**:
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
bun add @mastra/core@latest
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
2. Mastra's model router auto-detects environment variables for your chosen provider. For OpenAI, set `OPENAI_API_KEY`:
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
OPENAI_API_KEY=<your-api-key>
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
> **Note:** Mastra supports more than 600 models. Choose from the [full list](https://mastra.ai/models).
|
|
46
|
-
|
|
47
|
-
3. Create an agent by instantiating the `Agent` class with system `instructions` and a `model`:
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
import { Agent } from '@mastra/core/agent'
|
|
51
|
-
|
|
52
|
-
export const testAgent = new Agent({
|
|
53
|
-
id: 'test-agent',
|
|
54
|
-
name: 'Test Agent',
|
|
55
|
-
instructions: 'You are a helpful assistant.',
|
|
56
|
-
model: 'openai/gpt-5.4',
|
|
57
|
-
})
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### Instruction formats
|
|
61
|
-
|
|
62
|
-
Instructions define the agent's behavior, personality, and capabilities. They're system-level prompts that establish the agent's core identity and expertise.
|
|
63
|
-
|
|
64
|
-
Instructions can be provided in multiple formats for greater flexibility. The examples below illustrate the supported shapes:
|
|
13
|
+
Create an agent by instantiating the `Agent` class from `@mastra/core` and provide the required properties:
|
|
65
14
|
|
|
66
15
|
```typescript
|
|
67
|
-
|
|
68
|
-
instructions: 'You are a helpful assistant.'
|
|
69
|
-
|
|
70
|
-
// Array of strings
|
|
71
|
-
instructions: ['You are a helpful assistant.', 'Always be polite.', 'Provide detailed answers.']
|
|
72
|
-
|
|
73
|
-
// Array of system messages
|
|
74
|
-
instructions: [
|
|
75
|
-
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
76
|
-
{ role: 'system', content: 'You have expertise in TypeScript.' },
|
|
77
|
-
]
|
|
78
|
-
```
|
|
16
|
+
import { Agent } from '@mastra/core/agent'
|
|
79
17
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
instructions: {
|
|
87
|
-
role: "system",
|
|
88
|
-
content:
|
|
89
|
-
"You are an expert code reviewer. Analyze code for bugs, performance issues, and best practices.",
|
|
90
|
-
providerOptions: {
|
|
91
|
-
openai: { reasoningEffort: "high" }, // OpenAI's reasoning models
|
|
92
|
-
anthropic: { cacheControl: { type: "ephemeral" } } // Anthropic's prompt caching
|
|
93
|
-
}
|
|
94
|
-
}
|
|
18
|
+
export const testAgent = new Agent({
|
|
19
|
+
id: 'test-agent',
|
|
20
|
+
name: 'Test Agent',
|
|
21
|
+
instructions: 'You are a helpful assistant.',
|
|
22
|
+
model: 'openai/gpt-5.4',
|
|
23
|
+
})
|
|
95
24
|
```
|
|
96
25
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
### Dynamic instructions
|
|
100
|
-
|
|
101
|
-
Instructions can be provided as an async function, allowing you to resolve prompts at runtime. This enables patterns like personalizing instructions based on user context, fetching prompts from external registry services, and running A/B tests with different variants.
|
|
26
|
+
The `instructions` define the agent's behavior, personality, and capabilities. They're system-level prompts that establish the agent's core identity and expertise. The `model` is specified as `'provider/model-name'` using Mastra's [model router](https://mastra.ai/models).
|
|
102
27
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
### Registering an agent
|
|
106
|
-
|
|
107
|
-
Register your agent in the Mastra instance to make it available throughout your application. Once registered, it can be called from workflows, tools, or other agents, and has access to shared resources such as memory, logging, and observability features:
|
|
28
|
+
To make the agent available throughout your application, register it in your Mastra instance (typically located in `src/mastra/index.ts`):
|
|
108
29
|
|
|
109
30
|
```typescript
|
|
110
31
|
import { Mastra } from '@mastra/core'
|
|
@@ -115,190 +36,60 @@ export const mastra = new Mastra({
|
|
|
115
36
|
})
|
|
116
37
|
```
|
|
117
38
|
|
|
118
|
-
|
|
39
|
+
Once registered, it can be called from workflows, tools, or other agents, and has access to shared resources such as memory, logging, and observability features.
|
|
119
40
|
|
|
120
|
-
|
|
41
|
+
> **Tip:** Use [Studio](https://mastra.ai/docs/getting-started/studio) to test your agent with different messages, inspect tool calls and responses, and debug agent behavior.
|
|
121
42
|
|
|
122
|
-
|
|
43
|
+
> **Note:** Visit the [agent reference](https://mastra.ai/reference/agents/agent) for more information on available properties and configurations.
|
|
123
44
|
|
|
124
|
-
|
|
45
|
+
## Use your agent
|
|
125
46
|
|
|
126
|
-
|
|
47
|
+
After registration, retrieve your agent with [`mastra.getAgentById()`](https://mastra.ai/reference/core/getAgentById). Call `.generate()` for a complete response or `.stream()` to deliver tokens in real time. You can call agents from [workflow steps](https://mastra.ai/docs/workflows/agents-and-tools), [tools](https://mastra.ai/docs/agents/using-tools), the [Mastra Client](https://mastra.ai/reference/client-js/mastra-client), route handlers, [server adapters](https://mastra.ai/docs/server/server-adapters), or the command line. Visit the [guides section](https://mastra.ai/guides) to learn how to use agents in your framework of choice.
|
|
127
48
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
```typescript
|
|
131
|
-
const testAgent = mastra.getAgent('testAgent')
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
> **Info:** `mastra.getAgent()` is preferred over a direct import, since it provides access to the Mastra instance configuration (logger, telemetry, storage, registered agents, and vector stores).
|
|
135
|
-
|
|
136
|
-
## Generating responses
|
|
137
|
-
|
|
138
|
-
Agents can return results in two ways: generating the full output before returning it or streaming tokens in real time. Choose the approach that fits your use case: generate for short, internal responses or debugging, and stream to deliver pixels to end users as quickly as possible.
|
|
49
|
+
When referencing an agent from your Mastra instance, use `mastra.getAgentById()` to ensure it has access to shared services such as instance-level storage, logging, and agent registry. A directly imported agent can still work with its own local configuration, but it won't have access to those shared services.
|
|
139
50
|
|
|
140
51
|
**Generate**:
|
|
141
52
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
(The `role` defines the speaker for each message. Typical roles are `user` for human input, `assistant` for agent responses, and `system` for instructions.)
|
|
145
|
-
|
|
146
|
-
```typescript
|
|
147
|
-
const response = await testAgent.generate([
|
|
148
|
-
{ role: 'user', content: 'Help me organize my day' },
|
|
149
|
-
{ role: 'user', content: 'My day starts at 9am and finishes at 5.30pm' },
|
|
150
|
-
{ role: 'user', content: 'I take lunch between 12:30 and 13:30' },
|
|
151
|
-
{
|
|
152
|
-
role: 'user',
|
|
153
|
-
content: 'I have meetings Monday to Friday between 10:30 and 11:30',
|
|
154
|
-
},
|
|
155
|
-
])
|
|
53
|
+
Returns the full response after all tool calls and steps complete. The result includes `text`, `toolCalls`, `toolResults`, `steps`, and token `usage` statistics.
|
|
156
54
|
|
|
55
|
+
```ts
|
|
56
|
+
const agent = mastra.getAgentById('test-agent')
|
|
57
|
+
const response = await agent.generate('Help me organize my day')
|
|
157
58
|
console.log(response.text)
|
|
158
59
|
```
|
|
159
60
|
|
|
160
61
|
**Stream**:
|
|
161
62
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
(The `role` defines the speaker for each message. Typical roles are `user` for human input, `assistant` for agent responses, and `system` for instructions.)
|
|
63
|
+
Returns a stream you can consume as tokens arrive. The result exposes `textStream` for incremental output and promises for `toolCalls`, `toolResults`, `steps`, and token `usage` that resolve when the stream finishes.
|
|
165
64
|
|
|
166
|
-
```
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
{ role: 'user', content: 'My day starts at 9am and finishes at 5.30pm' },
|
|
170
|
-
{ role: 'user', content: 'I take lunch between 12:30 and 13:30' },
|
|
171
|
-
{
|
|
172
|
-
role: 'user',
|
|
173
|
-
content: 'I have meetings Monday to Friday between 10:30 and 11:30',
|
|
174
|
-
},
|
|
175
|
-
])
|
|
65
|
+
```ts
|
|
66
|
+
const agent = mastra.getAgentById('test-agent')
|
|
67
|
+
const stream = await agent.stream('Help me organize my day')
|
|
176
68
|
|
|
177
69
|
for await (const chunk of stream.textStream) {
|
|
178
70
|
process.stdout.write(chunk)
|
|
179
71
|
}
|
|
180
72
|
```
|
|
181
73
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
When streaming responses, the `onFinish()` callback runs after the LLM finishes generating its response and all tool executions are complete. It provides the final `text`, execution `steps`, `finishReason`, token `usage` statistics, and other metadata useful for monitoring or logging.
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
const stream = await testAgent.stream('Help me organize my day', {
|
|
188
|
-
onFinish: ({ steps, text, finishReason, usage }) => {
|
|
189
|
-
console.log({ steps, text, finishReason, usage })
|
|
190
|
-
},
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
for await (const chunk of stream.textStream) {
|
|
194
|
-
process.stdout.write(chunk)
|
|
195
|
-
}
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
> **Info:** Visit [.generate()](https://mastra.ai/reference/agents/generate) or [.stream()](https://mastra.ai/reference/streaming/agents/stream) for more information.
|
|
199
|
-
|
|
200
|
-
## Structured output
|
|
201
|
-
|
|
202
|
-
Agents can return structured, type-safe data using Zod or JSON Schema. The parsed result is available on `response.object`.
|
|
203
|
-
|
|
204
|
-
> **Info:** Visit [Structured Output](https://mastra.ai/docs/agents/structured-output) for more information.
|
|
205
|
-
|
|
206
|
-
## Analyzing images
|
|
207
|
-
|
|
208
|
-
Agents can analyze and describe images by processing both the visual content and any text within them. To enable image analysis, pass an object with `type: 'image'` and the image URL in the `content` array. You can combine image content with text prompts to guide the agent's analysis.
|
|
209
|
-
|
|
210
|
-
```typescript
|
|
211
|
-
const response = await testAgent.generate([
|
|
212
|
-
{
|
|
213
|
-
role: 'user',
|
|
214
|
-
content: [
|
|
215
|
-
{
|
|
216
|
-
type: 'image',
|
|
217
|
-
image: 'https://placebear.com/cache/395-205.jpg',
|
|
218
|
-
mimeType: 'image/jpeg',
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
type: 'text',
|
|
222
|
-
text: 'Describe the image in detail, and extract all the text in the image.',
|
|
223
|
-
},
|
|
224
|
-
],
|
|
225
|
-
},
|
|
226
|
-
])
|
|
227
|
-
|
|
228
|
-
console.log(response.text)
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
## Using `maxSteps`
|
|
232
|
-
|
|
233
|
-
The `maxSteps` parameter controls the maximum number of sequential LLM calls an agent can make. Each step includes generating a response, executing any tool calls, and processing the result. Limiting steps helps prevent infinite loops, reduce latency, and control token usage for agents that use tools. The default is 5, but can be increased:
|
|
234
|
-
|
|
235
|
-
```typescript
|
|
236
|
-
const response = await testAgent.generate('Help me organize my day', {
|
|
237
|
-
maxSteps: 10,
|
|
238
|
-
})
|
|
239
|
-
|
|
240
|
-
console.log(response.text)
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
## Using `onStepFinish`
|
|
244
|
-
|
|
245
|
-
You can monitor the progress of multi-step operations using the `onStepFinish` callback. This is useful for debugging or providing progress updates to users.
|
|
246
|
-
|
|
247
|
-
`onStepFinish` is only available when streaming or generating text without structured output.
|
|
248
|
-
|
|
249
|
-
```typescript
|
|
250
|
-
const response = await testAgent.generate('Help me organize my day', {
|
|
251
|
-
onStepFinish: ({ text, toolCalls, toolResults, finishReason, usage }) => {
|
|
252
|
-
console.log({ text, toolCalls, toolResults, finishReason, usage })
|
|
253
|
-
},
|
|
254
|
-
})
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
## Using tools
|
|
258
|
-
|
|
259
|
-
Agents can use tools to go beyond language generation, enabling structured interactions with external APIs and services. Tools allow agents to access data and perform clearly defined operations in a reliable, repeatable way.
|
|
260
|
-
|
|
261
|
-
```typescript
|
|
262
|
-
export const testAgent = new Agent({
|
|
263
|
-
id: 'test-agent',
|
|
264
|
-
name: 'Test Agent',
|
|
265
|
-
tools: { testTool },
|
|
266
|
-
})
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
> **Info:** Visit [Using Tools](https://mastra.ai/docs/agents/using-tools) for more information.
|
|
270
|
-
|
|
271
|
-
## Using `RequestContext`
|
|
272
|
-
|
|
273
|
-
Use `RequestContext` to access request-specific values. This lets you conditionally adjust behavior based on the context of the request.
|
|
274
|
-
|
|
275
|
-
```typescript
|
|
276
|
-
export type UserTier = {
|
|
277
|
-
'user-tier': 'enterprise' | 'pro'
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
export const testAgent = new Agent({
|
|
281
|
-
id: 'test-agent',
|
|
282
|
-
name: 'Test Agent',
|
|
283
|
-
model: ({ requestContext }) => {
|
|
284
|
-
const userTier = requestContext.get('user-tier') as UserTier['user-tier']
|
|
285
|
-
|
|
286
|
-
return userTier === 'enterprise' ? 'openai/gpt-5.4' : 'openai/gpt-5-mini'
|
|
287
|
-
},
|
|
288
|
-
})
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
> **Info:** See [Request Context](https://mastra.ai/docs/server/request-context) for more information.
|
|
74
|
+
## Expand your agent
|
|
292
75
|
|
|
293
|
-
|
|
76
|
+
Once your agent is running, use this table to find the right page for what you want to do next:
|
|
294
77
|
|
|
295
|
-
|
|
78
|
+
| Goal | Start here |
|
|
79
|
+
| -------------------------------------------------------------- | ---------------------------------------------------------------------- |
|
|
80
|
+
| Give your agent tools to call external APIs or services | [Tools](https://mastra.ai/docs/agents/using-tools) |
|
|
81
|
+
| Keep context and preferences across conversations | [Memory](https://mastra.ai/docs/memory/overview) |
|
|
82
|
+
| Get typed objects back instead of plain text | [Structured output](https://mastra.ai/docs/agents/structured-output) |
|
|
83
|
+
| Human-in-the-loop: Pause execution and wait for human approval | [Approval](https://mastra.ai/docs/agents/agent-approval) |
|
|
84
|
+
| Build a multi-agent network | [Supervisor agents](https://mastra.ai/docs/agents/supervisor-agents) |
|
|
85
|
+
| Register subagents | [Tools](https://mastra.ai/docs/agents/using-tools) |
|
|
86
|
+
| Intercept or transform messages before and after generation | [Processors](https://mastra.ai/docs/agents/processors) |
|
|
87
|
+
| Keep your agent safe | [Guardrails](https://mastra.ai/docs/agents/guardrails) |
|
|
88
|
+
| Swap instructions or models based on request context | [Dynamic configuration](https://mastra.ai/docs/server/request-context) |
|
|
89
|
+
| Add speech-to-text or text-to-speech | [Voice](https://mastra.ai/docs/agents/adding-voice) |
|
|
296
90
|
|
|
297
|
-
|
|
91
|
+
## Multi-agent systems
|
|
298
92
|
|
|
299
|
-
|
|
93
|
+
A multi-agent system uses multiple agents to solve a task that is too broad or too specialized for a single agent. Instead of building one agent with dozens of tools and a long instruction set, you split responsibilities across focused agents and let a coordinator bring results together.
|
|
300
94
|
|
|
301
|
-
|
|
302
|
-
- [Agent Memory](https://mastra.ai/docs/agents/agent-memory)
|
|
303
|
-
- [Supervisor Agents](https://mastra.ai/docs/agents/supervisor-agents)
|
|
304
|
-
- [Request Context](https://mastra.ai/docs/server/request-context)
|
|
95
|
+
Read the [conceptual overview of multi-agent systems](https://mastra.ai/guides/concepts/multi-agent-systems) to learn how you can apply different patterns with Mastra.
|