or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

agents.mdcaches.mdcallbacks.mddocuments.mdembeddings.mdindex.mdlanguage-models.mdmemory-storage.mdmessages.mdoutput-parsers.mdprompts.mdretrievers.mdrunnables.mdtools.mdvectorstores.md
tile.json

memory-storage.mddocs/

0

# Memory & Storage

1

2

Memory management and storage abstractions for maintaining conversation state and persistence. These components enable applications to remember context across interactions.

3

4

## Capabilities

5

6

### Base Chat Message History

7

8

Abstract base class for storing and retrieving chat messages.

9

10

```typescript { .api }

11

/**

12

* Abstract base class for chat message storage

13

*/

14

abstract class BaseChatMessageHistory {

15

/** Get all stored messages */

16

abstract getMessages(): Promise<BaseMessage[]>;

17

18

/** Add a message to the history */

19

abstract addMessage(message: BaseMessage): Promise<void>;

20

21

/** Add multiple messages to the history */

22

abstract addMessages(messages: BaseMessage[]): Promise<void>;

23

24

/** Add a user message */

25

abstract addUserMessage(message: string): Promise<void>;

26

27

/** Add an AI message */

28

abstract addAIChatMessage(message: string): Promise<void>;

29

30

/** Clear all messages */

31

abstract clear(): Promise<void>;

32

}

33

```

34

35

### Base List Chat Message History

36

37

Base implementation using array-based storage.

38

39

```typescript { .api }

40

/**

41

* Base implementation using list/array storage

42

*/

43

abstract class BaseListChatMessageHistory extends BaseChatMessageHistory {

44

/** Internal message storage */

45

protected messages: BaseMessage[];

46

47

constructor(messages?: BaseMessage[]);

48

49

/** Get all messages */

50

async getMessages(): Promise<BaseMessage[]>;

51

52

/** Add a message */

53

async addMessage(message: BaseMessage): Promise<void>;

54

55

/** Add multiple messages */

56

async addMessages(messages: BaseMessage[]): Promise<void>;

57

58

/** Add user message */

59

async addUserMessage(message: string): Promise<void>;

60

61

/** Add AI message */

62

async addAIChatMessage(message: string): Promise<void>;

63

64

/** Clear all messages */

65

async clear(): Promise<void>;

66

}

67

```

68

69

### In-Memory Chat Message History

70

71

Simple in-memory implementation for chat message storage.

72

73

```typescript { .api }

74

/**

75

* In-memory chat message history implementation

76

*/

77

class InMemoryChatMessageHistory extends BaseListChatMessageHistory {

78

constructor(messages?: BaseMessage[]);

79

}

80

```

81

82

**Usage Examples:**

83

84

```typescript

85

import { InMemoryChatMessageHistory, HumanMessage, AIMessage } from "@langchain/core/memory";

86

87

// Create in-memory history

88

const history = new InMemoryChatMessageHistory();

89

90

// Add messages

91

await history.addUserMessage("Hello, how are you?");

92

await history.addAIChatMessage("I'm doing well, thank you!");

93

94

// Add message objects directly

95

await history.addMessage(new HumanMessage("What can you help me with?"));

96

await history.addMessage(new AIMessage("I can help with various tasks like answering questions, writing, and more."));

97

98

// Get all messages

99

const messages = await history.getMessages();

100

console.log(messages);

101

102

// Clear history

103

await history.clear();

104

```

105

106

### Base Memory

107

108

Abstract base class for memory components.

109

110

```typescript { .api }

111

/**

112

* Abstract base class for memory components

113

*/

114

abstract class BaseMemory {

115

/** Input keys that this memory component handles */

116

abstract get memoryKeys(): string[];

117

118

/** Load memory variables for the given inputs */

119

abstract loadMemoryVariables(inputs: InputValues): Promise<MemoryVariables>;

120

121

/** Save context from chain execution */

122

abstract saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;

123

124

/** Clear memory contents */

125

abstract clear(): Promise<void>;

126

}

127

```

128

129

### Conversation Buffer Memory

130

131

Memory that maintains a buffer of conversation messages.

132

133

```typescript { .api }

134

/**

135

* Memory that keeps a buffer of conversation messages

136

*/

137

class ConversationBufferMemory extends BaseMemory {

138

/** Chat message history instance */

139

chatHistory: BaseChatMessageHistory;

140

/** Key for human input in memory variables */

141

humanPrefix: string;

142

/** Key for AI output in memory variables */

143

aiPrefix: string;

144

/** Key for memory in the returned variables */

145

memoryKey: string;

146

/** Input key to use from chain inputs */

147

inputKey?: string;

148

/** Output key to use from chain outputs */

149

outputKey?: string;

150

/** Whether to return messages as objects or strings */

151

returnMessages: boolean;

152

153

constructor(fields?: ConversationBufferMemoryInput);

154

155

get memoryKeys(): string[];

156

157

/** Load conversation buffer as memory variables */

158

async loadMemoryVariables(inputs: InputValues): Promise<MemoryVariables>;

159

160

/** Save conversation turn to memory */

161

async saveContext(inputValues: InputValues, outputValues: OutputValues): Promise<void>;

162

163

/** Clear conversation buffer */

164

async clear(): Promise<void>;

165

}

166

```

167

168

**Usage Examples:**

169

170

