Claude Memory Hooks: Save and Reload the Right Context

Claude Code hooks are shell commands that run at session lifecycle events. Use a Stop hook to save context automatically and reload the right record next time.

Updated 5 min read

Claude Code hooks are shell commands that run automatically at specific points in a session—before a tool runs, after it finishes, or when the session stops. Used alongside a tool like fenbs, which holds shared checkpoints and human-approved rules outside any single chat, they give you a repeatable way to carry the right context from one session into the next without pasting it in by hand.

This is distinct from Claude's memory skill, which saves facts about you or your preferences. Hooks are automation—they run code, not store facts. The combination is: hooks trigger the save, an outside record holds it, and the next session reads it back.

What Claude Code hooks are

Claude Code hooks are commands you configure to run at lifecycle events. The main events are PreToolUse (before any tool call), PostToolUse (after a tool call returns), and Stop (when Claude finishes a turn or the session ends). You write a shell command; Claude Code runs it at the right moment.

Hooks run in your environment, not inside Claude. They can write files, call a script, or trigger an external service. Claude Code passes information about the event—such as which tool ran and what it returned—as JSON on standard input, so your script can act on the actual result.

Hooks are configured in your Claude Code settings, either at user level (~/.claude/settings.json) or at project level (.claude/settings.json). Project-level hooks apply only when Claude Code opens that folder. Check Anthropic's Claude Code documentation (opens in a new tab) for the current JSON structure, since the syntax can change between releases.

Saving context with a Stop hook

A Stop hook runs when Claude finishes a response or the session ends. That makes it the natural place to save what just happened. A simple hook calls a script that writes a short context file—what was completed, what is still open, and any decisions that were confirmed.

Example Stop hook in .claude/settings.json
{
  "hooks": {
    "Stop": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "bash .claude/save-context.sh"
          }
        ]
      }
    ]
  }
}

The script itself can do whatever your project needs: append to a markdown log, call an API, or run a command that writes to a shared record. Keep it short and exit with code 0 on success. If the script exits with a non-zero code, Claude Code may treat it as a blocking error.

What to save and what to leave out

Not every response produces something worth saving. A session that only reads files and asks questions leaves very little that the next session cannot reconstruct. Save the things that take effort to rediscover.

  • What was completed in this session and how it was checked.
  • Decisions that were confirmed—what was chosen, by whom, and why.
  • Anything that was started but not finished, with the next step.
  • New constraints or rules that should apply from now on.
  • Tests that failed, with the last known state.

Leave out raw output, large file contents, and anything that is already recorded elsewhere. A long context note is slower to read than a short one and may push useful facts out of view. Write the minimum that lets the next session begin without needing to re-explain.

How fenbs helps

Keep shared checkpoints outside the chat

fenbs holds projects, tasks, Decisions and rules, and Where we left off outside any one AI app. A connected assistant reads the current record at the start of each session and saves progress before it stops—so the hook's job is just to trigger the save, not to decide what matters. Create a free account and add your first project to try it.

Create a free account

Free for one person · No credit card · Works with Claude, ChatGPT, Cursor and more

Reloading context at the start of a new session

A hook that runs at session start—or a CLAUDE.md instruction that asks Claude to read a file first—can load the saved record back. The simplest version is a PreToolUse hook that runs once on the first tool call and prints the saved notes, or a short instruction in CLAUDE.md: "Read .claude/context.md before starting."

Claude Code reads AGENTS.md and CLAUDE.md natively at session start, so you can point to a generated context file from there. The claude-code-hooks-examples post covers specific patterns for loading context this way.

Test the round-trip: save some notes, close the session, open a new one, and check whether Claude Code correctly summarizes where things stand. If it cannot, the saved notes are missing something or the reload instruction is not reaching it.

Human-approved rules and shared checkpoints

Hooks handle the mechanical part—triggering saves and reloads. They do not decide what the rules are or sign off on decisions. That separation matters: an assistant can write "Pending approval: change the logging format" but should not write "Approved: change the logging format" unless a person confirmed it.

Keep approved rules in a place a person can review and update, separate from the generated context notes. In fenbs, Decisions and rules appear first in the AI context that connected assistants read, so the rules are always visible before the current-state notes. See claude-code-memory for how Claude Code handles project memory files and how to structure them.

For shared work, Claude Code Projects shared memory (added September 17, 2026) lets teammates in the same Project see the same memory files. Hooks that write to those files give the whole team a shared starting point—though each person still reviews the output.

How to do this with fenbs

Connect Claude Code to fenbs over MCP and configure a Stop hook that calls fenbs_save_progress. Claude Code saves where each task stands and the project's where we left off, and reads them back at the start of the next session.

  1. Create a free account and add your project in fenbs. Add the current tasks and any rules that already apply.
  2. Connect Claude Code to fenbs from Integrations. Follow the MCP guide for Claude Code, sign in in the browser, and approve the access you want to give it.
  3. Ask Claude Code to call fenbs_save_progress at the end of each session—where each task stands and the project's next step. Add this as a CLAUDE.md instruction or a Stop hook that prompts Claude to save before closing.
  4. Add a CLAUDE.md instruction to call fenbs_get_context at the start of each session. Claude Code will read the Rules, Where we left off and In progress sections before starting work.
  5. After a few sessions, review the saved record with a teammate. Can they find the current task, the latest decision, and the next step without asking you? If not, adjust what the Stop hook saves.

fenbs does not connect to Microsoft Teams or Slack. If your team communicates there, copy the key decisions into fenbs by hand—or ask Claude Code to record them when they come up in a session.

Questions people ask.

Do Claude Code hooks work without a git repository?

Yes. Hooks run based on Claude Code lifecycle events, not repository state. A project-level hook in .claude/settings.json applies whenever Claude Code opens that folder, with or without a git repository.

Can a hook save context to fenbs automatically?

A hook can run a shell command that calls the fenbs MCP server, but MCP calls go through the AI assistant, not directly from a shell script. The practical pattern is a CLAUDE.md instruction that asks Claude Code to call fenbs_save_progress before ending each session, which Claude Code can do as part of its normal flow.

What is the difference between a Stop hook and a PreToolUse hook for context?

A Stop hook runs when the session ends—useful for saving what was done. A PreToolUse hook runs before each tool call—useful for checking a rule or loading a note before Claude reads or writes a file. Use Stop to save, PreToolUse to enforce a gate if you need one.

Can I use hooks to block Claude from running certain tools?

A PreToolUse hook can exit with a non-zero code to signal a problem, but the exact behavior depends on how Claude Code handles hook failures. Tool permissions in .claude/settings.json are the recommended way to restrict which tools Claude Code can use—hooks are better suited to observation and automation.

Where is the best place to store the saved context file?

A file inside .claude/ keeps it out of your source tree but in the project folder. If your team shares context, commit the file to version control or use a shared tool like fenbs so everyone reads the same record.

More in Agent harness (for developers)

All Agent harness (for developers) articles

Start with one thing.

Free for one person · No credit card · Works with Claude, ChatGPT, Cursor and more

There is nothing to set up first. Write one line and you’ve started.