DEV Community

Zac
Zac

Posted on

State management for long Claude Code sessions: the pattern that survives resets

Every Claude Code session starts cold. If you're running a task that spans hours or multiple days, context resets will happen — whether from hitting the context limit, container restarts, or just starting a new session the next morning.

Here's the state management pattern that makes resets a 30-second recovery instead of a 10-minute re-explanation.

The state file

Create tasks/current-task.md in your project root. Claude reads it at session start, updates it as it works.

## Active Task
goal: "Add rate limiting to all public API endpoints"
started: 2026-03-17T10:00:00Z

steps:
  - [x] Identify all public routes in /src/routes/
  - [x] Install express-rate-limit
  - [x] Add rate limit middleware to auth routes
  - [ ] Add rate limit middleware to user routes
  - [ ] Add rate limit middleware to data routes
  - [ ] Write tests for rate limit behavior
  - [ ] Update API documentation

last_checkpoint: "Finished auth routes. Rate limiter configured at 10 req/min per IP. Next: /src/routes/users.ts"
Enter fullscreen mode Exit fullscreen mode

Add to CLAUDE.md:

At session start, read tasks/current-task.md if it exists. This tells you what we were working on and where we left off. Update last_checkpoint after every meaningful progress point.
Enter fullscreen mode Exit fullscreen mode

Why this works

After a reset, Claude reads the file and has:

  • What the overall goal is
  • Which steps are done and which aren't
  • Exactly where to start

No re-explaining the task. No repeating decisions already made. No discovering mid-session that you're duplicating work that was already done.

Checkpoint discipline

The file is only useful if it's current. Update last_checkpoint after each step, not just at the end. If you update it every 15-20 minutes of work, a reset at any point means at most 20 minutes of repeated work.

Add to CLAUDE.md:

Update last_checkpoint in tasks/current-task.md after completing each step. Do not wait until the task is done.
Enter fullscreen mode Exit fullscreen mode

Completed task history

When a task finishes, don't delete the file — archive it:

mv tasks/current-task.md tasks/history/$(date +%Y-%m-%d)-rate-limiting.md
Enter fullscreen mode Exit fullscreen mode

Over time, tasks/history/ becomes a log of decisions made and approaches taken. Useful for future sessions working on related areas.

The recovery prompt

When starting a session where a task is already in progress:

Read tasks/current-task.md. Summarize where we are and what the next step is. Ask me one question if anything is unclear before proceeding.
Enter fullscreen mode Exit fullscreen mode

30 seconds of context rebuild instead of 10 minutes.

Multi-day tasks

For work spanning multiple days, add a blockers section:

blockers:
  - Waiting on API access for the third-party service. Unblock by running: npm run setup:api-key
  - Need decision on whether rate limit should be per-user or per-IP for authenticated routes
Enter fullscreen mode Exit fullscreen mode

Blockers noted in the file get reviewed at session start. You don't rediscover them mid-task.

Full session management patterns: builtbyzac.com/agent-harness.html.

Top comments (0)