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











