Summary

The advanced companion to the Shorthand Guide, covering techniques for long-running productive sessions. Focuses on four domains: context and memory management across sessions (session files, strategic compaction, dynamic system prompt injection), memory persistence hooks (PreCompact/SessionStart/Stop lifecycle), continuous learning via Stop hooks that extract session patterns into reusable skills, and token optimization through subagent model selection, mgrep, and modular codebases.

Shorthand Guide 的進階篇,針對長時間高效 session 的技術。四大領域:跨 session 的 context 和記憶管理(session 檔案、戰略性 compact、動態系統提示注入)、記憶持久化 hooks(PreCompact/SessionStart/Stop 生命週期)、透過 Stop hook 提取 session 模式成可重用 skills 的持續學習,以及透過 subagent 模型選擇、mgrep 和模組化程式庫的 token 優化。

Key Points

  • Session files pattern: save current state to .tmp files in .claude/sessions/YYYY-MM-DD-topic.tmp — what worked (with evidence), what failed, what’s untried; next session loads the file for context continuity
  • Strategic compaction: disable auto-compact; compact manually at logical phase transitions (exploration → execution, milestone completion) to preserve relevant context and avoid mid-task compaction
  • Dynamic system prompt injection: claude --system-prompt "$(cat memory.md)" injects context at system-prompt level (higher authority than tool results) — use CLI aliases for scenario-specific context (dev/review/research modes)
  • Memory persistence hooks trio: PreCompact (save state before summary), SessionStart (load recent context on open), Stop (persist learnings at session end)
  • Continuous learning skill: Stop hook evaluates each session for non-trivial patterns (debugging techniques, workarounds, project-specific knowledge) and saves them as new skills in ~/.claude/skills/learned/
  • /learn command: manual mid-session extraction when you’ve just solved something — drafts a skill file and asks for confirmation
  • Model selection for token optimization: Haiku for repetitive/clear-instruction workers, Opus for complex/multi-file/architectural tasks; Sonnet sits in an awkward middle (1.67x cheaper than Opus vs Haiku’s 5x cheaper)
  • System prompt slimming: Claude Code’s system prompt uses ~18k tokens; can be patched to ~10k (41% reduction in static overhead) — author doesn’t do this

Insights

The continuous learning pattern (Stop hook → extract patterns → save as skills) is the software engineering version of Karpathy’s compounding loop: every session adds to the knowledge base rather than disappearing. The key implementation detail is using the Stop hook rather than UserPromptSubmit — Stop runs once at session end, not on every message, keeping latency zero during active work.

The --system-prompt injection vs. .claude/rules/ distinction reveals an important Claude architecture detail: system prompt content has higher instruction authority than tool results. For most work this is marginal, but for strict behavioral rules or hard constraints, injection level matters. This is rarely documented explicitly.

The reflection agent pattern (from @RLanceMartin) — building a “diary” of what works and what doesn’t, updated after each session — is essentially what the Karpathy wiki does for research knowledge, but applied to coding session patterns. Both are the same underlying mechanism: structured retrospective capture that makes future work cheaper.

Connections

Raw Excerpt

When Claude Code discovers something that isn’t trivial — a debugging technique, a workaround, some project-specific pattern — it saves that knowledge as a new skill. Next time a similar problem comes up, the skill gets loaded automatically. Why did I use a Stop hook instead of UserPromptSubmit? UserPromptSubmit runs on every single message you send — that’s a lot of overhead. Stop runs once at session end — lightweight, doesn’t slow you down during the session, and evaluates the complete session rather than piecemeal.