It Happened on a Friday Afternoon
Four hours into a complex auth refactor. State machines, edge cases, all the subtle context Claude and I had built up together.
Then: compaction.
Claude's response came back generic. It had forgotten the architecture decisions we'd made. The nuanced understanding of why we'd structured things a certain way — gone.
I spent 45 minutes trying to rebuild the context. It never fully came back.
If you use Claude Code regularly, this has probably happened to you.
Why Claude Code Compaction Hurts So Much
Claude Code has a context window limit. When you hit it, it "compacts" — summarizes your conversation to free up space. The summary is lossy. Nuanced architectural decisions, the "why" behind choices, subtle patterns — these get flattened into generic descriptions.
The worst part? It happens silently. No warning. You're deep in flow and suddenly you're talking to a Claude that doesn't know your codebase anymore.
Common scenarios:
- 3+ hour coding sessions on complex features
- Refactoring sessions with many interdependencies
- Debugging sessions where context about what you already tried matters
- Multi-file changes where Claude needs to hold a lot in mind
What I Tried (And Why It Failed)
Approach 1: Manual CLAUDE.md summaries
I started writing summaries of important context into CLAUDE.md. This helped with project-level context but didn't capture session-specific state — the conversation flow, what we'd just tried, where we were in the middle of a task.
Approach 2: Copying conversation snippets
Tedious. You have to decide what's important in real-time, which breaks flow. And it's not searchable.
Approach 3: /compact before it auto-compacts
Slightly better — at least you control timing. But you're still losing context; you're just choosing when to lose it.
The real problem: None of these preserve the conversation history — the actual back-and-forth that contains the reasoning.
The Fix: Snapshot Your Session State
Claude Code stores conversation history as JSONL files at:
~/.claude/projects/[project-hash]/[session-id].jsonl
Each file is a complete record of a coding session. The insight: if you snapshot these files, you can restore exact session state.
Here's a simple script:
#!/bin/bash
# save-session.sh
PROJECT_DIR="$HOME/.claude/projects"
BACKUP_DIR="$HOME/.claude/session-backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
cp -r "$PROJECT_DIR" "$BACKUP_DIR"
echo "Session saved to $BACKUP_DIR"
Run this before any risky operation. If compaction destroys your context:
# Restore a specific session
cp ~/.claude/session-backups/20260221_143022/projects/[hash]/[session].jsonl \
~/.claude/projects/[hash]/
Then claude --resume [session-id] brings back exact context.
Taking This Further: Mantra
I automated this workflow into a tool called Mantra. It goes beyond simple session backup — it's a full control center for AI-assisted coding with three capabilities:
▶ Replay — Visual timeline of every session. Click any message → see the exact code state at that moment. Drag the scrubber like a video. Works with Claude Code, Cursor, Gemini CLI, and Codex.
⚙️ Control — One MCP gateway for all your AI tools. Add a service once, and Claude Code + Cursor + Gemini CLI automatically share the same tools and context. No more duplicate configs across tools. Skills Hub manages prompt templates across projects.
🔒 Secure — Local Rust engine auto-detects API keys, passwords, and tokens in your sessions before you accidentally share them. One-click redaction with diff preview.
All three run locally. No cloud, no sign-up, free: https://mantra.gonewx.com?utm_source=devto&utm_medium=article&utm_campaign=devto-article-launch (macOS/Windows/Linux).
The Broader Problem
This isn't just a Claude Code issue. Cursor has the same problem with its AI chat history. Aider loses context between runs. We're all building complex things with AI assistants, but the tools don't yet treat session state as something worth preserving.
The script above is free and takes 2 minutes to set up. Your future self will thank you the next time compaction hits at the worst possible moment.
Have you lost important session context to compaction? What's your workaround? Share in the comments.
Top comments (0)