Mastra integration
Add long-term agent memory to Mastra agents with Zep tools
Mastra agents using Zep gain long-term memory backed by a temporal knowledge graph. The @getzep/zep-mastra package exposes Zep as a small set of idiomatic Mastra tools your agent calls to persist, search, and recall user context across turns and sessions.
Core benefits
- Idiomatic Mastra tools: Zep’s operations drop straight into an
Agent’stoolsrecord - Persist, search, and recall: Store messages and facts, search the graph, and fetch a prompt-ready context block
- User and standalone graphs: Bind tools to a user’s personal graph or a shared knowledge base
- Graceful degradation: A Zep outage is logged and surfaced as a non-fatal result — it never crashes the host agent
How it works
Zep is a temporal knowledge graph, not a row-oriented message store. A MastraStorage adapter would require CRUD operations a temporal knowledge graph can’t honor faithfully — there’s no row to update or delete in place. Exposing Zep’s two real operations — persist and retrieve — as createTool tools fits the graph model instead. Provision the Zep user and thread once with ensureZepUserAndThread, build the tool set with createZepToolset bound to that identity, and attach the tools to your agent.
The toolset provides three tools:
The Mastra integration is tool-based: the agent calls a tool to persist or recall memory. There is no automatic context injection — have the agent call the zepContext tool when you want the context block in the prompt.
Installation
Requires Node.js 20+, @mastra/core>=1.42.0 (peer), @getzep/zep-cloud>=3.23.0, and a Zep Cloud API key. Get your API key from app.getzep.com.
Set up your environment variables:
Usage
Provision the Zep user and thread, build the tool set bound to that identity, and attach the tools to a Mastra Agent:
Each tool is also exported as a standalone factory (createZepRememberTool, createZepSearchTool, createZepContextTool) for wiring a single tool with custom options.
Tools
Each tool has a typed input and output schema:
zepSearch returns facts as extracted strings tailored to the bound scope — edge facts, "name: summary" for entities, episode content, and so on — with found set to true when the result is non-empty.
Binding: user graph vs standalone graph
Tools are bound to a graph via a ZepBinding:
userIdtargets a user graph — the home for personalized agent memory. Use it for a conversational agent that remembers an end user. ThezepContexttool also needs athreadId(the thread scopes relevance; retrieval still spans the whole user graph).graphIdtargets a standalone graph — shared or domain knowledge such as a product knowledge base or runbooks. No user node, no user summary.
If both are set, userId wins. If neither is set, tools return a graceful “not configured” result instead of throwing.
Best practices
- Call
ensureZepUserAndThreadonce before the first turn, then reuse a singleZepClient - Pass real names so Zep can anchor the user’s identity node in the graph
- Don’t read-after-write within a turn — Zep builds the graph asynchronously, so a just-stored fact is not instantly retrievable
- Pass a custom
loggerto route Zep warnings into your logging stack
Next steps
- Explore customizing graph structure for advanced knowledge organization
- Learn about searching the graph and how to tune search
- See code examples for additional patterns