The complete guide to Claude Code. Opus 4.7, Sonnet 4.6, Haiku 4.5. 1M token context window. 27 hook events. 43 production-tested chapters across 6 topical Parts (Foundation, Workflow, Extension, Context Engineering, Advanced, Reference). Three install tiers. CC 2.1.121+ compatible.
How to close a Claude Code session cleanly and keep memory sharp over time. Two skills, two different cadences: /session-end every session that produced something worth remembering; /memory-defrag monthly, when memory files start to drift into noise.
Session-end discipline prevents knowledge loss between sessions. Without it, Claude Code becomes a rolling window of context that forgets its own learnings — pattern X gets rediscovered, session after session, minus the evidence of why X is the right answer.
The failure mode looks like this:
A one-minute session-end ritual collapses that cost to zero: write the insight down once, in a canonical place, and every future session sees it.
/session-end skillThe skill runs a short structured closing. Typical flow:
git status --short, incomplete features, open TODO markers./session-end enforces)wip, not fix)?The skill is an optimizer, not a religion. The gate is “did this session produce knowledge worth preserving?”
/memory-defrag skillOver time, memory files accumulate. Daily notes pile up. The same fact gets written in three places. Stale entries reference projects that have shipped or been abandoned. MEMORY.md bloats past 500 lines and becomes slow to scan.
/memory-defrag is the periodic cleanup — like filesystem defrag, but for knowledge.
Defrag Plan with bullet items.| Problem | Signal | Fix |
|---|---|---|
| Bloated file | >300 lines, covers many topics | Split into focused files |
| Duplicate info | Same fact in multiple places | Consolidate to one location |
| Stale entries | References to completed work, resolved issues | Remove or archive |
| Orphan files | Files never referenced or updated | Review, merge, or remove |
| Orphan skill refs | Notes mention skills that no longer exist | Update or delete reference |
| Inconsistencies | Contradictory info across files | Resolve to ground truth |
| Poor organization | Related info scattered | Restructure by topic |
rules/ai/knowledge-lifecycle.md)Every note type has a freshness threshold. After the SLA expires without an edit, the note is flagged for review during /memory-defrag or the monthly AI DNA health check.
| Note type | Max age | Action when stale |
|---|---|---|
decision |
90 days | Review: still valid? Update status or confirm |
investigation |
60 days | Archive unless actively referenced |
log |
30 days | Auto-archive (ephemeral by nature) |
note |
120 days | Flag for review |
research-cache |
90 days | Re-search if technology has changed |
ai-dna/* |
60 days | Validate against current code |
Stale ≠ wrong. Stale = “has not been checked against current reality recently enough to trust blindly”. The SLA forces a look.
If /session-end isn’t installed in your setup, run this by hand:
git status — commit or checkpoint anything uncommitted.~/.claude/projects/<project>/memory/MEMORY.md or a Basic Memory note.production-gotchas.md or equivalent.Takes 2–5 minutes. Saves hours of re-learning in future sessions.
The manual workflow is the fallback. In a mature setup, scripts and hooks do most of the maintenance:
| Automation | Frequency | What it does |
|---|---|---|
~/.claude/scripts/ai-knowledge-consolidation.sh |
Weekly (Sun) | Review growth log, flag stale notes, write consolidation log |
~/.claude/scripts/bm-daily-maintenance.sh |
Daily | Basic Memory reindex + growth counter |
/memory-defrag skill |
Monthly | Human-in-the-loop reorganization |
/session-end skill |
Per-session | Capture learnings, commit, checkpoint |
The scripts surface what needs attention. The skills do the thinking. The human makes the judgement calls (is this pattern universal? is this note still relevant?).
/session-end on every session regardless of whether learnings emerged. Noise in memory is worse than absence — it dilutes the signal./memory-defrag is meant to be monthly. More frequent = churn, not maintenance. You’ll rewrite notes that were fine.ai-dna/* grow unchecked. The 60-day SLA exists for a reason. AI infrastructure changes fast; a 6-month-old pattern may be obsolete./session-end feeds its growth log.