# Chat History and Memory — LangChain / LlamaIndex

Source: https://www.geekswithgeeks.com/en/langchain-llamaindex/a-memory

> Keep conversation turns and feed them back to the model.

## The model remembers only what you resend

Chat models are stateless. A **chat message history** stores the messages of a conversation (human, AI, tool). On each turn you put the stored messages into the prompt with a `MessagesPlaceholder`, then append the new question and answer. Modern LangChain recommends keeping this state explicitly, for example with a `RunnableWithMessageHistory` wrapper or, for richer needs, **LangGraph persistence/checkpointers**. Control growth: keep the last N messages, summarise old ones, or store key facts separately, and key histories by **session ID** so users never see each other's conversations.

## An in-memory chat history, run

I ran this offline in a Python virtual environment with langchain-core 1.6.6, langchain-text-splitters 1.1.2 and llama-index-core 0.14.25. No API key or network call is needed because a fake model or a toy embedding stands in for the real one. The history stores three messages in order, tagged `human`, `ai`, `human`. Passing them back with the next prompt is what lets the model answer "What is my name?".

```python
from langchain_core.chat_history import InMemoryChatMessageHistory

history = InMemoryChatMessageHistory()
history.add_user_message("My name is Asha.")
history.add_ai_message("Nice to meet you, Asha.")
history.add_user_message("What is my name?")
for m in history.messages:
    print(m.type, "|", m.content)
print("messages kept:", len(history.messages))

```

Output:

```
human | My name is Asha.
ai | Nice to meet you, Asha.
human | What is my name?
messages kept: 3
```

**Quiz:** Why key chat histories by session ID?

- [ ] To shorten prompts only
- [ ] To speed up tokens
- [ ] To train the model
- [x] So users never see each other's conversations

*Answer:* So users never see each other's conversations. Session isolation prevents data leaking between users.
