LangMemLangMem 0.0.30 · LangGraph 1.2 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
18 small wins to finish your pathNext lesson

What LangMem asks the model

A memory manager is a prompt and a set of tools. Printing the request shows the conversation wrapped in a session tag and a Memory tool the model must call.

This subclass prints every request before answering it:

Example
class ShowRequest(MemoryModel):
    def _generate(self, messages, stop=None, run_manager=None, tools=None, **kwargs):
        print("tools:", [tool["function"]["name"] for tool in tools])
        for message in messages:
            print(f"--- {message.type}")
            print(message.content)
        return super()._generate(messages, stop, run_manager, tools, **kwargs)
Example
manager = create_memory_manager(ShowRequest(), instructions="Keep facts that help support this customer.")
manager.invoke({"messages": [{"role": "user", "content": "Please email me."}]})
  • One tool, Memory, whose arguments are the fields of the Memory model: "call this tool once for each new memory".
  • A short system message.
  • A human message with your instructions, a paragraph telling the model to enrich and prune memories in one set of tool calls, then the conversation inside a <session_...> tag.

Without instructions, LangMem uses a long default prompt about semantic, procedural and episodic memory. Your own instructions replace it, and are the main way to say what is worth remembering for your app.

With existing memories

Example
manager = create_memory_manager(ShowRequest())
manager.invoke({"messages": [{"role": "user", "content": "Text me instead."}], "existing": [("m-1", Memory(content="Wants us to email them"))]})

Passing existing changes the request: the system message lists each memory in an <instance id=...> block, and a PatchDoc tool appears for changing them with JSON Patch operations. Lesson 7 is about that.

Try it yourself
  • Pass enable_deletes=True and look for a new tool.
  • Remove instructions and read the default prompt.
  • Print tools in full to see the Memory schema.

Slow is fine. Stopping is the only problem.