liveBook · Manning

Summary memory

As explained in Building Reliable AI Systems

A conversation-memory pattern that maintains a compressed summary of the conversation instead of its complete message history.

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

How Building Reliable AI Systems covers it

How deep it goes
Explains initial summarization, turn-by-turn updates, and gradual condensation of less-relevant details.
Examples
Demonstrates summarizing an exchange about mountain hiking and the benefits of the activity.
What you can do after
Build it from scratch with LangChain by summarizing new turns alongside an existing summary.
How it connects
Contrasts with buffer memory and uses large language models to update conversation history.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Reliable AI Systems. The section links point to the parts of the book it draws on.