Tutorials Logic, IN info@tutorialslogic.com

LangChain Memory and State: Conversations, Chat History and Workflows

Types of Memory

Memory means carrying useful information from earlier turns or earlier workflow steps. In chat apps, memory may include recent messages. In business workflows, state may include selected documents, user role, tool results, approvals, or partial outputs.

The key is to store the right information in the right place. Do not blindly append the entire conversation forever. Summarize, trim, retrieve, and separate durable user profile data from temporary chat context.

Short-term memory keeps the current conversation coherent. Long-term memory stores durable facts or preferences. Workflow state tracks intermediate steps in a multi-step process.

  • <strong>Message history:</strong> recent conversation turns.
  • <strong>Summary memory:</strong> compressed conversation context.
  • <strong>Profile memory:</strong> durable facts such as user preferences.
  • <strong>Workflow state:</strong> tool results, approvals, and intermediate decisions.

Memory Risks

Memory can leak sensitive information, increase token cost, and preserve incorrect assumptions. Treat memory as data with privacy, retention, and correction rules.

  • Do not store secrets or sensitive data unless the product explicitly requires it.
  • Let users inspect or reset durable memory.
  • Trim or summarize long histories before they exceed context limits.

Runnable with Message History

This pattern keeps chat history outside the model and injects it only when invoking the chain.

Runnable with Message History
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI

store = {}

def get_history(session_id: str):
    if session_id not in store:
        store[session_id] = InMemoryChatMessageHistory()
    return store[session_id]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful programming mentor."),
    MessagesPlaceholder(variable_name="history"),
    ("human", "{question}"),
])

chain = prompt | ChatOpenAI(model="gpt-4o-mini", temperature=0.2)

chat = RunnableWithMessageHistory(
    chain,
    get_history,
    input_messages_key="question",
    history_messages_key="history",
)

config = {"configurable": {"session_id": "user-123"}}
print(chat.invoke({"question": "What is RAG?"}, config=config).content)
print(chat.invoke({"question": "Give me one use case for it."}, config=config).content)
  • InMemoryChatMessageHistory is for demos, not durable production storage.
  • Session IDs should map to authenticated users or anonymous sessions safely.

Bound Conversation History Before a Model Call

Bound Conversation History Before a Model Call
messages = [
    {'role': 'user', 'content': 'first'},
    {'role': 'assistant', 'content': 'answer one'},
    {'role': 'user', 'content': 'second'},
    {'role': 'assistant', 'content': 'answer two'},
    {'role': 'user', 'content': 'latest question'},
]

window = messages[-3:]
print([message['content'] for message in window])
Output
['second', 'answer two', 'latest question']
Before you move on

LangChain Memory and State: Conversations, Chat History and Workflows Mastery Check

4 checks
  • Key short-term history by an authenticated session identity and verify one user cannot read another session.
  • Bound prompt history with a window or summary and test what survives when older turns are removed.
  • Store only approved durable facts with explicit update, deletion, and retention behavior.
  • Test session reset, concurrent requests, stale summaries, and unavailable persistence before release.

LangChain Memory State Questions Learners Ask

No. Memory usually stores conversation or user state. RAG retrieves external knowledge from documents or databases.

Yes. Old, irrelevant, or incorrect memory can distract the model and produce poor answers.

Keep only what the product needs, summarize older context, and apply a clear retention policy.

Next Step
Next Practice

Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.