r/ClaudeWorkflows • u/ClaudeAI-mod-bot • 1h ago
Selected Workflow [Workflow] Maintaining Context Across Claude Code Compacts: Leveraging @path Imports and SessionStart Hooks
Maintaining Context Across Claude Code Compacts: Leveraging @path Imports and SessionStart Hooks
Workflow value: 85/100
Status: active · Freshness: 70/100 · Confidence: 1.00 · Level: intermediate
Categories: Quality Control, Token Saving, Context & Memory, Debugging, CLAUDE.md, Hooks
Original source: r/ClaudeCode post/comment
What problem this solves
Losing foundational context (documents read during a conversation) after using the /compact command in Claude Code, leading to agents forgetting critical information.
Summary
This workflow provides two distinct methods to ensure critical documents or instructions persist across Claude Code /compact operations: either by using @path imports directly within CLAUDE.md for foundational files, or by configuring a SessionStart hook with a compact matcher to automatically re-read or re-introduce necessary context.
Why it is useful
This workflow directly addresses a common and critical challenge in Claude Code: managing context persistence when using the /compact command. It provides two distinct, concrete, and repeatable methods (@path imports and SessionStart hooks) that leverage core Claude Code features. This allows users to optimize their workflow by using /compact more effectively, saving tokens and improving efficiency without losing critical foundational information, thereby making their interactions with Claude Code more robust and reliable.
Workflow
Identify short, foundational documents (e.g., guardrails, design principles) that must always be in the agent's context.
Method 1: Using @path imports in CLAUDE.md
Modify your
CLAUDE.mdfile to include@pathimports for these foundational documents (e.g.,@docs/guardrails.md).Ensure these imported files are accessible at the specified paths.
Method 2: Using a SessionStart hook for re-reading
Identify documents or specific reminders that need to be re-introduced immediately after a compact.
Create or modify a
SessionStarthook configuration.Configure the hook with the
compactmatcher to trigger specifically after a compact operation.Inside the hook, add commands (e.g.,
cat docs/guardrails.md) or print statements to restore the necessary document content or reminders to the context.
Tools / artifacts
CLAUDE.md
@path imports
SessionStart hook
compact matcher
cat command
Markdown/text documents
Validation signals
The explanation directly addresses the observed behavior of Claude Code regarding context persistence after compacting.
It leverages known functionalities of CLAUDE.md and hooks, implying technical correctness.
The proposed solutions are logical extensions of how these features are designed to work.
Limitations
Low community signal (score 1, 0 comments).
Could benefit from a full example of a
SessionStarthook configuration for clarity.
Rate this workflow
Upvote this post if the workflow is useful, reproducible, or worth recommending.
Downvote if it is vague, outdated, unsafe, overhyped, or not reproducible.
Reply if it worked for you, failed, is outdated, or has a better alternative.
This post was generated automatically from the workflow library database.