Claude Code Guide

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.

View the Project on GitHub ytrofr/claude-code-guide

Session End and Defrag Workflow

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.


Why bother

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.


The /session-end skill

The skill runs a short structured closing. Typical flow:

  1. Surface uncommitted stategit status --short, incomplete features, open TODO markers.
  2. Extract learnings — ask: what did this session teach me? (Pattern, gotcha, decision, fix.)
  3. Categorize — feedback note vs project note vs reference vs skill candidate.
  4. Write to memory — append to the appropriate Basic Memory note or auto-memory file.
  5. Checkpoint or commit — don’t leave work half-committed.
  6. Update handoff — brief “next steps” note so the next session picks up smoothly.

Verification checklist (what /session-end enforces)

When to run

When NOT to run

The skill is an optimizer, not a religion. The gate is “did this session produce knowledge worth preserving?”


The /memory-defrag skill

Over 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.

Process

  1. Audit current state — inventory all memory files. For each: line count, last modified, topic coverage, staleness.
  2. Identify problems — from the checklist below.
  3. Plan changes — write a short Defrag Plan with bullet items.
  4. Execute one at a time — split, merge, prune, restructure.
  5. Verify & log — ensure nothing was lost; record what was done in today’s daily note.

Common issues to fix

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

Run cadence


Staleness SLAs (recap from 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.


Manual session-end checklist (when the skill isn’t available)

If /session-end isn’t installed in your setup, run this by hand:

Takes 2–5 minutes. Saves hours of re-learning in future sessions.


Automation integrations

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?).


Anti-patterns


See also