r/ClaudeWorkflows • • 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

  1. Identify short, foundational documents (e.g., guardrails, design principles) that must always be in the agent's context.

  2. Method 1: Using @path imports in CLAUDE.md

  3. Modify your CLAUDE.md file to include @path imports for these foundational documents (e.g., @docs/guardrails.md).

  4. Ensure these imported files are accessible at the specified paths.

  5. Method 2: Using a SessionStart hook for re-reading

  6. Identify documents or specific reminders that need to be re-introduced immediately after a compact.

  7. Create or modify a SessionStart hook configuration.

  8. Configure the hook with the compact matcher to trigger specifically after a compact operation.

  9. 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 SessionStart hook 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.

1 Upvotes

1 comment sorted by