Skip to content

Skill: develop-skill-hooks — patterns for enforcing skill context via hooks #21

@krisrowe

Description

@krisrowe

Summary

Create a skill that teaches how to build Claude Code hooks that enforce skill context injection. Covers the pattern of using PreToolUse hooks to detect when an agent is about to do something (e.g., write tests) and injecting relevant skill content via additionalContext if the skill hasn't been invoked yet.

Motivation

Discovered during development of claude-coding-plugin. Skills often don't auto-invoke when they should. Hooks can guarantee the agent has the right context before taking action, without relying on the agent to remember to invoke a skill.

Proven pattern to document

  • PreToolUse hook on Edit|Write with if field for path matching (e.g., tests/**)
  • Read session_id from stdin JSON
  • Track injection state per session in $CLAUDE_PLUGIN_DATA/sessions/<session_id>/
  • On first match: read SKILL.md, return exit 0 with additionalContext containing skill content (10k char limit)
  • Create marker file so injection is one-time per session

Key findings to include

  • additionalContext in hook response injects text into agent context without blocking the tool call
  • No SkillInvoked hook event exists — cannot detect skill invocation, so direct injection is more reliable
  • if field uses permission-rule syntax: ToolName(glob-pattern)
  • Session ID comes via stdin JSON, not env vars
  • Marker files under $CLAUDE_PLUGIN_DATA for plugin hooks, $XDG_CACHE_HOME for standalone hooks

Scope

  • The skill itself (SKILL.md)
  • Example hook script (shell or python)
  • Placement in the coding/ collection

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions