skip to content

A Claude Code skill says to run a spec check after each edit, yet Claude does it once — fix the skill or add a hook?

level: middleimportance: should knowfreq 14%

answer

  1. read once, never re-read
  2. a request against a guarantee
  3. rule or judgement call
  4. hooks in the skill's frontmatter
  5. compaction keeps only the start

basics

~20 s

If the check must run after every edit, use a hook: a skill is read once and applied with judgement, while a hook fires on its event every time. Rewording the skill suits judgement calls only.

solid answer

~50 s

A skill's content enters the conversation once, when it is invoked, and Claude Code doesn't re-read the file on later turns, so a step written as a one-off ("run the check") easily reads as done. Claude Code's guidance splits the fix three ways. A rule that must hold every time belongs in a hook, which runs whenever its event fires, here a `PostToolUse` hook matching `Edit|Write` that runs the checker, whether or not Claude is following the skill; you can keep it with the skill by declaring it in the skill's `hooks` frontmatter, which registers it on invocation for the rest of the session. Guidance that genuinely needs judgement should be reworded to cover the whole task ("after every edit"). And if the session compacted, invoke the skill again, because only its first 5,000 tokens came back.

code

yaml · 12 lines
yaml
---
name: api-spec-change
description: Edits the OpenAPI spec following the team's API conventions. Use when adding or changing an endpoint, schema or response.
hooks:
  PostToolUse:
    - matcher: "Edit|Write"
      hooks:
        - type: command
          command: "${CLAUDE_PROJECT_DIR}/scripts/check-api-spec.sh"
---

After every spec edit, read the check's findings and fix each one before moving on.

go deeper

for a junior

Remember the split: skills and CLAUDE.md are instructions Claude may not follow, while hooks run every time their event fires.

for a middle

Explain the skill lifecycle that causes drift: read once, never re-read, capped after compaction, and the three documented fixes that match each cause.

for a senior

Show the design: a PostToolUse hook in the skill's frontmatter or in settings for the guarantee, skill text for how to respond to failures, and the limits of a frontmatter hook.

for a principal

Discuss where a team draws the line between guidance and guarantees, and how it keeps enforcement in reviewed, shared configuration rather than scattered across skills.

## Why Claude follows a skill once and then drifts When you or Claude invoke a skill, Claude Code renders `SKILL.md` and adds it to the conversation as **one message**. That message stays across later turns, but Claude Code **does not re-read the file**, so Claude works from the instructions as it first understood them. A step written as a single action, "run the spec check", is easily read as done after the first edit. The skill is guidance: Claude interprets it, weighs it against everything else in the conversation, and may skip it. A long session adds a second cause. When the conversation is **auto-compacted**, Claude Code re-attaches the most recent invocation of each skill after the summary, keeping the first **5,000 tokens** of each within a combined **25,000 tokens**, filled from the most recently invoked skill. Instructions near the bottom of a long skill, or an older skill entirely, can be gone. Invoking the skill again is not always a full refresh either. When the rendered content is identical to the copy already in context, Claude Code adds only a short note that the skill is already loaded; it appends the full content again only when the rendering differs, for example because the arguments or an injected command's output changed. ## Three causes, three fixes | What you see | Cause | Fix | | :- | :- | :- | | A step that must happen every time was skipped | It is a rule, and a skill can't guarantee anything | Move it into a **hook** | | Judgement-based guidance was applied once | It was worded as a one-time step | Reword it to cover the whole task, such as "after every edit" | | Claude stopped following after a long session | Compaction truncated or dropped the skill | Invoke the skill again; put critical instructions near the top | ## Skill against hook, in general - **What runs.** A hook runs a shell command, HTTP request, MCP tool call, LLM prompt or subagent; a skill is instructions Claude reads and follows. - **What triggers it.** A hook fires on a lifecycle event such as `PostToolUse` or `SessionStart`; a skill loads when you type `/name` or Claude matches its description. - **Determinism.** A hook always fires on its event; with a skill, Claude interprets the steps and the outcome can vary. - **Context cost.** A hook costs nothing unless it returns output; a skill's description is listed each turn and its body stays once loaded. - **Best for.** Hooks suit checks, blocking unsafe commands, logging and notifications; skills suit workflows that need reasoning, reference material and multi-step tasks. The same line applies to prohibitions: "never edit `.env`" in a skill or in CLAUDE.md is a request; a `PreToolUse` hook that blocks the edit is enforcement. ## Keeping the hook with the skill A skill's frontmatter can declare `hooks`, in the same format as hooks in settings files: - Claude Code **registers them when you or Claude invoke the skill** and keeps running them for the rest of the session, on turns after the skill's own turn as well. - `once: true` removes a hook after its first successful run. It is honoured only for hooks declared in skill frontmatter. - The limit: a frontmatter hook exists only after the skill has been invoked in that session. If the check must run in every session whether or not anyone used the skill, configure the hook in a settings file instead; building that hook is the hooks material's subject. ## Dividing the work The strongest design uses both, each for what it is good at. The hook runs the checker on every `Edit` or `Write`, and its output lands in context for Claude to read; the skill tells Claude how to interpret the failures and fix them. Claude Code's own comparison describes the same split: a `PostToolUse` hook runs your linter, and a skill tells Claude how to resolve what it reports. ## Choosing in practice 1. Ask whether a skipped run would be a defect. If yes, it needs a hook. 2. Ask whether the step needs reasoning about what to do. If yes, that part stays in the skill. 3. If both are true, use both: the hook for the guarantee, the skill for the response. 4. Re-test in a fresh session, including after a long task, to confirm the behaviour holds.

  • If the hook lives in the skill's frontmatter, when does it stop running?
    Claude Code registers it when you or Claude invoke the skill and keeps it for the rest of the session, including turns after the skill's own. With `once: true` it is removed after its first successful run. It never exists in a session where the skill wasn't invoked, so a check that must run everywhere belongs in a settings file.
  • Why not repeat the instruction in CLAUDE.md as well?
    CLAUDE.md is guidance too. It is always loaded and is re-read after compaction, so repeating the step there may raise compliance, but Claude can still skip it. Only an enforcing layer such as a hook or a permission rule makes an action happen or blocks it. Use CLAUDE.md or the skill to explain why, and the hook to make it happen.

  • Claude Code re-reads SKILL.md at the start of every turn.
  • Writing MUST in capitals turns a skill step into a guarantee.
  • A hook in a skill's frontmatter runs in every session from startup.
  • After compaction, invoked skills come back in full.
  • A hook costs as much context as a skill on every turn.