Skip to content
Dashboard

5 ways to add long-term memory to an AI agent

Content Engineer

Add long-term memory to an AI agent by saving information outside the conversation and retrieving it for future tasks. You can use structured profiles, conversation summaries, task journals, searchable records, or persistent files. Choose the approach based on what the agent needs to remember and how it will use that information.

Copy link to headingWhich memory pattern fits your agent?

Combine memory patterns when future tasks need known facts alongside relevant material from a larger collection.

What the agent needs

Memory pattern

Where you store it

How the agent uses it

User preferences or project settings

Structured profile

Database records

Look up a user or project

Decisions from an earlier conversation

Conversation summary

Summary records linked to messages

Load a summary and inspect its sources

Results from similar tasks

Task journal

One record per attempt

Filter or search past attempts

Relevant passages from a large collection

Searchable store

Postgres with pgvector or Upstash Search

Retrieve passages for the current question

Notes and files for later work

Persistent workspace

A retained directory, such as a Vercel Drive

Read or edit files through tools

Support agents can use profiles for customer preferences, summaries for earlier troubleshooting, and search for relevant documentation. Start with the information your agent needs most often and add patterns as needed.

AI SDK supports memory integrations and custom tools that connect agents to storage. Your application decides what to retain and how each task retrieves it.

Profiles, summaries, and task journals can share a database. Scope records by user or project and coordinate concurrent writes to prevent lost updates.

Copy link to heading1. Store user preferences and project settings

Use a structured profile for confirmed preferences and project settings when the agent can identify the user or project. Named fields such as preferred language or runtime support direct lookups of specific facts.

Key an agent_profiles table by user or project ID, using columns for predictable fields and JSON for flexible preferences. Record each preference's source and last update. Vercel Marketplace offers database integrations.

Load the relevant profile fields before a model call, or let the agent retrieve them through an AI SDK lookup tool. Save confirmed corrections to the current record. Keep earlier values in an audit record when you need a history of changes.

Copy link to headingProfile tradeoffs

Advantage

Disadvantage

Implementation choice

Retrieve a specific record by ID

Facts outside the profile require another source

Define fields around recurring tasks

Individual fields can be inspected and corrected

An inferred preference can become an incorrect default

Distinguish confirmed facts from model inferences

Multiple sessions can use the same current record

Concurrent updates can overwrite one another

Check the record version before accepting an update

Use summaries or search for information that doesn't fit predictable profile fields.

Copy link to heading2. Carry conversation decisions forward with summaries

Planning assistants can save the agreed launch plan and unresolved requirements as a summary for the next discussion. Summaries carry those decisions and open questions forward in fewer words than a full transcript.

Store summaries in a table such as conversation_summaries. Include the summary text, user or project ID, source conversation ID, and range of messages covered. Keep the original messages separately so the agent can check the exact wording.

Generate summaries at checkpoints, such as a confirmed decision or the end of a conversation. Record the source version and select summaries relevant to later tasks. Store exact dates, identifiers, and requirements separately when an omission could change the agent's next action.

Copy link to headingSummary tradeoffs

Advantage

Disadvantage

Implementation choice

A selected summary uses fewer prompt tokens than its full transcript

Compression can omit exceptions or change meaning

Keep source references and exact constraints

Decisions can carry into a new conversation

A mistaken summary can mislead later tasks

Verify consequential claims against original messages

You can prepare summaries before they're needed

Generation adds model calls, and updates can lag

Define when to refresh each summary and record the source version it covers

Copy link to heading3. Record task attempts and outcomes

For a data import agent, record which parser handled a file, whether validation passed, and any errors returned. Keeping these methods, outcomes, and conditions in a task journal gives the agent evidence to consult on similar tasks.

Store each attempt in a task_attempts table with task and project IDs, input characteristics, method, outcome, timestamp, and evidence references. Keep large logs and output files in persistent storage such as Vercel Blob, and save their storage references in the record.

Write an entry after each attempt finishes. Before retrieving past attempts, filter by task type, access permissions, and input versions. Retain failures for diagnosis, but load only those relevant to the current task.

Copy link to headingTask-journal tradeoffs

Advantage

Disadvantage

Implementation choice

Prior attempts provide evidence for choosing a method

A similar task may have different causes or constraints

Record input versions and execution conditions

Failure records help identify repeated unsuccessful approaches

Agent-written explanations may misdiagnose the failure

Store observations separately from interpretations

Outcomes can support reusable procedures

Loading the full journal increases retrieval and prompt size

Select relevant cases and validate derived procedures

