Summary memory compresses a conversation into a running summary rather than retaining every message verbatim. An LLM processes each new turn, preserves key context, and gradually condenses less-relevant details, making the pattern useful for long-running conversations and memory constraints. section 6.2.2
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4.1-mini", temperature=0) #1
def summarize_history(messages, existing_summary=""): #2
history_text = "\n".join(
f"{'Human' if isinstance(m, HumanMessage) else 'AI'}: {m.content}"
for m in messages
)
prompt = [
SystemMessage(content="Summarize this conversation concisely."),
HumanMessage(content=f"Summary so far: {existing_summary}\n\nNew:\n{history_text}")
]
return llm.invoke(prompt).content
chat_history = [ #2
HumanMessage(content="I love hiking in the mountains."),
AIMessage(content="That's great! Mountain hiking offers beautiful views and great exercise.")
]
summary = summarize_history(chat_history) #3
print(summary)
The implementation combines an existing summary with new messages and asks a LangChain chat model to summarize them. section 6.2.2
