0
# Memory and Chat History
1
2
Systems for managing conversation state, storing chat history, and maintaining context across interactions with various storage backends. Memory components enable stateful conversations and context-aware applications.
3
4
## Capabilities
5
6
### Base Memory Classes
7
8
Foundation classes for creating and using memory in LangChain applications.
9
10
```typescript { .api }
11
/**
12
* Base memory class - all memory implementations inherit from this
13
*/
14
abstract class BaseMemory {
15
/** Return list of memory keys */
16
abstract get memoryKeys(): string[];
17
18
/** Load memory variables from storage */
19
abstract loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
20
21
/** Save context to memory */
22
abstract saveContext(
23
inputValues: InputValues,
24
outputValues: OutputValues
25
): Promise<void>;
26
27
/** Clear memory contents */
28
clear(): Promise<void>;
29
}
30
31
/**
32
* Base chat memory class with message history support
33
*/
34
abstract class BaseChatMemory extends BaseMemory {
35
/** Chat message history storage */
36
chatHistory: BaseListChatMessageHistory;
37
38
/** Key for returning messages */
39
returnMessages: boolean;
40
41
/** Key for input in memory */
42
inputKey?: string;
43
44
/** Key for output in memory */
45
outputKey?: string;
46
47
constructor(fields?: BaseChatMemoryInput);
48
49
/** Get messages from chat history */
50
getChatMessages(): Promise<BaseMessageInterface[]>;
51
52
/** Add user message to history */
53
addUserMessage(message: string): Promise<void>;
54
55
/** Add AI message to history */
56
addAIChatMessage(message: string): Promise<void>;
57
58
clear(): Promise<void>;
59
}
60
```
61
62
### Buffer Memory
63
64
Simple memory implementation that stores all conversation history in a buffer.
65
66
```typescript { .api }
67
/**
68
* Simple buffer memory that stores conversation history
69
*/
70
class BufferMemory extends BaseChatMemory {
71
constructor(fields?: BufferMemoryInput);
72
73
/** Key for memory content */
74
memoryKey: string;
75
76
/** Prefix for human messages */
77
humanPrefix: string;
78
79
/** Prefix for AI messages */
80
aiPrefix: string;
81
82
get memoryKeys(): string[];
83
84
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
85
86
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
87
}
88
89
/**
90
* Buffer memory with sliding window - keeps only recent messages
91
*/
92
class BufferWindowMemory extends BaseChatMemory {
93
constructor(fields?: BufferWindowMemoryInput);
94
95
/** Key for memory content */
96
memoryKey: string;
97
98
/** Number of messages to keep in window */
99
k: number;
100
101
/** Prefix for human messages */
102
humanPrefix: string;
103
104
/** Prefix for AI messages */
105
aiPrefix: string;
106
107
get memoryKeys(): string[];
108
109
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
110
111
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
112
}
113
114
/**
115
* Token-based buffer memory with token limit
116
*/
117
class ConversationTokenBufferMemory extends BaseChatMemory {
118
constructor(fields: ConversationTokenBufferMemoryInput);
119
120
/** Language model for token counting */
121
llm: BaseLanguageModelInterface;
122
123
/** Maximum number of tokens to keep */
124
maxTokenLimit: number;
125
126
/** Key for memory content */
127
memoryKey: string;
128
129
/** Prefix for human messages */
130
humanPrefix: string;
131
132
/** Prefix for AI messages */
133
aiPrefix: string;
134
135
get memoryKeys(): string[];
136
137
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
138
139
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
140
141
/** Get token count for messages */
142
getTokenCount(messages: BaseMessageInterface[]): Promise<number>;
143
}
144
```
145
146
**Usage Example:**
147
148
```typescript
149
import { BufferMemory, BufferWindowMemory } from "langchain/memory";
150
import { ChatMessageHistory } from "langchain/stores/message/in_memory";
151
152
// Simple buffer memory
153
const memory = new BufferMemory({
154
memoryKey: "chat_history",
155
returnMessages: true,
156
});
157
158
await memory.saveContext(
159
{ input: "Hi, I'm Alice" },
160
{ output: "Hello Alice! How can I help you today?" }
161
);
162
163
const variables = await memory.loadMemoryVariables({});
164
console.log(variables.chat_history);
165
166
// Window memory - keeps only last 5 exchanges
167
const windowMemory = new BufferWindowMemory({
168
k: 5,
169
memoryKey: "history",
170
returnMessages: false,
171
});
172
173
await windowMemory.saveContext(
174
{ input: "What's the weather like?" },
175
{ output: "I don't have real-time weather data." }
176
);
177
```
178
179
### Summary Memory
180
181
Memory implementations that summarize conversation history to manage long conversations.
182
183
```typescript { .api }
184
/**
185
* Base conversation summary memory
186
*/
187
abstract class BaseConversationSummaryMemory extends BaseChatMemory {
188
/** Language model for summarization */
189
llm: BaseLanguageModelInterface;
190
191
/** Prompt for summarization */
192
prompt: BasePromptTemplate;
193
194
/** Key for summary in memory */
195
summaryMessageKey: string;
196
197
constructor(fields: BaseConversationSummaryMemoryInput);
198
199
/** Create summary of messages */
200
abstract predictNewSummary(
201
messages: BaseMessageInterface[],
202
existingSummary: string
203
): Promise<string>;
204
}
205
206
/**
207
* Memory that maintains a summary of the conversation
208
*/
209
class ConversationSummaryMemory extends BaseConversationSummaryMemory {
210
constructor(fields: ConversationSummaryMemoryInput);
211
212
/** Buffer for storing messages */
213
buffer: string;
214
215
get memoryKeys(): string[];
216
217
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
218
219
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
220
221
predictNewSummary(
222
messages: BaseMessageInterface[],
223
existingSummary: string
224
): Promise<string>;
225
}
226
227
/**
228
* Memory that maintains both summary and recent buffer
229
*/
230
class ConversationSummaryBufferMemory extends BaseConversationSummaryMemory {
231
constructor(fields: ConversationSummaryBufferMemoryInput);
232
233
/** Maximum token limit for buffer */
234
maxTokenLimit: number;
235
236
/** Buffer for recent messages */
237
movingSummaryBuffer: string;
238
239
get memoryKeys(): string[];
240
241
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
242
243
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
244
245
predictNewSummary(
246
messages: BaseMessageInterface[],
247
existingSummary: string
248
): Promise<string>;
249
250
/** Prune buffer if it exceeds token limit */
251
pruneBuffer(): Promise<void>;
252
}
253
```
254
255
**Usage Example:**
256
257
```typescript
258
import { ConversationSummaryMemory } from "langchain/memory";
259
import { OpenAI } from "@langchain/openai";
260
261
const llm = new OpenAI({ temperature: 0 });
262
263
const summaryMemory = new ConversationSummaryMemory({
264
llm: llm,
265
memoryKey: "chat_history",
266
returnMessages: true,
267
});
268
269
// After multiple exchanges, the memory will summarize older messages
270
await summaryMemory.saveContext(
271
{ input: "Tell me about machine learning" },
272
{ output: "Machine learning is a subset of AI..." }
273
);
274
275
await summaryMemory.saveContext(
276
{ input: "What are neural networks?" },
277
{ output: "Neural networks are computing systems..." }
278
);
279
280
const vars = await summaryMemory.loadMemoryVariables({});
281
console.log(vars.chat_history); // Will contain summary + recent messages
282
```
283
284
### Vector Store Memory
285
286
Memory that uses vector similarity search for retrieving relevant past conversations.
287
288
```typescript { .api }
289
/**
290
* Memory that uses vector store for retrieval-based memory
291
*/
292
class VectorStoreRetrieverMemory extends BaseMemory {
293
constructor(fields: VectorStoreRetrieverMemoryInput);
294
295
/** Vector store retriever for similarity search */
296
vectorStoreRetriever: BaseRetrieverInterface;
297
298
/** Key for memory content */
299
memoryKey: string;
300
301
/** Key for input in memory */
302
inputKey?: string;
303
304
/** Whether to return documents or just content */
305
returnDocs: boolean;
306
307
get memoryKeys(): string[];
308
309
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
310
311
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
312
313
clear(): Promise<void>;
314
}
315
```
316
317
**Usage Example:**
318
319
```typescript
320
import { VectorStoreRetrieverMemory } from "langchain/memory";
321
import { MemoryVectorStore } from "langchain/vectorstores/memory";
322
import { OpenAIEmbeddings } from "@langchain/openai";
323
324
const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());
325
const retriever = vectorStore.asRetriever({ k: 2 });
326
327
const vectorMemory = new VectorStoreRetrieverMemory({
328
vectorStoreRetriever: retriever,
329
memoryKey: "history",
330
inputKey: "human_input",
331
});
332
333
// Save context - will be embedded and stored
334
await vectorMemory.saveContext(
335
{ human_input: "My name is John and I like pizza" },
336
{ output: "Nice to meet you John!" }
337
);
338
339
// Later, when loading memory with similar content
340
const relevantMemory = await vectorMemory.loadMemoryVariables({
341
human_input: "What do you know about my food preferences?"
342
});
343
console.log(relevantMemory.history); // Will retrieve pizza-related conversation
344
```
345
346
### Entity Memory
347
348
Memory that extracts and tracks entities across conversation turns.
349
350
```typescript { .api }
351
/**
352
* Memory that extracts and tracks entities from conversations
353
*/
354
class EntityMemory extends BaseMemory {
355
constructor(fields: EntityMemoryInput);
356
357
/** Language model for entity extraction */
358
llm: BaseLanguageModelInterface;
359
360
/** Chat message history */
361
chatHistory: BaseListChatMessageHistory;
362
363
/** Key for entities in memory */
364
entitiesKey: string;
365
366
/** Key for history in memory */
367
historyKey: string;
368
369
/** Key for input */
370
inputKey: string;
371
372
/** Template for entity extraction */
373
entityExtractionPrompt: BasePromptTemplate;
374
375
/** Template for entity summarization */
376
entitySummarizationPrompt: BasePromptTemplate;
377
378
/** Current entity store */
379
entityStore: Record<string, string>;
380
381
get memoryKeys(): string[];
382
383
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
384
385
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
386
387
clear(): Promise<void>;
388
}
389
```
390
391
### Combined Memory
392
393
Memory that combines multiple memory implementations.
394
395
```typescript { .api }
396
/**
397
* Memory that combines multiple memory implementations
398
*/
399
class CombinedMemory extends BaseMemory {
400
constructor(fields: CombinedMemoryInput);
401
402
/** Array of memory implementations to combine */
403
memories: BaseMemory[];
404
405
get memoryKeys(): string[];
406
407
loadMemoryVariables(values: InputValues): Promise<MemoryVariables>;
408
409
saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;
410
411
clear(): Promise<void>;
412
}
413
```
414
415
**Usage Example:**
416
417
```typescript
418
import { CombinedMemory, BufferMemory, VectorStoreRetrieverMemory } from "langchain/memory";
419
420
const bufferMemory = new BufferMemory({
421
memoryKey: "chat_history",
422
inputKey: "input",
423
outputKey: "output",
424
});
425
426
const vectorMemory = new VectorStoreRetrieverMemory({
427
vectorStoreRetriever: retriever,
428
memoryKey: "relevant_history",
429
inputKey: "input",
430
});
431
432
const combinedMemory = new CombinedMemory({
433
memories: [bufferMemory, vectorMemory],
434
});
435
436
// Will save to both memory types
437
await combinedMemory.saveContext(
438
{ input: "I work as a software engineer" },
439
{ output: "That's interesting! What kind of software do you work on?" }
440
);
441
442
// Will load from both memory types
443
const variables = await combinedMemory.loadMemoryVariables({ input: "Tell me about my job" });
444
console.log(variables.chat_history); // From buffer memory
445
console.log(variables.relevant_history); // From vector memory
446
```
447
448
### Chat Message History
449
450
Storage backend for chat messages with various implementations.
451
452
```typescript { .api }
453
/**
454
* Base interface for chat message history storage
455
*/
456
abstract class BaseListChatMessageHistory {
457
/** Get all messages */
458
abstract getMessages(): Promise<BaseMessageInterface[]>;
459
460
/** Add a message to history */
461
abstract addMessage(message: BaseMessageInterface): Promise<void>;
462
463
/** Add user message */
464
addUserMessage(message: string): Promise<void>;
465
466
/** Add AI message */
467
addAIChatMessage(message: string): Promise<void>;
468
469
/** Clear message history */
470
abstract clear(): Promise<void>;
471
}
472
473
/**
474
* In-memory chat message history
475
*/
476
class ChatMessageHistory extends BaseListChatMessageHistory {
477
constructor(messages?: BaseMessageInterface[]);
478
479
/** Array of messages */
480
messages: BaseMessageInterface[];
481
482
getMessages(): Promise<BaseMessageInterface[]>;
483
484
addMessage(message: BaseMessageInterface): Promise<void>;
485
486
clear(): Promise<void>;
487
}
488
```
489
490
### Memory Utility Functions
491
492
Helper functions for working with memory variables and message formatting.
493
494
```typescript { .api }
495
/**
496
* Get input value from memory variables
497
*/
498
function getInputValue(
499
inputValues: InputValues,
500
memoryVariables: MemoryVariables
501
): string;
502
503
/**
504
* Get output value from memory variables
505
*/
506
function getOutputValue(
507
outputValues: OutputValues,
508
memoryVariables: MemoryVariables
509
): string;
510
511
/**
512
* Convert memory buffer to string representation
513
*/
514
function getBufferString(
515
messages: BaseMessageInterface[],
516
humanPrefix?: string,
517
aiPrefix?: string
518
): string;
519
```
520
521
### Memory Constants and Templates
522
523
Pre-defined templates and constants for memory operations.
524
525
```typescript { .api }
526
/**
527
* Template for entity memory conversations
528
*/
529
const ENTITY_MEMORY_CONVERSATION_TEMPLATE: string;
530
531
/**
532
* Default summarization prompt template
533
*/
534
const SUMMARIZER_TEMPLATE: BasePromptTemplate;
535
536
/**
537
* Default entity extraction prompt template
538
*/
539
const ENTITY_EXTRACTION_TEMPLATE: BasePromptTemplate;
540
```
541
542
## Types
543
544
### Base Memory Types
545
546
```typescript { .api }
547
type InputValues = Record<string, any>;
548
type OutputValues = Record<string, any>;
549
type MemoryVariables = Record<string, any>;
550
551
interface BaseChatMemoryInput {
552
chatHistory?: BaseListChatMessageHistory;
553
returnMessages?: boolean;
554
inputKey?: string;
555
outputKey?: string;
556
}
557
```
558
559
### Buffer Memory Types
560
561
```typescript { .api }
562
interface BufferMemoryInput extends BaseChatMemoryInput {
563
memoryKey?: string;
564
humanPrefix?: string;
565
aiPrefix?: string;
566
}
567
568
interface BufferWindowMemoryInput extends BufferMemoryInput {
569
k?: number;
570
}
571
572
interface ConversationTokenBufferMemoryInput extends BufferMemoryInput {
573
llm: BaseLanguageModelInterface;
574
maxTokenLimit?: number;
575
}
576
```
577
578
### Summary Memory Types
579
580
```typescript { .api }
581
interface BaseConversationSummaryMemoryInput extends BaseChatMemoryInput {
582
llm: BaseLanguageModelInterface;
583
prompt?: BasePromptTemplate;
584
summaryMessageKey?: string;
585
}
586
587
interface ConversationSummaryMemoryInput extends BaseConversationSummaryMemoryInput {
588
memoryKey?: string;
589
humanPrefix?: string;
590
aiPrefix?: string;
591
}
592
593
interface ConversationSummaryBufferMemoryInput extends ConversationSummaryMemoryInput {
594
maxTokenLimit?: number;
595
}
596
```
597
598
### Vector Store Memory Types
599
600
```typescript { .api }
601
interface VectorStoreRetrieverMemoryInput {
602
vectorStoreRetriever: BaseRetrieverInterface;
603
memoryKey?: string;
604
inputKey?: string;
605
returnDocs?: boolean;
606
}
607
```
608
609
### Entity Memory Types
610
611
```typescript { .api }
612
interface EntityMemoryInput {
613
llm: BaseLanguageModelInterface;
614
chatHistory?: BaseListChatMessageHistory;
615
entitiesKey?: string;
616
historyKey?: string;
617
inputKey?: string;
618
entityExtractionPrompt?: BasePromptTemplate;
619
entitySummarizationPrompt?: BasePromptTemplate;
620
entityStore?: Record<string, string>;
621
}
622
```
623
624
### Combined Memory Types
625
626
```typescript { .api }
627
interface CombinedMemoryInput {
628
memories: BaseMemory[];
629
}
630
```
631
632
### Message Types
633
634
```typescript { .api }
635
interface BaseMessageInterface {
636
content: string;
637
name?: string;
638
additional_kwargs?: Record<string, any>;
639
response_metadata?: Record<string, any>;
640
}
641
642
interface HumanMessageInterface extends BaseMessageInterface {
643
_getType(): "human";
644
}
645
646
interface AIMessageInterface extends BaseMessageInterface {
647
_getType(): "ai";
648
}
649
650
interface SystemMessageInterface extends BaseMessageInterface {
651
_getType(): "system";
652
}
653
654
interface FunctionMessageInterface extends BaseMessageInterface {
655
_getType(): "function";
656
name: string;
657
}
658
```
659
660
## Memory Usage Patterns
661
662
### Memory with Chains
663
664
```typescript
665
import { LLMChain } from "langchain/chains";
666
import { BufferMemory } from "langchain/memory";
667
import { OpenAI } from "@langchain/openai";
668
import { PromptTemplate } from "@langchain/core/prompts";
669
670
const memory = new BufferMemory({ memoryKey: "history" });
671
const llm = new OpenAI({ temperature: 0 });
672
673
const prompt = PromptTemplate.fromTemplate(`
674
The following is a conversation between a human and AI:
675
{history}
676
Human: {input}
677
AI:`);
678
679
const chain = new LLMChain({
680
llm,
681
prompt,
682
memory,
683
verbose: true
684
});
685
686
// Memory automatically managed
687
const result1 = await chain.call({ input: "Hi, I'm Alice" });
688
const result2 = await chain.call({ input: "What's my name?" });
689
```
690
691
### Custom Memory Implementation
692
693
```typescript
694
class CustomMemory extends BaseMemory {
695
private data: Record<string, any> = {};
696
697
get memoryKeys(): string[] {
698
return ["custom_memory"];
699
}
700
701
async loadMemoryVariables(values: InputValues): Promise<MemoryVariables> {
702
return { custom_memory: this.data };
703
}
704
705
async saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void> {
706
// Custom logic for saving context
707
this.data = { ...this.data, ...inputValues, ...outputValues };
708
}
709
710
async clear(): Promise<void> {
711
this.data = {};
712
}
713
}
714
```