Claude Code Compaction Hook: Keep Task Context
Prepared and checked using AI. Sources are linked in the article.
To preserve task context after Claude Code compaction, save your task instructions and progress to files, then use a SessionStart hook with the compact matcher to inject their contents back into Claude’s context. The hook restores what you saved; keeping that checkpoint current is a separate part of the workflow.
Separate project rules from task progress
Put durable project conventions in CLAUDE.md. Project-root CLAUDE.md is re-read from disk after compaction, so you do not need a hook just to repeat those instructions. Nested instruction files and path-scoped rules reload on demand.
Use a task checkpoint for the changing details: the current objective, acceptance criteria, decisions, unfinished work, and verification results. For organizing shared project instructions, see AGENTS.md and CLAUDE.md.
For example, use .claude/task-context/task.md for the task specification and .claude/task-context/progress.md for its checkpoint. The following sample describes a pagination task; replace its contents with your actual requirements and progress.
Save this in .claude/task-context/task.md:
## Current task
Add pagination to the existing search endpoint.
## Acceptance criteria
- Preserve the existing response fields.
- Follow the repository's pagination conventions.
- Add coverage for empty results and the final page.
## Constraints
- Do not change authentication behavior.
- Ask before introducing a dependency.
## Checkpoint maintenance
Update .claude/task-context/progress.md after meaningful changes,
verification runs, and decisions, and before manually compacting
the conversation.
Start .claude/task-context/progress.md with:
## Saved progress
- Completed: task recorded; implementation has not started.
- Decisions: none yet.
- Verification: not run yet.
- Blockers: none recorded.
- Next action: inspect the current endpoint and related tests.
Treat this as a checkpoint. Verify the current files before
continuing, and replace stale entries when updating this note.
Keep these files short enough to read as a handoff. Plain stdout and each additionalContext string are capped at 10,000 characters, so keep the combined checkpoint output within that limit to preserve full restoration. Larger output becomes a file reference with a preview, without an instruction for Claude to read the file. Include exact file paths, unresolved questions, and commands with their actual results as work proceeds. Avoid copying whole logs or the entire conversation into the checkpoint.
Configure the SessionStart hook
This example assumes a macOS or Linux shell with cat available and one active task per project directory.
Use .claude/settings.json for a shared project hook, or .claude/settings.local.json for a personal project hook. If you create the local settings file yourself, add it to .gitignore. Merge the following configuration into existing settings, preserving other keys and hook entries:
{
"hooks": {
"SessionStart": [
{
"matcher": "compact",
"hooks": [
{
"type": "command",
"command": "cat \"$CLAUDE_PROJECT_DIR/.claude/task-context/task.md\" \"$CLAUDE_PROJECT_DIR/.claude/task-context/progress.md\""
}
]
}
]
}
}
CLAUDE_PROJECT_DIR points to the project root where the session started. Quoting both paths accommodates spaces. This command prints both files to stdout, which becomes context after compaction; no executable script is required.
The compact matcher covers automatic and manual compaction. To also load these files on a new session or a resumed session, replace the matcher with:
"matcher": "startup|resume|compact"
Only broaden it when these files identify the task you intend to continue. Otherwise, an old checkpoint could introduce unrelated instructions into a new task.
Do you need additionalContext JSON?
Plain stdout is sufficient for this hook. If an existing script already returns structured JSON, put the context inside hookSpecificOutput:
{
"hookSpecificOutput": {
"hookEventName": "SessionStart",
"additionalContext": "The task specification and current checkpoint go here."
}
}
This is an alternative output format. The cat configuration above does not need it.
Keep the checkpoint current
At the beginning of the task, ask Claude to read the task files. Give it a concrete maintenance instruction:
Read .claude/task-context/task.md and
.claude/task-context/progress.md before working. Keep
.claude/task-context/progress.md current after meaningful changes
and verification.
Record completed work, decisions, exact verification commands and
results, blockers, and the next action. Distinguish planned checks
from checks that actually ran.
Before manually compacting, ask Claude to update the checkpoint and review what it wrote. After unexpected errors or interruptions, check it yourself before continuing.
A command hook cannot directly invoke Claude’s tools or slash commands. Adding a PreCompact hook that prints “save your progress” therefore does not itself write a useful checkpoint. PreCompact runs before compaction; use it for a concrete save or snapshot operation if your workflow already has one. The SessionStart hook shown here handles restoration afterward.
The maintenance prompt still relies on Claude following instructions. Review the checkpoint at important boundaries instead of assuming it always reflects the latest work.
Verify restoration
- Confirm that both checkpoint files exist and contain the intended task.
- Run
/hooksinside Claude Code and confirm the entry appears underSessionStartwith thecompactmatcher. - Ask Claude to update the progress file, then inspect it.
- Run
/compactinside Claude Code. - Ask Claude to state the task constraints and next action from the restored checkpoint before editing further.
For a more specific check, put a distinctive, harmless constraint in the task file and ask Claude to identify it afterward. Compare its answer with the file; a generic continuation is weak evidence of restoration.
If the hook does not appear, check the settings location and JSON syntax. Restart the session if a settings change has not been picked up. If the command fails, check that both paths exist and are readable.
Keep parallel tasks separate
Use a separate checkpoint for each task directory. A single shared progress file gives competing tasks a place to overwrite one another’s state.
There is also a path caveat: if Claude enters a different worktree during the session, CLAUDE_PROJECT_DIR stays at the original project root. The hook’s input JSON has a cwd field that follows the working directory. This example deliberately restores files from the launch root; if your workflow changes worktrees mid-session, adapt the hook to select the correct task explicitly.
For the surrounding workflow, see resuming an agent session in the right worktree.
Where Parallel Code fits
Parallel Code is our free, open-source desktop app for macOS and Linux that runs several AI coding agents in parallel, each isolated in its own git worktree. Separate worktrees give each task a place for its own checkpoint files. Configure the restoration hook in the Claude Code settings used by each task.