How to keep an AI agent's state across restarts and crashes
To keep an agent's state across restarts, store the state outside the agent: write progress, decisions and next steps to a file, database or shared server, and reload it at the start of each run. Save at checkpoints, keep steps repeatable, and resume from the last saved step.
Why agents lose state
An agent's working memory is its context window. When the session ends, the process crashes or the machine restarts, that memory is gone. Anything the agent only knew in context has to be learned again. The fix is not a bigger window. It is to write down what matters somewhere that outlives the process.
What to save
Save only what a fresh run needs to continue.
- The goal and any constraints.
- Decisions made and why.
- What is done, what is in progress and what is next.
- Pointers to files and results, not their full content.
- Anything that was tried and failed, so it is not repeated.
Four ways to store state
Pick by how long the job runs and how many agents are involved.
- A state file. A markdown or JSON file the agent updates and reads at start. Simple and readable. Good for one agent on one job.
- A database. Better for structured state, many writers and queries.
- A workflow engine. Tools such as Temporal and LangGraph with checkpointing record each step so a run can resume. More setup, stronger recovery.
- A shared server. State held by an MCP server that any agent can reach, so another agent can take over.
Resuming sessions in Claude Code
Claude Code saves every conversation locally. To pick up where you left off, use the commands below. The first resumes the most recent session in the current directory. The second lets you choose from a list.
claude --continue
claude --resumeDesign for restart
Make each step safe to run twice. If the agent crashes after sending an email but before saving that it did, the retry will send it again. Record the step before you do it and the result after, check the record before repeating, and keep steps small so little work is lost.
Agents that run overnight or on a schedule
Claude Code documents several options. Routines run in the cloud, desktop scheduled tasks run on your machine with access to local files, GitHub Actions run in your CI, and /loop polls inside a running session. Pick by where the work needs to run. Scheduled agents cannot ask questions, so say clearly what success looks like and where to put results.
Common mistakes
Three mistakes come up often. Saving too much, so the next run drowns in old notes. Saving too little, so it cannot tell what was finished. And never cleaning up, so stale entries mislead the agent. Keep the state short, date it, and prune what no longer applies.
Where a NodePress node fits
A node is a place for state that outlives any one agent. It holds a shared thread, a memory graph, and files and context you choose. A new session can join with one pasted line through the remote MCP server at https://nodepress.ai/mcp and read what the earlier agents saved. It runs on your own computer, so the machine needs to stay on for the state to stay available. Every step is written to one readable thread, so you can see where a run stopped.
A node does not restart a crashed agent for you. Use a scheduler or your agent's own resume features for that.
Do it with a NodePress node
- Name a node on nodepress.ai, on a computer that stays on.
- Paste the one line into each agent. It connects through the remote MCP server at https://nodepress.ai/mcp.
- Tell the agent to write its goal, decisions and next step to the node as it works.
- If the session ends, start a new one and paste the same line.
- Ask the new agent to read the node and continue from the last step.
For a short job that finishes in one session, you do not need persistent state. Write the result and move on.
Questions people ask
How do long-running AI agents keep state?
They write progress and decisions to storage outside the agent, such as a file, database or shared server, and reload it on start.
How do I resume an agent after a crash?
Reload the last saved state and continue from the last completed step. In Claude Code, claude --continue resumes the most recent session in the current directory.
How can agents run overnight?
Use a scheduler such as Claude Code routines, desktop scheduled tasks or GitHub Actions, and give clear success criteria since the run cannot ask questions.
What is the difference between stateful and stateless agents?
A stateless agent starts from nothing each run. A stateful one loads saved state, so it can continue earlier work.
Do I need a database for agent state?
Not always. A state file is enough for one agent on one job. Use a database when several agents write or you need to query it.
Related
- How to hand off work between AI sessions and models
- How to stop re-prompting your AI every session
- How to give AI agents shared memory
- Knowledge graph as memory for AI agents: how it works
- Claude Code context window full: what to do
- How to get AI agents talking to each other