In the Evo-Memory experiments, some baseline methods performed worse when their memories included unfiltered failed attempts. Recording more outcomes does not necessarily improve an agent's performance; it also needs a way to select and refine useful experience.

“The import timed out” records what happened. “The batch is too large” is a possible explanation. Test that explanation with a smaller batch before saving “reduce the batch size” as a reusable procedure.

Use retrieval-augmented generation (RAG) to find relevant passages in saved notes, decisions, and documents, then supply them to the model. This approach fits collections that are too large or varied to load for every question.

Keep original documents in a database or file store. Index passages with source IDs, versions, and access metadata. For each question, enforce the user's access permissions before returning passages and source references to the model.

Generate embeddings with AI SDK and store them alongside source records using pgvector. Your application still needs to handle document updates, retrieval queries, and access rules.

Copy link to headingRetrieval tradeoffs

Advantage

Disadvantage

Implementation choice

Selected passages limit how much material enters the prompt

Retrieval can omit the passage needed to answer

Evaluate retrieval separately from answer generation

Source references let readers inspect the evidence

A passage can lose meaning when split from its document

Preserve headings and context around chunk boundaries

Search can select passages from a growing collection

Changed or deleted sources can leave stale index entries

Update or delete derived records when sources change

Anthropic's Contextual Retrieval experiments show how adding context to passages and combining semantic retrieval with keyword search can improve retrieval. Test these techniques on your own documents and questions before choosing a setup. Use a direct lookup for a known record ID.

Copy link to heading5. Keep memory in persistent files

Keep profiles in JSON files and summaries or task journals in Markdown when the agent works through file tools. For example, a planning agent can update decisions.md as requirements change, then read it before proposing the next milestone. The same workspace can also retain scripts, datasets, and outputs.

Use Vercel Sandbox to run file tools and the generated code, and expose scoped tools through the AI SDK. Record the workspace ID, file paths, and input versions with each task.

Vercel Sandbox can save and restore its own filesystem. Vercel Drives can retain a directory independently of the sandbox, so later runs can reuse files in a different environment.

Copy link to headingFile-memory tradeoffs

Advantage

Disadvantage

Implementation choice

Tools can reuse original files and intermediate outputs

Files occupy storage and can become outdated

Record source versions and retention rules

Agents can open exact artifacts instead of reconstructing them from prose

They still need to discover which files matter

Maintain descriptive paths and an artifact index

Multiple sessions can reuse a retained collection

Shared writes need coordination; snapshots can be stale

Allow one read-write mount per drive; mount new read-only snapshots to see updates

Anthropic's long-running agent harness uses progress files and Git history to help later coding sessions continue. At the start of a session, the coding agent reads those records and tests the application before making further changes.

Retaining files does not keep processes running. To reproduce earlier results, later runs also need the required tools, dependencies, and input versions.

Copy link to headingFrequently asked questions

Copy link to headingDo you need a vector database for long-term AI agent memory?

No. Structured profiles can use direct database lookups, and agents can read retained files through tools. Vector search is useful when a task needs to find relevant information within a large collection based on similarity.

Copy link to headingHow do you add long-term memory to an AI SDK agent?

Choose a memory integration or define AI SDK tools that read and write your storage backend. Scope records to the correct user or project, retrieve relevant information for each task, and define correction and deletion rules. The integration or backend retains the information between conversations.

Copy link to headingShould you store full conversations or summaries?

Keep full conversations when you need exact wording or an audit trail. Use summaries when future tasks need a shorter account of decisions, and link each summary to its source messages. Update the summary when a source correction changes its meaning.

Copy link to headingWhen should agent memory updates run in the background?

Run extraction or indexing in the background when the current answer doesn't depend on it. Later tasks may start before the new information is available, so define how your application handles pending updates. Save a confirmed fact before continuing when the next action depends on it.

Copy link to headingHow do you stop an agent from recalling stale memories?

Record each memory's source version, owner, and update time. When a fact changes, replace or invalidate the record and refresh summaries and indexes that use it. Then ask for the fact in a new conversation and verify that retrieval returns the corrected value.

Copy link to headingCan an agent use RAG and persistent files together?

Yes. Use a retrieval store to locate relevant passages while a workspace retains the underlying documents, scripts, and editable outputs. Keep source IDs and file versions aligned so retrieved information points to the material used by the task.

Copy link to headingCan multiple sandboxes read the same Vercel Drive?

Yes. Once a drive has been mounted read-write and written to, multiple sandboxes can mount read-only snapshots concurrently. Only one sandbox can mount the drive read-write at a time.

Each snapshot shows files at mount time; mount a new one for updates.

More Build with AI articles

Ready to deploy?