```typescript

171

import { ConversationBufferMemory, InMemoryChatMessageHistory } from "@langchain/core/memory";

172

173

// Create memory with in-memory storage

174

const memory = new ConversationBufferMemory({

175

chatHistory: new InMemoryChatMessageHistory(),

176

returnMessages: true,

177

memoryKey: "chat_history"

178

});

179

180

// Save conversation turn

181

await memory.saveContext(

182

{ input: "Hi there!" },

183

{ output: "Hello! How can I help you today?" }

184

);

185

186

await memory.saveContext(

187

{ input: "What's the weather like?" },

188

{ output: "I don't have access to current weather data, but I can help you find weather information." }

189

);

190

191

// Load memory variables

192

const memoryVars = await memory.loadMemoryVariables({});

193

console.log(memoryVars.chat_history); // Array of BaseMessage objects

194

195

// Use with conversational chain

196

const chain = ConversationalRetrievalChain.fromLLM(model, retriever, {

197

memory: memory,

198

returnSourceDocuments: true,

199

});

200

```

201

202

### Base Store

203

204

Abstract base class for key-value storage.

205

206

```typescript { .api }

207

/**

208

* Abstract base class for key-value storage

209

* @template K - Key type

210

* @template V - Value type

211

*/

212

abstract class BaseStore<K = string, V = unknown> {

213

/** Get values for given keys */

214

abstract mget(keys: K[]): Promise<(V | undefined)[]>;

215

216

/** Set key-value pairs */

217

abstract mset(keyValuePairs: [K, V][]): Promise<void>;

218

219

/** Delete keys */

220

abstract mdelete(keys: K[]): Promise<void>;

221

222

/** Yield all keys */

223

abstract *yieldKeys(prefix?: string): AsyncGenerator<K>;

224

}

225

```

226

227

### In-Memory Store

228

229

Simple in-memory key-value store implementation.

230

231

```typescript { .api }

232

/**

233

* In-memory key-value store

234

* @template K - Key type

235

* @template V - Value type

236

*/

237

class InMemoryStore<K = string, V = unknown> extends BaseStore<K, V> {

238

/** Internal storage map */

239

protected store: Map<K, V>;

240

241

constructor();

242

243

/** Get multiple values */

244

async mget(keys: K[]): Promise<(V | undefined)[]>;

245

246

/** Set multiple key-value pairs */

247

async mset(keyValuePairs: [K, V][]): Promise<void>;

248

249

/** Delete multiple keys */

250

async mdelete(keys: K[]): Promise<void>;

251

252

/** Yield all keys with optional prefix filter */

253

async *yieldKeys(prefix?: string): AsyncGenerator<K>;

254

}

255

```

256

257

**Usage Examples:**

258

259

```typescript

260

import { InMemoryStore } from "@langchain/core/stores";

261

262

// Create store

263

const store = new InMemoryStore<string, { name: string; value: number }>();

264

265

// Set values

266

await store.mset([

267

["user:1", { name: "Alice", value: 100 }],

268

["user:2", { name: "Bob", value: 200 }],

269

["config:theme", { name: "dark", value: 1 }]

270

]);

271

272

// Get values

273

const users = await store.mget(["user:1", "user:2"]);

274

console.log(users); // [{ name: "Alice", value: 100 }, { name: "Bob", value: 200 }]

275

276

// Iterate keys with prefix

277

for await (const key of store.yieldKeys("user:")) {

278

console.log(key); // "user:1", "user:2"

279

}

280

281

// Delete keys

282

await store.mdelete(["user:1"]);

283

```

284

285

## Conversation Memory Types

286

287

### Conversation Buffer Window Memory

288

289

Memory that keeps only the last N conversation turns.

290

291

```typescript { .api }

292

/**

293

* Memory that keeps a sliding window of recent conversation turns

294

*/

295

class ConversationBufferWindowMemory extends BaseMemory {

296

/** Number of conversation turns to keep */

297

k: number;

298

/** Chat message history instance */

299

chatHistory: BaseChatMessageHistory;

300

301

constructor(fields: ConversationBufferWindowMemoryInput);

302

303

/** Load recent conversation window */

304

async loadMemoryVariables(inputs: InputValues): Promise<MemoryVariables>;

305

}

306

```

307

308

### Conversation Summary Memory

309

310

Memory that maintains a running summary of the conversation.

311

312

```typescript { .api }

313

/**

314

* Memory that maintains a summary of the conversation

315

*/

316

class ConversationSummaryMemory extends BaseMemory {

317

/** LLM for generating summaries */

318

llm: BaseLanguageModel;

319

/** Prompt template for summarization */

320

prompt: PromptTemplate;

321

/** Current conversation summary */

322

buffer: string;

323

324

constructor(fields: ConversationSummaryMemoryInput);

325

326

/** Predict new summary given old summary and new messages */

327

async predictNewSummary(messages: BaseMessage[], existingSummary: string): Promise<string>;

328

}

329

```

330

331

## Types

332

333

```typescript { .api }

334

type InputValues = Record<string, unknown>;

335

type OutputValues = Record<string, unknown>;

336

type MemoryVariables = Record<string, unknown>;

337

338

interface ConversationBufferMemoryInput {

339

chatHistory?: BaseChatMessageHistory;

340

humanPrefix?: string;

341

aiPrefix?: string;

342

memoryKey?: string;

343

inputKey?: string;

344

outputKey?: string;

345

returnMessages?: boolean;

346

}

347

348

interface ConversationBufferWindowMemoryInput extends ConversationBufferMemoryInput {

349

k: number;

350

}

351

352

interface ConversationSummaryMemoryInput {

353

llm: BaseLanguageModel;

354

chatHistory?: BaseChatMessageHistory;

355

prompt?: PromptTemplate;

356

humanPrefix?: string;

357

aiPrefix?: string;

358

memoryKey?: string;

359

returnMessages?: boolean;

360

}

361

```