Skip to main content
In this lesson you’ll learn how to give a chatbot or other LLM-based application a minimal form of short-term memory (conversation history) by using a MessagesPlaceholder inside a chat prompt template. This approach ensures previous messages are included in the prompt so the model can respond with appropriate context.

Minimal example (no memory)

This example creates a ChatPromptTemplate without any history placeholder and then invokes the model twice. Because the previous exchange is not included in the prompt, the second invocation has no context about the first.
Example output:
Now a follow-up question, still without passing any history:
Because the model was not provided the prior exchange as part of the prompt, it typically asks for clarification:
This demonstrates the core problem: LLMs do not retain conversation context across separate calls unless you explicitly include that context in the prompt.

Adding a MessagesPlaceholder to carry short-term memory

To give the model access to prior turns, add a MessagesPlaceholder to the chat prompt template and pass a history list on invocation. At runtime the placeholder will be replaced with the provided messages.
Create a simple history list that represents the prior human/AI exchange, then invoke the chain while passing that history:
With the history included, the model can respond in context:
The variable_name you assign to MessagesPlaceholder (for example, "history") is the key you must use when passing the list to invoke. The name can be anything, but the invocation dictionary key must match the placeholder’s variable_name.

Quick reference

Summary and next steps

  • Without explicit history included in the prompt, the model has no memory of prior invocations.
  • Adding a MessagesPlaceholder to your chat prompt template and providing a history list at invocation time gives your application short-term conversational memory.
  • Use this technique for multi-step workflows, follow-up questions, or any conversational app that needs access to preceding messages.

Further reading

Experiment: try changing the contents of history, the ability parameter, or the user input to observe how the model’s responses change when conversation history is included.

Watch Video