ctx: Don't Lose the Thread Between Claude Code Compactions
I’d been working in long Claude Code sessions for weeks when I started noticing a pattern that was quietly costing me time.
Halfway through a complex task — refactoring a module, or building a feature that spanned several files — Claude would compact the conversation. Necessary, the context window was full. But after the compaction, something would shift.
Not all at once. Gradually. Claude would start asking questions we’d already answered. It would revisit approaches we’d dismissed. It would lose the thread of technical decisions we’d made together ten minutes ago.
The work wasn’t lost — it was in the code. But the momentum was.
🤔 The actual problem
When Claude Code compacts, it summarizes the conversation. It’s a smart summary, but it’s still a summary. And summaries prioritize by volume, not by immediate relevance.
What gets diluted first isn’t the long explanations or the code. It’s the small but critical things:
- The specific goal of this session.
- The decision we made twenty minutes ago to not use that abstraction.
- The file that was halfway done.
- The concrete next step that was still pending.
Those things don’t take up much space in the conversation, so the summary flattens them. And after two or three compactions, working with Claude started to feel like restarting from scratch each time.
💡 The obvious solution that didn’t exist
I searched for tools. I didn’t find anything that addressed exactly this problem.
There were plenty of cross-session memory solutions — saving notes, persisting context across days, building knowledge bases. Useful for other things, but not for this.
What I needed was simpler: for Claude to know exactly where we were before the compaction, and know it again immediately after.
A checkpoint. Not a history. Not accumulative memory. Just the minimum state to resume work without losing the thread.
🔧 Building ctx
So I built it. It’s called ctx - short for context.
The idea is straightforward: ctx hooks into Claude Code’s internal events to capture a structured snapshot of the context right before each compaction, and restore it automatically when context resumes.
flowchart TD
A([Active session]) --> B([Claude compacts])
B --> C["**PreCompact hook**
reads transcript + git state
calls claude -p
writes snapshot scoped to branch"]
C --> D["**PostCompact hook**
reads snapshot
prints to stdout → Claude"]
D --> E([Session resumes with context])
F([New session — same branch]) --> G["**SessionStart hook**
finds snapshot for project+branch
injects as context"]
G --> H([Claude picks up where you left off])
The snapshot is a markdown document with four fields:
# Session Context
_Captured: 2026-05-16T10:32Z_
## Goal
Migrate the auth system to JWT with RS256
## Decisions
- Middleware applied at router level, not per-handler
- Use 7-day refresh tokens with automatic rotation
## In Progress
auth/middleware.go | 45 +++++++++
auth/jwt.go | 32 ++++++
2 files changed, 77 insertions(+)
## Next
Add refresh endpoint and write integration tests
That’s all Claude needs to pick up exactly where it left off.
⚙️ How it works in practice
Installation is one line:
curl -fsSL https://raw.githubusercontent.com/AgusRdz/ctx/main/install.sh | sh
ctx init
After ctx init, the hooks are registered in Claude Code. Nothing else to configure. The next time Claude compacts, ctx captures the context. The next time Claude starts up on that project and branch, it restores it.
You don’t have to remember to do anything.
🤖 A detail I find elegant
To generate the snapshot, ctx doesn’t use simple heuristics. It calls claude -p with the compressed transcript, the git diff, and the current project state, and asks it to extract the four fields semantically.
This means the snapshot isn’t a mechanical dump - it’s what actually matters, filtered by Claude itself.
If claude -p is unavailable for any reason, ctx has a deterministic fallback that builds the snapshot from git state. It never fails silently.
📋 What changed in my workflow
Before ctx, after a compaction I’d spend several minutes re-orienting Claude: what we were doing, what we’d decided, what still needed to happen. Sometimes I’d manually paste text from earlier in the conversation.
Now I do nothing. The session continues.
What surprised me most wasn’t the convenience - it was realizing how much context I’d been losing without noticing. You don’t see it while it’s happening. You just see Claude starting to ask strange questions or suggesting things you already ruled out.
🙃 Why I made it public
I could have left ctx as a script in my dotfiles. But the problem it solves is specific enough and common enough for anyone who regularly works in Claude Code that it seemed worth polishing.
It’s not a tool for everyone. If you use Claude Code for short sessions or one-off tasks, you probably don’t need this. But if you work on complex projects with sessions that last hours and multiple compactions per day - ctx probably saves you 10-20 minutes of re-orientation per session.
✳️ Where to go from here
Free, open source, MIT licensed:
curl -fsSL https://raw.githubusercontent.com/AgusRdz/ctx/main/install.sh | sh
ctx init
Compaction isn’t a Claude Code bug - it’s an architectural necessity. But the loss of operational context that comes with it is a solvable problem. ctx solves it in the smallest way possible.