@mastra/memory 1.0.0-beta.9 → 1.0.0

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.
@@ -0,0 +1,100 @@
1
+ # Processors API Reference
2
+
3
+ > API reference for processors - 1 entries
4
+
5
+
6
+ ---
7
+
8
+ ## Reference: Token Limiter Processor
9
+
10
+ > Documentation for the TokenLimiterProcessor in Mastra, which limits the number of tokens in messages.
11
+
12
+ The `TokenLimiterProcessor` limits the number of tokens in messages. It can be used as both an input and output processor:
13
+
14
+ - **Input processor**: Filters historical messages to fit within the context window, prioritizing recent messages
15
+ - **Output processor**: Limits generated response tokens via streaming or non-streaming with configurable strategies for handling exceeded limits
16
+
17
+ ## Usage example
18
+
19
+ ```typescript
20
+ import { TokenLimiterProcessor } from "@mastra/core/processors";
21
+
22
+ const processor = new TokenLimiterProcessor({
23
+ limit: 1000,
24
+ strategy: "truncate",
25
+ countMode: "cumulative"
26
+ });
27
+ ```
28
+
29
+ ## Constructor parameters
30
+
31
+ ### Options
32
+
33
+ ## Returns
34
+
35
+ ## Error behavior
36
+
37
+ When used as an input processor, `TokenLimiterProcessor` throws a `TripWire` error in the following cases:
38
+
39
+ - **Empty messages**: If there are no messages to process, a TripWire is thrown because you cannot send an LLM request with no messages.
40
+ - **System messages exceed limit**: If system messages alone exceed the token limit, a TripWire is thrown because you cannot send an LLM request with only system messages and no user/assistant messages.
41
+
42
+ ```typescript
43
+ import { TripWire } from "@mastra/core/agent";
44
+
45
+ try {
46
+ await agent.generate("Hello");
47
+ } catch (error) {
48
+ if (error instanceof TripWire) {
49
+ console.log("Token limit error:", error.message);
50
+ }
51
+ }
52
+ ```
53
+
54
+ ## Extended usage example
55
+
56
+ ### As an input processor (limit context window)
57
+
58
+ Use `inputProcessors` to limit historical messages sent to the model, which helps stay within context window limits:
59
+
60
+ ```typescript title="src/mastra/agents/context-limited-agent.ts"
61
+ import { Agent } from "@mastra/core/agent";
62
+ import { Memory } from "@mastra/memory";
63
+ import { TokenLimiterProcessor } from "@mastra/core/processors";
64
+
65
+ export const agent = new Agent({
66
+ name: "context-limited-agent",
67
+ instructions: "You are a helpful assistant",
68
+ model: "openai/gpt-4o",
69
+ memory: new Memory({ /* ... */ }),
70
+ inputProcessors: [
71
+ new TokenLimiterProcessor({ limit: 4000 }) // Limits historical messages to ~4000 tokens
72
+ ]
73
+ });
74
+ ```
75
+
76
+ ### As an output processor (limit response length)
77
+
78
+ Use `outputProcessors` to limit the length of generated responses:
79
+
80
+ ```typescript title="src/mastra/agents/response-limited-agent.ts"
81
+ import { Agent } from "@mastra/core/agent";
82
+ import { TokenLimiterProcessor } from "@mastra/core/processors";
83
+
84
+ export const agent = new Agent({
85
+ name: "response-limited-agent",
86
+ instructions: "You are a helpful assistant",
87
+ model: "openai/gpt-4o",
88
+ outputProcessors: [
89
+ new TokenLimiterProcessor({
90
+ limit: 1000,
91
+ strategy: "truncate",
92
+ countMode: "cumulative"
93
+ })
94
+ ]
95
+ });
96
+ ```
97
+
98
+ ## Related
99
+
100
+ - [Guardrails](https://mastra.ai/docs/v1/agents/guardrails)