> ## Documentation Index
> Fetch the complete documentation index at: https://docs.langchain.com/llms.txt
> Use this file to discover all available pages before exploring further.

# InMemoryStore integration

> Integrate with the InMemoryStore store using LangChain JavaScript.

This will help you get started with [InMemoryStore](/oss/javascript/integrations/stores). For detailed documentation of all `InMemoryStore` features and configurations head to the [API reference](https://reference.langchain.com/javascript/langchain-core/stores/InMemoryStore).

The `InMemoryStore` allows for a generic type to be assigned to the values in the store. We'll assign type `BaseMessage` as the type of our values, keeping with the theme of a chat history store.

## Overview

### Integration details

| Class                                                                                             | Package                                                            | Local | [PY support](https://python.langchain.com/docs/integrations/stores/in_memory/) |                                            Downloads                                            |                                            Version                                           |
| :------------------------------------------------------------------------------------------------ | :----------------------------------------------------------------- | :---: | :----------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------: |
| [`InMemoryStore`](https://reference.langchain.com/javascript/langchain-core/stores/InMemoryStore) | [`@langchain/core`](https://www.npmjs.com/package/@langchain/core) |   ✅   |                                        ✅                                       | ![NPM - Downloads](https://img.shields.io/npm/dm/@langchain/core?style=flat-square\&label=%20&) | ![NPM - Version](https://img.shields.io/npm/v/@langchain/core?style=flat-square\&label=%20&) |

## Setup

### Installation

The LangChain InMemoryStore integration lives in the `@langchain/core` package:

<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/core
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/core
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/core
  ```
</CodeGroup>

## Instantiation

Now we can instantiate our byte store:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { InMemoryStore } from "@langchain/core/stores"
import { BaseMessage } from "@langchain/core/messages";

const kvStore = new InMemoryStore<BaseMessage>();
```

## Usage

You can set data under keys like this using the `mset` method:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { AIMessage, HumanMessage } from "@langchain/core/messages";

await kvStore.mset(
  [
    ["key1", new HumanMessage("value1")],
    ["key2", new AIMessage("value2")],
  ]
)

await kvStore.mget(
  [
    "key1",
    "key2",
  ]
)
```

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[
  HumanMessage {
    "content": "value1",
    "additional_kwargs": {},
    "response_metadata": {}
  },
  AIMessage {
    "content": "value2",
    "additional_kwargs": {},
    "response_metadata": {},
    "tool_calls": [],
    "invalid_tool_calls": []
  }
]
```

And you can delete data using the `mdelete` method:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
await kvStore.mdelete(
  [
    "key1",
    "key2",
  ]
)

await kvStore.mget(
  [
    "key1",
    "key2",
  ]
)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[ undefined, undefined ]
```

## Yielding values

If you want to get back all the keys you can call the `yieldKeys` method. Optionally, you can pass a key prefix to only get back keys which match that prefix.

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { InMemoryStore } from "@langchain/core/stores"
import { AIMessage, BaseMessage, HumanMessage } from "@langchain/core/messages";

const kvStoreForYield = new InMemoryStore<BaseMessage>();

// Add some data to the store
await kvStoreForYield.mset(
  [
    ["message:id:key1", new HumanMessage("value1")],
    ["message:id:key2", new AIMessage("value2")],
  ]
)

const yieldedKeys = [];
for await (const key of kvStoreForYield.yieldKeys("message:id:")) {
  yieldedKeys.push(key);
}

console.log(yieldedKeys);
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[ 'message:id:key1', 'message:id:key2' ]
```

***

## API reference

For detailed documentation of all `InMemoryStore` features and configurations, head to the [API reference](https://reference.langchain.com/javascript/langchain-core/stores/InMemoryStore)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/stores/in_memory.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
