@mastra/memory 0.0.0-error-handler-fix-20251020202607 → 0.0.0-esbuild-bundle-worker-20260807182016

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.
Files changed (169) hide show
  1. package/CHANGELOG.md +4656 -3
  2. package/LICENSE.md +15 -0
  3. package/README.md +26 -1
  4. package/dist/_types/@internal_ai-sdk-v4/dist/index.d.ts +7450 -0
  5. package/dist/docs/SKILL.md +62 -0
  6. package/dist/docs/assets/SOURCE_MAP.json +11 -0
  7. package/dist/docs/references/docs-agents-agent-approval.md +664 -0
  8. package/dist/docs/references/docs-agents-networks.md +184 -0
  9. package/dist/docs/references/docs-capabilities-subagents.md +454 -0
  10. package/dist/docs/references/docs-evals-evals-with-memory.md +146 -0
  11. package/dist/docs/references/docs-long-running-agents-background-tasks.md +382 -0
  12. package/dist/docs/references/docs-long-running-agents-goals.md +118 -0
  13. package/dist/docs/references/docs-memory-memory-processors.md +385 -0
  14. package/dist/docs/references/docs-memory-message-history.md +348 -0
  15. package/dist/docs/references/docs-memory-multi-user-threads.md +208 -0
  16. package/dist/docs/references/docs-memory-observational-memory.md +835 -0
  17. package/dist/docs/references/docs-memory-overview.md +266 -0
  18. package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
  19. package/dist/docs/references/docs-memory-working-memory.md +431 -0
  20. package/dist/docs/references/docs-storage-overview.md +214 -0
  21. package/dist/docs/references/reference-core-getMemory.md +51 -0
  22. package/dist/docs/references/reference-core-listMemory.md +57 -0
  23. package/dist/docs/references/reference-file-based-agents-memory.md +58 -0
  24. package/dist/docs/references/reference-memory-clone-utilities.md +203 -0
  25. package/dist/docs/references/reference-memory-cloneThread.md +173 -0
  26. package/dist/docs/references/reference-memory-createThread.md +70 -0
  27. package/dist/docs/references/reference-memory-getThreadById.md +26 -0
  28. package/dist/docs/references/reference-memory-listThreads.md +147 -0
  29. package/dist/docs/references/reference-memory-memory-class.md +148 -0
  30. package/dist/docs/references/reference-memory-observational-memory.md +877 -0
  31. package/dist/docs/references/reference-memory-summarizeConversation.md +99 -0
  32. package/dist/docs/references/reference-memory-summarizeThread.md +93 -0
  33. package/dist/docs/references/reference-processors-token-limiter-processor.md +158 -0
  34. package/dist/docs/references/reference-storage-dsql.md +430 -0
  35. package/dist/docs/references/reference-storage-dynamodb.md +284 -0
  36. package/dist/docs/references/reference-storage-libsql.md +143 -0
  37. package/dist/docs/references/reference-storage-mongodb.md +267 -0
  38. package/dist/docs/references/reference-storage-postgresql.md +531 -0
  39. package/dist/docs/references/reference-storage-redis.md +268 -0
  40. package/dist/docs/references/reference-storage-upstash.md +162 -0
  41. package/dist/docs/references/reference-vectors-libsql.md +307 -0
  42. package/dist/docs/references/reference-vectors-mongodb.md +567 -0
  43. package/dist/docs/references/reference-vectors-pg.md +430 -0
  44. package/dist/docs/references/reference-vectors-upstash.md +296 -0
  45. package/dist/index.cjs +35 -893
  46. package/dist/index.d.ts +465 -62
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +2 -887
  49. package/dist/processors/index.cjs +32 -165
  50. package/dist/processors/index.d.ts +1 -2
  51. package/dist/processors/index.d.ts.map +1 -1
  52. package/dist/processors/index.js +2 -158
  53. package/dist/processors/observational-memory/activation-ttl.d.ts +4 -0
  54. package/dist/processors/observational-memory/activation-ttl.d.ts.map +1 -0
  55. package/dist/processors/observational-memory/anchor-ids.d.ts +4 -0
  56. package/dist/processors/observational-memory/anchor-ids.d.ts.map +1 -0
  57. package/dist/processors/observational-memory/buffering-coordinator.d.ts +61 -0
  58. package/dist/processors/observational-memory/buffering-coordinator.d.ts.map +1 -0
  59. package/dist/processors/observational-memory/built-in-extractors.d.ts +15 -0
  60. package/dist/processors/observational-memory/built-in-extractors.d.ts.map +1 -0
  61. package/dist/processors/observational-memory/constants.d.ts +74 -0
  62. package/dist/processors/observational-memory/constants.d.ts.map +1 -0
  63. package/dist/processors/observational-memory/date-utils.d.ts +41 -0
  64. package/dist/processors/observational-memory/date-utils.d.ts.map +1 -0
  65. package/dist/processors/observational-memory/debug.d.ts +3 -0
  66. package/dist/processors/observational-memory/debug.d.ts.map +1 -0
  67. package/dist/processors/observational-memory/extracted-values.d.ts +44 -0
  68. package/dist/processors/observational-memory/extracted-values.d.ts.map +1 -0
  69. package/dist/processors/observational-memory/extraction-runner.d.ts +22 -0
  70. package/dist/processors/observational-memory/extraction-runner.d.ts.map +1 -0
  71. package/dist/processors/observational-memory/extractor.d.ts +76 -0
  72. package/dist/processors/observational-memory/extractor.d.ts.map +1 -0
  73. package/dist/processors/observational-memory/index.d.ts +30 -0
  74. package/dist/processors/observational-memory/index.d.ts.map +1 -0
  75. package/dist/processors/observational-memory/internal-request-context.d.ts +17 -0
  76. package/dist/processors/observational-memory/internal-request-context.d.ts.map +1 -0
  77. package/dist/processors/observational-memory/markers.d.ts +118 -0
  78. package/dist/processors/observational-memory/markers.d.ts.map +1 -0
  79. package/dist/processors/observational-memory/message-utils.d.ts +83 -0
  80. package/dist/processors/observational-memory/message-utils.d.ts.map +1 -0
  81. package/dist/processors/observational-memory/model-by-input-tokens.d.ts +14 -0
  82. package/dist/processors/observational-memory/model-by-input-tokens.d.ts.map +1 -0
  83. package/dist/processors/observational-memory/model-context.d.ts +2 -0
  84. package/dist/processors/observational-memory/model-context.d.ts.map +1 -0
  85. package/dist/processors/observational-memory/observation-groups.d.ts +15 -0
  86. package/dist/processors/observational-memory/observation-groups.d.ts.map +1 -0
  87. package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts +39 -0
  88. package/dist/processors/observational-memory/observation-strategies/async-buffer.d.ts.map +1 -0
  89. package/dist/processors/observational-memory/observation-strategies/base.d.ts +122 -0
  90. package/dist/processors/observational-memory/observation-strategies/base.d.ts.map +1 -0
  91. package/dist/processors/observational-memory/observation-strategies/index.d.ts +7 -0
  92. package/dist/processors/observational-memory/observation-strategies/index.d.ts.map +1 -0
  93. package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts +43 -0
  94. package/dist/processors/observational-memory/observation-strategies/resource-scoped.d.ts.map +1 -0
  95. package/dist/processors/observational-memory/observation-strategies/sync.d.ts +41 -0
  96. package/dist/processors/observational-memory/observation-strategies/sync.d.ts.map +1 -0
  97. package/dist/processors/observational-memory/observation-strategies/types.d.ts +103 -0
  98. package/dist/processors/observational-memory/observation-strategies/types.d.ts.map +1 -0
  99. package/dist/processors/observational-memory/observation-turn/index.d.ts +4 -0
  100. package/dist/processors/observational-memory/observation-turn/index.d.ts.map +1 -0
  101. package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts +9 -0
  102. package/dist/processors/observational-memory/observation-turn/load-memory-context.d.ts.map +1 -0
  103. package/dist/processors/observational-memory/observation-turn/step.d.ts +53 -0
  104. package/dist/processors/observational-memory/observation-turn/step.d.ts.map +1 -0
  105. package/dist/processors/observational-memory/observation-turn/turn.d.ts +139 -0
  106. package/dist/processors/observational-memory/observation-turn/turn.d.ts.map +1 -0
  107. package/dist/processors/observational-memory/observation-turn/types.d.ts +46 -0
  108. package/dist/processors/observational-memory/observation-turn/types.d.ts.map +1 -0
  109. package/dist/processors/observational-memory/observation-utils.d.ts +16 -0
  110. package/dist/processors/observational-memory/observation-utils.d.ts.map +1 -0
  111. package/dist/processors/observational-memory/observational-memory.d.ts +930 -0
  112. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -0
  113. package/dist/processors/observational-memory/observer-agent.d.ts +190 -0
  114. package/dist/processors/observational-memory/observer-agent.d.ts.map +1 -0
  115. package/dist/processors/observational-memory/observer-runner.d.ts +141 -0
  116. package/dist/processors/observational-memory/observer-runner.d.ts.map +1 -0
  117. package/dist/processors/observational-memory/operation-registry.d.ts +14 -0
  118. package/dist/processors/observational-memory/operation-registry.d.ts.map +1 -0
  119. package/dist/processors/observational-memory/processor.d.ts +70 -0
  120. package/dist/processors/observational-memory/processor.d.ts.map +1 -0
  121. package/dist/processors/observational-memory/reflector-agent.d.ts +62 -0
  122. package/dist/processors/observational-memory/reflector-agent.d.ts.map +1 -0
  123. package/dist/processors/observational-memory/reflector-runner.d.ts +133 -0
  124. package/dist/processors/observational-memory/reflector-runner.d.ts.map +1 -0
  125. package/dist/processors/observational-memory/repro-capture.d.ts +33 -0
  126. package/dist/processors/observational-memory/repro-capture.d.ts.map +1 -0
  127. package/dist/processors/observational-memory/retry.d.ts +63 -0
  128. package/dist/processors/observational-memory/retry.d.ts.map +1 -0
  129. package/dist/processors/observational-memory/string-utils.d.ts +13 -0
  130. package/dist/processors/observational-memory/string-utils.d.ts.map +1 -0
  131. package/dist/processors/observational-memory/summarize.d.ts +92 -0
  132. package/dist/processors/observational-memory/summarize.d.ts.map +1 -0
  133. package/dist/processors/observational-memory/temporal-markers.d.ts +4 -0
  134. package/dist/processors/observational-memory/temporal-markers.d.ts.map +1 -0
  135. package/dist/processors/observational-memory/temporary-memory.d.ts +8 -0
  136. package/dist/processors/observational-memory/temporary-memory.d.ts.map +1 -0
  137. package/dist/processors/observational-memory/thresholds.d.ts +52 -0
  138. package/dist/processors/observational-memory/thresholds.d.ts.map +1 -0
  139. package/dist/processors/observational-memory/token-counter.d.ts +57 -0
  140. package/dist/processors/observational-memory/token-counter.d.ts.map +1 -0
  141. package/dist/processors/observational-memory/tool-result-helpers.d.ts +12 -0
  142. package/dist/processors/observational-memory/tool-result-helpers.d.ts.map +1 -0
  143. package/dist/processors/observational-memory/tracing.d.ts +17 -0
  144. package/dist/processors/observational-memory/tracing.d.ts.map +1 -0
  145. package/dist/processors/observational-memory/types.d.ts +997 -0
  146. package/dist/processors/observational-memory/types.d.ts.map +1 -0
  147. package/dist/processors/observational-memory/working-memory-extractor.d.ts +5 -0
  148. package/dist/processors/observational-memory/working-memory-extractor.d.ts.map +1 -0
  149. package/dist/processors/working-memory-state/index.d.ts +2 -0
  150. package/dist/processors/working-memory-state/index.d.ts.map +1 -0
  151. package/dist/processors/working-memory-state/processor.d.ts +53 -0
  152. package/dist/processors/working-memory-state/processor.d.ts.map +1 -0
  153. package/dist/src-C1tlhGeW.js +28559 -0
  154. package/dist/src-C1tlhGeW.js.map +1 -0
  155. package/dist/src-DqoifKIy.cjs +28813 -0
  156. package/dist/src-DqoifKIy.cjs.map +1 -0
  157. package/dist/tools/om-tools.d.ts +172 -0
  158. package/dist/tools/om-tools.d.ts.map +1 -0
  159. package/dist/tools/working-memory.d.ts +33 -28
  160. package/dist/tools/working-memory.d.ts.map +1 -1
  161. package/package.json +36 -28
  162. package/dist/index.cjs.map +0 -1
  163. package/dist/index.js.map +0 -1
  164. package/dist/processors/index.cjs.map +0 -1
  165. package/dist/processors/index.js.map +0 -1
  166. package/dist/processors/token-limiter.d.ts +0 -32
  167. package/dist/processors/token-limiter.d.ts.map +0 -1
  168. package/dist/processors/tool-call-filter.d.ts +0 -20
  169. package/dist/processors/tool-call-filter.d.ts.map +0 -1
