--- title: "Memory for LlamaIndex agents" description: "Two packages add past.dev memory to LlamaIndex agents: PastDevMemoryBlock for the Memory class, and PastDevToolSpec with recall and ingest tools." canonical: https://past.dev/integrations/llamaindex last-updated: 2026-10-28 --- # Add memory to LlamaIndex agents Source: https://past.dev/integrations/llamaindex LlamaIndex's Memory class keeps recent chat messages for one session and flushes older ones into long-term memory blocks. Two packages connect it to past.dev. `llama-index-memory-pastdotdev` adds `PastDevMemoryBlock`, which runs a recall before the agent answers and ingests the messages that Memory flushes. `llama-index-tools-pastdotdev` adds `PastDevToolSpec`, with `recall` and `ingest` tools the agent calls when it decides to. You can use both. In both, your code sets the identity. ## What LlamaIndex memory covers The [memory guide](https://developers.llamaindex.ai/python/framework/module_guides/deploying/agents/memory/) documents a `Memory` class built around a FIFO queue of chat messages. `token_limit` caps what the object holds, and when the queue exceeds its budget, batches of `token_flush_size` tokens move into long-term memory blocks. A `session_id` names the conversation. The default backing store is an in-memory SQLite database; a database URI makes it persistent. Agents accept the object per run: `agent.run(question, memory=memory)`. Long-term memory is a set of blocks. `StaticMemoryBlock` holds fixed text such as a persona. `FactExtractionMemoryBlock` has an LLM extract fact lines from flushed messages. `VectorMemoryBlock` writes flushed batches to a vector store and retrieves them by similarity. Each block carries a `priority`: zero is always kept, and higher numbers are truncated first when the context budget fills. - **Flushing follows the token budget.** Messages reach long-term blocks when token limits overflow. - **Fact lines are undated.** An extracted fact records no event time and no link to the fact it replaced, so a January value and a March value are two lines of text. - **Scope is the session you name.** Sharing memory across agents, products and users is left to the application. ## Install the packages ```bash pip install llama-index-memory-pastdotdev llama-index-tools-pastdotdev export PAST_API_KEY=past_sk_... ``` Install one or both. Create the project key in the [past.dev console](https://app.past.dev) under **Build › API keys**, or pass it as `api_key`. The packages are on PyPI as [llama-index-memory-pastdotdev](https://pypi.org/project/llama-index-memory-pastdotdev/) and [llama-index-tools-pastdotdev](https://pypi.org/project/llama-index-tools-pastdotdev/), and their source and READMEs are in the [pastdotdev/llama-index-pastdotdev repository](https://github.com/pastdotdev/llama-index-pastdotdev). ## Add the past.dev memory block ```python from llama_index.core.agent.workflow import FunctionAgent from llama_index.core.memory import Memory from llama_index.llms.openai import OpenAI from llama_index.memory.pastdotdev import PastDevMemoryBlock memory = Memory.from_defaults( session_id="session-42", token_limit=30000, memory_blocks=[ PastDevMemoryBlock( identity=current_user.id, # your id for the end user, from your own user record ) ], ) agent = FunctionAgent(tools=[], llm=OpenAI(model="gpt-4.1-mini")) response = await agent.run("How should you contact me?", memory=memory) ``` When the agent reads its memory, the block recalls with the latest user message and puts the memories in the system message, each with its date, its kind and the quoted evidence. When Memory flushes old messages, the block ingests the user and assistant text as one data point, with a `timestamp` from the first message when your application sets one. Set `audience`, `level` or `max_tokens` on the block, and `raise_on_error=False` to carry on without memory when the API refuses a call. ## Give the agent recall and ingest tools ```python from llama_index.core.agent.workflow import FunctionAgent from llama_index.llms.openai import OpenAI from llama_index.tools.pastdotdev import PastDevToolSpec past_dev = PastDevToolSpec( identity=current_user.id, # your id for the end user, from your own user record ) agent = FunctionAgent( tools=past_dev.to_tool_list(), llm=OpenAI(model="gpt-4.1-mini"), system_prompt="Recall what you know about the user before you answer.", ) response = await agent.run("What did I decide about the launch date?") ``` | Tool | Arguments the model gives | API call | | --- | --- | --- | | `recall` | `query` (required), `occurredFrom`, `occurredTo`, `sort` | `POST /api/v1/recall` | | `ingest` | `content` (required), `timestamp` | `POST /api/v1/ingest` | > **Identity** > > The application sets the identity. The model never chooses it. Recall returns what the identity in the request may read, so take the identity from the signed-in user in code, and keep it out of every value that the model fills. `identity` is a constructor argument of both classes, never a tool argument, so the model can't see or change it. The same goes for `audience` and the recall budget. Make one spec or one block for each user. ## Or connect the MCP server The `llama-index-tools-mcp` package provides `BasicMCPClient` and `McpToolSpec`. A URL that does not end in `/sse` uses Streamable HTTP ([MCP tools](https://developers.llamaindex.ai/python/framework/module_guides/mcp/llamaindex_mcp/)). The project's end-user MCP server then gives the agent `recall`, `answer` and `who_am_i`, plus `remember` while the project's write toggle is on, with no tool code to write. 1. Turn on the project's end-user MCP server on **Build › MCP server** in the console. 2. Mint an access link there, or with the account server's `create_mcp_access_link` tool. The link has the form `https://api.past.dev/mcp//link/`. It is one URL that is one identity, and the server answers it with no sign-in step. 3. Keep the link in a secret store, such as the `PAST_MCP_URL` environment variable that the code below reads. The secret in its path is the credential. ```python import os from llama_index.core.agent.workflow import FunctionAgent from llama_index.tools.mcp import BasicMCPClient, McpToolSpec client = BasicMCPClient(os.environ["PAST_MCP_URL"]) tools = await McpToolSpec(client=client).to_tool_list_async() agent = FunctionAgent( llm=llm, # any LlamaIndex LLM tools=tools, system_prompt="Call recall before you answer questions about people, decisions or history.", ) ``` Every call through the link runs as its identity, with that identity's audiences. Mint one link per install, so that a revoked link stops one agent only. When the agent serves several people, mint one link per end user, and connect each request with the link of the user who makes it. Never share one link between users. `remember` writes to the identity's own private audience, and only that identity recalls it. Send data that the whole project must recall through the ingest call. The [end-user server documentation](/docs/mcp/serving-your-own-users) describes the tools, the links and revocation. ## A worked example: the budget that changed A client emails on January 8 that the renewal budget is 30k. A call on March 12 raises it to 45k. Inside `Memory`, both statements were flushed as text. `VectorMemoryBlock` retrieval returns both passages, because both are similar to the question, and neither passage carries the date it was true. Ingested with their original timestamps and stable source ids, the two statements stay tied to their sources, and recall marks the January figure as replaced by the March one. The application can answer both the current question and the historical one. Recall returns ranked documents with dates and source excerpts. The application decides whether those documents support an answer, contain a disagreement, or are insufficient; HTTP failures are handled separately. The [quickstart](/docs/memory-api/quickstart) shows the request flow, and the [benchmarks](/benchmarks) document how recall is measured. [Session memory](/glossary/session-memory) and [cross-session memory](/glossary/cross-session-memory) define the two layers this page separates. ## Frequently asked questions ### Does LlamaIndex have built in long term memory? Yes. The Memory class flushes older chat messages into long-term memory blocks: static text, LLM-extracted fact lines, or vector retrieval over past messages. The blocks live with the session and the database you configure. ### Should I use the memory block or the tools? Use the memory block when memory must work without the model asking for it. Use the tools when the model decides when to recall and what to keep. You can use both together. ### Can a LlamaIndex agent remember a user across sessions? Yes, with either package. Memory lives on past.dev under the identity you set, so any session or agent with a key for the same project recalls the same user's history. ## Related - [Memory for CrewAI](https://past.dev/integrations/crewai) - [Memory for LangGraph](https://past.dev/integrations/langgraph) - [Memory for the Vercel AI SDK](https://past.dev/integrations/vercel-ai-sdk) - [Memory for Haystack](https://past.dev/integrations/haystack) - [What is agent memory?](https://past.dev/what-is-agent-memory) - [Quickstart](https://past.dev/docs/memory-api/quickstart)