Memory lets an agent carry context from one step or interaction to the next. Without it, a follow-up request may become an isolated prompt: a travel assistant that has just offered flights to New York cannot reliably interpret “make sure it’s a window seat” unless it retains the earlier request. Memory supports continuity; it does not, by itself, make the resulting choice correct. section 6.2.1 section 1.4.2
Choose what to retain based on how long the information must remain useful:
- Short-term memory keeps session context, such as conversation turns or troubleshooting details. A buffer or dictionary can hold it temporarily and discard it when the session ends. section 6.2.1
- Persistent memory retains information across sessions, such as user preferences and past interactions. It serves a different purpose from a session transcript; no storage or retrieval implementation is specified here. section 6.2.1 section 6.5
For conversation memory, start with a chronological list of messages. LangChain models accept HumanMessage and AIMessage objects directly, so retaining the full history needs no special memory class. This buffer preserves every recorded turn, but its size grows with conversation length; it is better suited to short or limited-scope interactions than an indefinitely running chat. section 6.2.2
from langchain_core.messages import HumanMessage, AIMessage
chat_history = []
chat_history.append(HumanMessage(content="What's the weather in Paris?"))
chat_history.append(AIMessage(content="It's sunny and 25°C."))
# Inspect the history
for msg in chat_history:
role = "Human" if isinstance(msg, HumanMessage) else "AI"
print(f"{role}: {msg.content}")
The message-list example shows the minimal buffer: append human and AI turns, then inspect the recorded history. section 6.2.2
For longer conversations, an LLM can compress new turns into a running summary. Each update combines the existing summary with new messages, keeping key context while condensing less-relevant details. This reduces the history carried forward and helps stay within context limits, but a summary is not a verbatim record: use a full buffer when exact earlier wording matters. section 6.2.2 section 6.5
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 summarization listing shows how to pass an existing summary and new HumanMessage and AIMessage turns to a ChatOpenAI call. section 6.2.2
Memory is one component of an agent, alongside tool integration and decision-making. In a harness, session and state management link multiturn tasks while memory stores and retrieves context; the harness also owns the agent loop, tool routing, approvals, and tracing. Treat remembered context as support for the workflow, not as a substitute for checking an action before it has consequences. section 6.2 section 7.9.1 section 1.4.2