@@ -0,0 +1,99 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # summarizeConversation()
4
+
5
+ The standalone `summarizeConversation()` function summarizes a conversation in one shot. It distills the messages you pass in with the same Observer plumbing that powers [Observational Memory](https://mastra.ai/reference/memory/observational-memory): without Observational Memory attached to an agent, and without reading from or writing to storage.
6
+
7
+ Nothing is written back to memory. The summary and extracted values are returned to you (and to each extractor's `onExtracted` hook), so you decide where they go: for example your own database.
8
+
9
+ Use this when you already have the messages in hand and want explicit control over what gets summarized. For a stored thread identified by ID, use [`Memory.summarizeThread()`](https://mastra.ai/reference/memory/summarizeThread), which loads the messages for you.
10
+
11
+ ## Usage example
12
+
13
+ ```typescript
14
+ import { summarizeConversation } from '@mastra/memory'
15
+
16
+ const result = await summarizeConversation({
17
+ model: 'openai/gpt-5-mini',
18
+ messages,
19
+ instructions: 'Summarize this voicemail call for the business owner.',
20
+ })
21
+ ```
22
+
23
+ ## Parameters
24
+
25
+ **model** (`string | LanguageModel | DynamicModel`): Model that runs the summarization, such as a router string like '\_\_GATEWAY\_OPENAI\_MODEL\_MINI\_\_'.
26
+
27
+ **messages** (`MastraDBMessage[]`): The conversation to summarize. When empty, the function returns an empty result without calling the model.
28
+
29
+ **instructions** (`string`): Extra guidance appended to the summarizer's system prompt, such as what to focus on or who the summary is for.
30
+
31
+ **extract** (`Extractor[]`): Extractors to run over the conversation. Extractors with a Zod schema run as a follow-up structured output call; schema-less extractors are extracted inline. Each extractor's onExtracted hook fires with the extracted value.
32
+
33
+ **threadId** (`string`): Thread the conversation belongs to. Defaults to the first threadId found on messages. Passed to extractor contexts.
34
+
35
+ **resourceId** (`string`): Resource (user) the conversation belongs to. Defaults to the first resourceId found on messages. Passed to extractor contexts.
36
+
37
+ **memory** (`Memory`): Memory instance forwarded to extractor contexts. Set automatically when called through Memory.summarizeThread().
38
+
39
+ **mastra** (`Mastra`): Mastra instance used to resolve custom gateway models. Set automatically when called through Memory.summarizeThread().
40
+
41
+ **requestContext** (`RequestContext`): Request context forwarded to the summarizer model and extractor hooks.
42
+
43
+ **abortSignal** (`AbortSignal`): Signal to cancel the summarization call.
44
+
45
+ ## Returns
46
+
47
+ **summary** (`string`): The distilled observations produced from the conversation, in dense bullet form.
48
+
49
+ **extracted** (`Record<string, unknown>`): Values produced by extract extractors, keyed by extractor slug.
50
+
51
+ **extractionFailures** (`{ slug: string; error: string }[]`): Extractors that failed to produce a valid value, with the reason. Only present when at least one extractor failed.
52
+
53
+ **usage** (`{ inputTokens?: number; outputTokens?: number; totalTokens?: number }`): Token usage of the summarization call.
54
+
55
+ ## Extended usage example
56
+
57
+ The following example summarizes an in-flight voice session from messages the application already holds, and extracts a structured record:
58
+
59
+ ```typescript
60
+ import { summarizeConversation, Extractor } from '@mastra/memory'
61
+ import type { MastraDBMessage } from '@mastra/core/agent'
62
+ import { z } from 'zod'
63
+ import { sessionRecords } from './db'
64
+
65
+ const sessionSummary = new Extractor({
66
+ name: 'session-summary',
67
+ instructions: 'Return a concise summary of the session.',
68
+ schema: z.object({
69
+ summary: z.string(),
70
+ sentiment: z.enum(['positive', 'neutral', 'negative']),
71
+ followUps: z.array(z.string()),
72
+ }),
73
+ metadataKeyPath: false, // don't persist into memory metadata — the hook owns storage
74
+ onExtracted: async ({ current, threadId }) => {
75
+ await sessionRecords.upsert({ sessionId: threadId, record: current })
76
+ },
77
+ })
78
+
79
+ export async function onSessionEnd(messages: MastraDBMessage[]) {
80
+ const { summary, extracted, extractionFailures } = await summarizeConversation({
81
+ model: 'openai/gpt-5-mini',
82
+ messages,
83
+ instructions: 'Summarize this support session for the account manager.',
84
+ extract: [sessionSummary],
85
+ })
86
+
87
+ if (extractionFailures?.length) {
88
+ console.warn('Some extractors failed', extractionFailures)
89
+ }
90
+
91
+ return { summary, extracted }
92
+ }
93
+ ```
94
+
95
+ ### Related
96
+
97
+ - [.summarizeThread()](https://mastra.ai/reference/memory/summarizeThread)
98
+ - [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
99
+ - [Observational Memory](https://mastra.ai/reference/memory/observational-memory)
@@ -0,0 +1,93 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # Memory.summarizeThread()
4
+
5
+ The `.summarizeThread()` method summarizes a thread's conversation in one shot. It loads the thread's messages from storage and distills them with the same Observer plumbing that powers [Observational Memory](https://mastra.ai/reference/memory/observational-memory): as a standalone call, without Observational Memory attached to an agent.
6
+
7
+ Messages load page-by-page starting from the newest, bounded by `lastMessages` and `maxInputTokens`, so summarizing a long thread doesn't read its entire history from storage.
8
+
9
+ Nothing is written back to memory. The summary and extracted values are returned to you (and to each extractor's `onExtracted` hook), so you decide where they go: for example your own database.
10
+
11
+ Use this when a session ends and you want a summary or structured extraction of the whole conversation, such as a voice call at hang-up. For messages you already have in hand (without loading them from a thread), use the standalone [`summarizeConversation()`](https://mastra.ai/reference/memory/summarizeConversation) function instead. It takes the same options with `messages` in place of `threadId`.
12
+
13
+ ## Usage example
14
+
15
+ ```typescript
16
+ const result = await memory.summarizeThread({
17
+ model: 'openai/gpt-5-mini',
18
+ threadId: 'thread-123',
19
+ instructions: 'Summarize this voicemail call for the business owner.',
20
+ })
21
+ ```
22
+
23
+ ## Parameters
24
+
25
+ **threadId** (`string`): The unique identifier of the thread to summarize.
26
+
27
+ **model** (`string | LanguageModel | DynamicModel`): Model that runs the summarization, such as a router string like '\_\_GATEWAY\_OPENAI\_MODEL\_MINI\_\_'.
28
+
29
+ **resourceId** (`string`): ID of the resource that owns the thread. If provided, validates thread ownership. Also passed to extractor contexts.
30
+
31
+ **lastMessages** (`number`): Only summarize the last N messages of the thread. By default the whole thread is loaded, bounded by maxInputTokens.
32
+
33
+ **maxInputTokens** (`number`): Stop loading older messages once the collected messages exceed this estimated token count. The newest message is always included. (Default: `1000000`)
34
+
35
+ **instructions** (`string`): Extra guidance appended to the summarizer's system prompt, such as what to focus on or who the summary is for.
36
+
37
+ **extract** (`Extractor[]`): Extractors to run over the conversation. Extractors with a Zod schema run as a follow-up structured output call; schema-less extractors are extracted inline. Each extractor's onExtracted hook fires with the extracted value.
38
+
39
+ **requestContext** (`RequestContext`): Request context forwarded to the summarizer model and extractor hooks.
40
+
41
+ **abortSignal** (`AbortSignal`): Signal to cancel the summarization call.
42
+
43
+ ## Returns
44
+
45
+ **summary** (`string`): The distilled observations produced from the conversation, in dense bullet form.
46
+
47
+ **extracted** (`Record<string, unknown>`): Values produced by extract extractors, keyed by extractor slug.
48
+
49
+ **extractionFailures** (`{ slug: string; error: string }[]`): Extractors that failed to produce a valid value, with the reason. Only present when at least one extractor failed.
50
+
51
+ **usage** (`{ inputTokens?: number; outputTokens?: number; totalTokens?: number }`): Token usage of the summarization call.
52
+
53
+ ## Extended usage example
54
+
55
+ The following example summarizes a finished voice call and stores a structured record in the application's own database:
56
+
57
+ ```typescript
58
+ import { Extractor } from '@mastra/memory'
59
+ import { z } from 'zod'
60
+ import { memory } from './mastra/memory'
61
+ import { callRecords } from './db'
62
+
63
+ const callSummary = new Extractor({
64
+ name: 'call-summary',
65
+ instructions: 'Return a concise summary of the call.',
66
+ schema: z.object({
67
+ summary: z.string(),
68
+ sentiment: z.enum(['positive', 'neutral', 'negative']),
69
+ requestedServices: z.array(z.string()),
70
+ }),
71
+ metadataKeyPath: false, // don't persist into memory metadata — the hook owns storage
72
+ onExtracted: async ({ current, threadId, resourceId }) => {
73
+ await callRecords.upsert({ callId: threadId, callerId: resourceId, record: current })
74
+ },
75
+ })
76
+
77
+ export async function onCallEnd(threadId: string, resourceId: string) {
78
+ await memory.summarizeThread({
79
+ model: 'openai/gpt-5-mini',
80
+ threadId,
81
+ resourceId,
82
+ instructions: 'Summarize this voicemail call for the business owner.',
83
+ extract: [callSummary],
84
+ })
85
+ }
86
+ ```
87
+
88
+ ### Related
89
+
90
+ - [summarizeConversation()](https://mastra.ai/reference/memory/summarizeConversation)
91
+ - [Memory Class Reference](https://mastra.ai/reference/memory/memory-class)
92
+ - [Observational Memory](https://mastra.ai/reference/memory/observational-memory)
93
+ - [.recall()](https://mastra.ai/reference/memory/recall)
@@ -0,0 +1,158 @@
1
+ > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
+
3
+ # TokenLimiterProcessor
4
+
5
+ The `TokenLimiterProcessor` limits the number of tokens in messages. It can be used as an input, per-step input, and output processor:
6
+
7
+ - **Input processor** (`processInput`): Filters historical messages to fit within the context window before the agentic loop starts, prioritizing recent messages
8
+ - **Per-step input processor** (`processInputStep`): Prunes messages at each step of a multi-step agent workflow, preventing unbounded token growth when tools trigger additional LLM calls
9
+ - **Output processor**: Limits generated response tokens via streaming or non-streaming with configurable strategies for handling exceeded limits
10
+
11
+ ## Usage example
12
+
13
+ ```typescript
14
+ import { TokenLimiterProcessor } from '@mastra/core/processors'
15
+
16
+ const processor = new TokenLimiterProcessor({
17
+ limit: 1000,
18
+ strategy: 'truncate',
19
+ countMode: 'cumulative',
20
+ })
21
+ ```
22
+
23
+ ## Constructor parameters
24
+
25
+ **options** (`number | Options`): Either a simple number for token limit, or configuration options object
26
+
27
+ **options.limit** (`number`): Maximum number of tokens to allow in the response
28
+
29
+ **options.encoding** (`TiktokenBPE`): Optional encoding to use. Defaults to o200k\_base which is used by gpt-5.1
30
+
31
+ **options.strategy** (`'truncate' | 'abort'`): Strategy when token limit is reached: 'truncate' stops emitting chunks, 'abort' calls abort() to stop the stream
32
+
33
+ **options.countMode** (`'cumulative' | 'part'`): Whether to count tokens from the beginning of the stream or just the current part: 'cumulative' counts all tokens from start, 'part' only counts tokens in current part
34
+
35
+ **options.trimMode** (`'best-fit' | 'contiguous'`): Controls how messages are trimmed when exceeding the token limit: 'best-fit' keeps as many messages as possible (may create gaps), 'contiguous' stops at the first message that does not fit, ensuring a continuous suffix of conversation history
36
+
37
+ ## Returns
38
+
39
+ **id** (`string`): Processor identifier set to 'token-limiter'
40
+
41
+ **name** (`string`): Optional processor display name
42
+
43
+ **processInput** (`(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never }) => Promise<MastraDBMessage[]>`): Filters input messages to fit within token limit before the agentic loop starts, prioritizing recent messages while preserving system messages
44
+
45
+ **processInputStep** (`(args: ProcessInputStepArgs) => Promise<void>`): Prunes messages at each step of the agentic loop (including tool call continuations) to keep the conversation within the token limit. Mutates the messageList directly by removing oldest messages first while preserving system messages.
46
+
47
+ **processOutputStream** (`(args: ProcessOutputStreamArgs) => Promise<ChunkType | null>`): Processes streaming output parts to limit token count during streaming. Only text and object parts count against the limit and can be withheld; lifecycle, reasoning and tool parts always pass through.
48
+
49
+ **processOutputResult** (`(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never }) => Promise<MastraDBMessage[]>`): Processes final output results to limit token count in non-streaming scenarios
50
+
51
+ **getMaxTokens** (`() => number`): Get the maximum token limit
52
+
53
+ ## Output stream behavior
54
+
55
+ As an output processor, only parts that carry generated output count against the limit: `text-delta` and `object`. Lifecycle parts (such as `step-start`), reasoning deltas, response metadata, and tool parts (`tool-call`, `tool-result`) are neither counted nor withheld, so tool calls always reach the agentic loop and get executed.
56
+
57
+ With the default `truncate` strategy, the first time output is withheld the processor emits a transient `data-token-limit-reached` part on the stream:
58
+
59
+ ```typescript
60
+ for await (const part of stream.fullStream) {
61
+ if (part.type === 'data-token-limit-reached') {
62
+ console.log('output truncated at', part.data.limit, 'tokens')
63
+ }
64
+ }
65
+ ```
66
+
67
+ ## Error behavior
68
+
69
+ When used as an input processor (both `processInput` and `processInputStep`), `TokenLimiterProcessor` throws a `TripWire` error in the following cases:
70
+
71
+ - **Empty messages**: If there are no messages to process, a TripWire is thrown because you can't send an LLM request with no messages.
72
+ - **System messages exceed limit**: If system messages alone exceed the token limit, a TripWire is thrown because you can't send an LLM request with only system messages and no user/assistant messages.
73
+
74
+ ```typescript
75
+ import { TripWire } from '@mastra/core/agent'
76
+
77
+ try {
78
+ await agent.generate('Hello')
79
+ } catch (error) {
80
+ if (error instanceof TripWire) {
81
+ console.log('Token limit error:', error.message)
82
+ }
83
+ }
84
+ ```
85
+
86
+ ## Extended usage example
87
+
88
+ ### As an input processor (limit context window)
89
+
90
+ Use `inputProcessors` to limit historical messages sent to the model, which helps stay within context window limits:
91
+
92
+ ```typescript
93
+ import { Agent } from '@mastra/core/agent'
94
+ import { Memory } from '@mastra/memory'
95
+ import { TokenLimiterProcessor } from '@mastra/core/processors'
96
+
97
+ export const agent = new Agent({
98
+ id: 'context-limited-agent',
99
+ name: 'context-limited-agent',
100
+ instructions: 'You are a helpful assistant',
101
+ model: 'openai/gpt-5.6-sol',
102
+ memory: new Memory({/* ... */}),
103
+ inputProcessors: [
104
+ new TokenLimiterProcessor({ limit: 4000 }), // Limits historical messages to ~4000 tokens
105
+ ],
106
+ })
107
+ ```
108
+
109
+ ### As a per-step input processor (limit multi-step token growth)
110
+
111
+ When an agent uses tools across multiple steps (e.g. `maxSteps > 1`), each step accumulates conversation history from all previous steps. Use `inputProcessors` to also limit tokens at each step of the agentic loop. The `TokenLimiterProcessor` automatically applies to both the initial input and every subsequent step:
112
+
113
+ ```typescript
114
+ import { Agent } from '@mastra/core/agent'
115
+ import { TokenLimiterProcessor } from '@mastra/core/processors'
116
+
117
+ export const agent = new Agent({
118
+ id: 'multi-step-agent',
119
+ name: 'multi-step-agent',
120
+ instructions: 'You are a helpful research assistant with access to tools',
121
+ model: 'openai/gpt-5.6-sol',
122
+ inputProcessors: [
123
+ new TokenLimiterProcessor({ limit: 8000 }), // Applied at every step
124
+ ],
125
+ })
126
+
127
+ // Each tool call step will be limited to ~8000 input tokens
128
+ const result = await agent.generate('Research this topic using your tools', {
129
+ maxSteps: 10,
130
+ })
131
+ ```
132
+
133
+ ### As an output processor (limit response length)
134
+
135
+ Use `outputProcessors` to limit the length of generated responses:
136
+
137
+ ```typescript
138
+ import { Agent } from '@mastra/core/agent'
139
+ import { TokenLimiterProcessor } from '@mastra/core/processors'
140
+
141
+ export const agent = new Agent({
142
+ id: 'response-limited-agent',
143
+ name: 'response-limited-agent',
144
+ instructions: 'You are a helpful assistant',
145
+ model: 'openai/gpt-5.6-sol',
146
+ outputProcessors: [
147
+ new TokenLimiterProcessor({
148
+ limit: 1000,
149
+ strategy: 'truncate',
150
+ countMode: 'cumulative',
151
+ }),
152
+ ],
153
+ })
154
+ ```
155
+
156
+ ## Related
157
+
158
+ - [Guardrails](https://mastra.ai/docs/agents/guardrails)