Back to catalogue
DocumentationPostCompact
Save compaction summary to log
Keep a readable trail of every compaction
Appends the compaction summary generated by Claude Code to a markdown log file inside the project. Provides a persistent record of what was summarized across context window resets, useful for long-running sessions and post-session review.
What does the Save compaction summary to log hook do?
Save compaction summary to log is a Claude Code PostCompact hook. It fires automatically at that lifecycle event — outside the model, so it can't be skipped or forgotten. Keep a readable trail of every compaction.
Because it is a deterministic Node.js script, it executes on every matching event without relying on the model to remember — the guarantee that makes agentic workflows safe to automate.
Use cases
- Keep a human-readable history of every compaction event in the project
- Review what Claude summarized at context boundaries for debugging or audit
- Reconstruct the timeline of a long session from compaction summaries
Tags
#compaction#logging#documentation#context
settings.json fragment
{
"hooks": {
"PostCompact": [
{
"hooks": [
{
"command": "node $CLAUDE_PROJECT_DIR/.claude/hooks/post-compact-save-summary.mjs",
"type": "command"
}
]
}
]
}
}Script · .claude/hooks/post-compact-save-summary.mjs
#!/usr/bin/env node
// @hookstack post-compact-save-summary
// Journalise le résumé de compaction dans .claude/compaction-log.md (PostCompact)
import { appendFileSync, mkdirSync, readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
export function run(
input,
{
append = appendFileSync,
mkdir = mkdirSync,
projectDir = process.env.CLAUDE_PROJECT_DIR ?? ".",
now = () => new Date().toISOString(),
} = {},
) {
const summary = input.compact_summary ?? "";
if (!summary.trim()) return null;
const logPath = join(projectDir, ".claude", "compaction-log.md");
try {
mkdir(dirname(logPath), { recursive: true });
} catch {
/* exists */
}
const entry = `\n## ${now()} (${input.trigger ?? "auto"})\n${summary}\n`;
append(logPath, entry);
return entry;
}
/* v8 ignore next 4 */
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const input = JSON.parse(readFileSync(0, "utf8"));
run(input);
}
Learn more
Related hooks
- Session summary generationAn automatic changelog of what the agent shipped
- Docs consistency reminderNo more READMEs telling two different stories
- Force implementation doc at stopNo code change ships without a doc/implementation/ trace
- OKF bundle staleness reminder on session startNever let your project knowledge base go stale and lie to you