r/ClaudeCode • u/Spare_Bat_3040 • 4d ago
Discussion Compaction keeps the code but loses the "why not". How are you handling this?
I've been building with Claude Code on a Go codebase for a while and the pattern I keep running into isn't that it forgets what the code does. It can re-read the code. It's that it forgets what we already ruled out.
Rejected approaches, tradeoffs, "we tried X and it broke Y". None of that lives in the code, so after a /compact or a fresh session it's just gone, and the agent happily walks back toward an option we already killed. It's not being dumb, it just doesn't have the history.
It's not only a Claude thing either. There's an open Codex issue where someone describes a side conversation missing a recent architecture decision right after compaction: https://github.com/openai/codex/issues/57475
What I've tried:
- CLAUDE.md. Good for rules, bad for decisions with context. It turns into a junk drawer fast.
- A DECISIONS.md the agent appends to. Works when I remember to ask. I mostly don't.
- Writing a short handoff note myself before compacting, with the rejected options and why. This works best, but it's manual and I skip it when I'm in flow, which is exactly when it matters.
I wrote up the longer version with the prompts I use before compacting here: https://www.useshifu.com/blog/preserve-decisions-through-compaction
Full disclosure, I'm building a tool around this problem (Shifu), but the post is mostly the manual stuff that works without any tool.
Curious what others do:
- Do you keep a decision log, and does the agent actually read it?
- Anyone found a compaction prompt that keeps the reasoning?
- Or do you just start fresh and accept re-explaining?
•
u/AutoModerator 4d ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/keto_brain 4d ago
I build adrs in docs/adrs/.
ADRs are Architectural Decision Records. I wrote a blog about it here:
https://www.outcomeops.ai/blogs/what-is-an-adr-and-why-theyre-critical-for-ai-powered-development
You can look at some of the ones I have written for my OSS app: https://github.com/bcarpio/outcome-ops-ai-assist/tree/main/docs/adr
They could be skills whatever you want to call them, but ADRs are how we capture decisions and claude code uses them so it doesn't forget after compaction or in a new session, etc..
4
u/Excellent-Issue-5956 4d ago
I use Claude Code's memory folder for this more than anything in the repo. When I correct it, it writes a short note on its own, so it doesn't depend on me remembering to ask. What makes the notes useful is that each one has a Why line under the rule. One of mine says never assign to a variable called status in zsh scripts, and the Why says status is read only in zsh and assigning to it silently killed the end of all 6 of my scheduled wrapper scripts. status=$? is the normal thing to write, so a new session would do it again without the reason sitting right there.
It also helps with notes going stale like you mentioned in the other reply. If the reason doesn't hold anymore you can tell from the note itself. I have a command that reads through all of them and lists the ones that look out of date, and nothing changes until I say yes.
1
u/Far_Reindeer8428 4d ago
the rejected approaches are the most expensive thing compaction throws away. we keep a why not file per module and load it before planning
3
u/torvalds75 4d ago
The thing that fixed it for me was moving the decision record off the session entirely and onto the unit of work. Every non-trivial change gets a ticket (a GitHub issue in my case), and rejected options get written there at the moment they're rejected, not at compaction time. "Tried X, broke Y because Z, going with W" takes ten seconds while it's fresh, and then compaction stops mattering.
Then two short rules in CLAUDE.md:
That covers your 1: the agent actually reads it because reading the ticket is the first step of the plan, not an optional file it may or may not open. It also scales better than a single DECISIONS.md, because decisions stay scoped to the thing they were about, so the junk drawer doesn't come back.
For 3: once that's in place, a fresh session is cheaper than /compact. I basically never compact anymore. I end the session and the next one rehydrates from the ticket in one read.
+1 to the comment about pointing at code. Linking the commit or file:line in the rejection note keeps it checkable after the code moves on.
2
u/algaefied_creek 4d ago
How are your code comments that you are having it leave? Set Claude code to more explanatory, and have AGENTS.md provide pedagogical code comments including decision making and rule-outs.
It bloats the hell out of your codebase but helps you and it understand and “remember”.
The other option is an iterative living DECISIONS.tsv to track decision making
2
u/LordLederhosen 4d ago
I never allow compaction to occur. Compaction is the devil.
1
u/tofutim8 4d ago
The thing you're trying to preserve isn't prose, it's a pointer into a version of the code. That's the distinction that makes it survive compaction without you maintaining a file by hand.
A "we tried X and it broke Y" note is only true relative to a specific state of the repo. Six months later, with Y refactored, the note is just a claim with no referent — and an agent reading it has to either trust it blindly or re-derive it, and re-deriving is exactly what it's trying to avoid.
If the note instead says "the caching layer in
cache/client.go:88was deliberately not used for the auth path because it holds connections across the tenant boundary — see PR #421" then two things survive compaction for free: the why, and a verifiable handle. The agent can re-read that exact location, check whether the code still looks the way the note assumes, and if it doesn't, that's a visible contradiction instead of a silent drift. Right now the DECISIONS.md note is the second copy that drifts, same failure mode as a stale summary.The other half: git already knows most of the "why not", but only if it's recorded in a place that's queryable by structure rather than by text. "Which functions in this module changed in the last 30 days, and which ones have no callers" is answerable from a tree-sitter parse plus git history; "search the repo for the word 'cache' and read the surrounding commits" is not, and that gap is why the grep-plus-manual-note workflow doesn't scale past one module.
We build an MCP server around that (srclight, MIT,
pip install srclight) — symbol search, caller/callee edges, git blame and hotspots per file, with the index reporting FRESH / STALE / MISSING so you know when the handle your note points at has moved. It's fully local. It won't write your decision log for you, but it can tell you which decisions still have a referent in the code and which ones are now floating free.The reason-not-file per module that Far_Reindeer8428 mentioned is the right instinct. I'd only add: make the entries point at code locations and PRs, not at prose, so the file is checkable rather than trusted.
1
u/AdEvening3382 4d ago
The step has to stop depending on memory. Your own list shows it: the file approaches fail when remembering is required, and the handoff note works but is manual, so it gets skipped exactly when it matters. A pre-compact hook that asks for the rejected options before the context goes turns it from a habit into part of the mechanism.
1
u/modern_medicine_isnt 4d ago
Your decisions.md is like my questions.md. it records the questions and answers. I put a line in my claude.md telling it to always record questions and answers. So I don't have to remember.
1
u/mylifeasacoder 4d ago
These issues speak of a fundamental problem with your workflow. I'm curious as to whether you can give an example of "we tried X and it broke Y"? What application are you working on that uses this experimental-coding sort of workflow?
1
u/gfunk5299 4d ago
I had fabke create a end of session hook that tells the agent to write its own handoff notes before compaction. I also wrapped a skill around it called good night. Do I can manually run the skill or just say good night, it wraps up all its own notes and memories however it best seems fit. Then I have another skill good morning that has it reread its notes before we start working on anything. So I work say good night, manually compact, say good morning and continue. It’s not perfect but most important decisions are retained. The drawback to my solution, which is intentional, each agent develops their own memories and they are trained to only share certain memories across all agents. For my use case this is ideal, but if you have multiple agents to all working on the same project, you will need to develop a centralized memory system for your agents to work from.
1
u/norviking 4d ago
https://github.com/vegardjorg/ravenseat
I made this to handle exactly that problem. And another metric shit ton of stupid things that happens daily in a larger project.
Tldr: All project state lives outside the agents. Use deterministic gates, tests and probes to enforce your rules, rather than hoping the claude.md lines, "memory" or skill instructions will work every time (Spoiler... they wont).
You will have to make the gates based on your stack and setup, they are individual, the checks tab will help you with this. But the alternative is often shipping tons of unsafe code, so you should do that anyway.
And you dont need the board if you dont want to, there is full parity with cli / claude desktop. And it has commands, but I never used them, I just talk to claude as usual.
1
u/CrustyBappen 4d ago
Feature specifications can hold the nuance and decisions about why things work in a certain way
1
u/rcs67 4d ago
These things aren't magic. You have to shift your mindset to "I'm now the product owner/project manager and architect". All that boring documentation and hand off stuff is now your responsibility. How you do this effectively with llms is a google search away.
1
u/onitsoga 4d ago
the log only works if it sits where the agent looks before starting, not in CLAUDE md. i put rejected options on the task itself so the next session reads them. i'm building KanbanMCP, a board agents work from. per task or per repo?
1
u/BarathKanna 3d ago
Rejected approaches are the most valuable context and the first thing compaction throws away, because they never show up in the code.
What works for me is keeping the why not next to the code it protects. A one line comment at the spot where someone would naturally try the obvious alternative, saying we tried X, it broke Y, see decision 14. The agent reads that exactly when it's about to make the mistake, which a DECISIONS.md at the repo root can't promise.
For bigger calls, short ADRs, one file per decision, with a rejected options section and the reason each died. Then add a rule that any change touching an ADR's area has to cite it or explicitly supersede it.
And when it suggests a dead option, ask why that's wrong before correcting it. If it can't find the reason, your notes are in the wrong place.
1
u/wtfihavetonamemyself 10h ago
Not seeing anybody say this- there’s a hook option for precompaction. Mine runs then to keep my hand off and decisions alive, and rue. Post compaction reads it back in.
1
u/cleverhoods 4d ago
well, it starts with viewing what is left after compaction and what is being loaded after compaction.
A typical compact message is usually including the recent messages, but only a handful of those.
Personally I find compacting a russian roulette, especially because of this unpredictability. Of course that can be mitigated, but in that case I much rather would use a fresh session