liveBook · Manning

Summarization

As explained in Building Reliable AI Systems

The process of condensing information into a shorter representation that preserves important content.

Conversation summarization compresses chat history into a running summary that preserves key context while reducing memory use. As new turns arrive, an LLM updates the summary, condensing less-relevant details. This suits long-running agent conversations that risk exceeding a model’s context window. section 6.2.2

Choose summarization based on conversation length and reliability needs:

  • Use a plain buffer for short conversations or when perfect recall matters; it is simpler and cheaper. section 6.2.2
  • Use periodic summarization when conversations regularly exceed the context window, accepting extra LLM latency and cost. 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)

A Python example builds a concise summary from conversation messages and an existing summary using ChatOpenAI. section 6.2.2

How Building Reliable AI Systems covers it

How deep it goes
Shows dynamic summary updates and a basic LLM-backed function, but not production safeguards.
Examples
Uses a chatbot conversation and a Python example summarizing hiking-related messages.
What you can do after
Apply via LangChain to summarize conversation messages and update an existing summary.
How it connects
Connects conversation summarization to agent memory, buffer memory, LLMs, and context windows.

Also explained in

Compare how 12 books explain summarization →

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.