How to Fix 'memory not persisting' in CrewAI (TypeScript)

By Cyprian AaronsUpdated 2026-04-21
memory-not-persistingcrewaitypescript

When CrewAI says “memory not persisting”, it usually means the agent ran, but the state you expected to survive into the next step, task, or session was never actually written anywhere durable. In TypeScript projects, this shows up most often when the memory store is recreated on every run, or when the agent is configured correctly but the process exits before the write completes.

The Most Common Cause

The #1 cause is simple: you’re creating a new memory instance inside a function or task loop, so every run starts with an empty store. In CrewAI TypeScript, that often looks fine at first because the agent can read memory during the same execution, but nothing survives to the next invocation.

Here’s the broken pattern versus the fixed pattern:

BrokenFixed
Memory created per requestMemory created once and reused
No stable storage pathPersistent file/db-backed storage
Agent gets a fresh memory every runAgent receives the same memory instance
// BROKEN: memory is recreated every time this function runs
import { Agent, Task, Crew } from "crewai";

export async function runSupportAgent() {
  const agent = new Agent({
    name: "SupportAgent",
    role: "Customer support assistant",
    goal: "Remember customer context",
    memory: {
      type: "local",
      path: `./tmp/memory-${Date.now()}.json`, // new file every run
    },
  });

  const task = new Task({
    description: "Summarize the customer issue and remember it",
    agent,
  });

  const crew = new Crew({ agents: [agent], tasks: [task] });
  return crew.kickoff();
}
// FIXED: create one persistent memory store and reuse it
import { Agent, Task, Crew } from "crewai";
import { LocalMemory } from "crewai/memory";

const memory = new LocalMemory({
  path: "./data/crew-memory.json", // stable path
});

export async function runSupportAgent() {
  const agent = new Agent({
    name: "SupportAgent",
    role: "Customer support assistant",
    goal: "Remember customer context",
    memory,
  });

  const task = new Task({
    description: "Summarize the customer issue and remember it",
    agent,
  });

  const crew = new Crew({ agents: [agent], tasks: [task] });
  return crew.kickoff();
}

If your logs show something like:

  • CrewAIError: memory not persisting
  • TypeError: Cannot read properties of undefined (reading 'save')
  • Memory store initialized but no records found on next run

that’s usually a sign you’re not using a stable backing store.

Other Possible Causes

1) You’re using in-memory storage in a serverless/runtime reset environment

If you deploy to Lambda, Vercel Functions, Cloud Run with short-lived containers, or any environment that restarts frequently, plain process memory will disappear.

// BAD for serverless
const memory = {
  type: "ephemeral",
};

Use a real store instead:

const memory = new LocalMemory({ path: "/tmp/crew-memory.json" });
// Better yet in production: Redis/Postgres-backed persistence if supported by your stack

2) The file path is wrong or not writable

A very common TypeScript issue is pointing persistence to a directory that doesn’t exist or can’t be written by the process.

const memory = new LocalMemory({
  path: "./does-not-exist/memory.json",
});

Fix it by creating the directory and checking permissions:

import fs from "node:fs";

fs.mkdirSync("./data", { recursive: true });

const memory = new LocalMemory({
  path: "./data/memory.json",
});

3) You are re-instantiating Crew and Agent on every request without shared state

This is subtle. The code “works,” but each HTTP request builds a brand-new graph of objects with no shared persistence layer.

app.post("/chat", async (req, res) => {
  const agent = new Agent({ /* ... */ });
  const crew = new Crew({ agents: [agent], tasks: [] });
  await crew.kickoff();
});

Instead, keep persistence outside request scope:

const memory = new LocalMemory({ path: "./data/memory.json" });

app.post("/chat", async (req, res) => {
  const agent = new Agent({ /* ... */, memory });
  const crew = new Crew({ agents: [agent], tasks: [] });
  await crew.kickoff();
});

4) You expect task output to become long-term memory automatically

CrewAI task output and agent memory are not always the same thing. If you don’t explicitly write important results into memory, they may only exist in that response payload.

const task = new Task({
  description: "Extract customer preferences",
  agent,
  // output exists here, but may not persist unless saved by your flow
});

Make sure your flow saves key data after task completion:

const result = await crew.kickoff();
await memory.save("customer_preferences", result);

How to Debug It

  1. Check whether your memory object survives between calls

    • Log its config on startup and on each request.
    • If the file path changes or a new instance is created each time, that’s your bug.
  2. Verify actual disk writes

    • After kickoff, inspect whether the file exists and changes size.
    • If you use JSON storage, open it directly and confirm records are being appended.
  3. Run outside your web framework first

    • Execute the same code in a plain Node script.
    • If persistence works there but fails in Next.js/NestJS/Fastify/serverless, your runtime lifecycle is resetting state.
  4. Turn on verbose logging around save/load

    • Add logs before and after kickoff().
    • Watch for failures like:
      • CrewAIError
      • ENOENT
      • permission errors
      • serialization errors on complex objects

Prevention

  • Create one persistence layer per application environment and inject it into agents/tasks instead of constructing it inline.
  • Use stable storage paths or external stores like Redis/Postgres for anything that must survive restarts.
  • Add an integration test that runs two separate executions and verifies the second one can read what the first one wrote.

If you’re still seeing “memory not persisting” after fixing object lifetimes and storage paths, assume it’s not an AI problem yet. It’s almost always a lifecycle, filesystem, or configuration problem first.


Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides