Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/assets/prompts/skills/agent-customization/references/hooks.md
13405 views

Hooks (.json)

Deterministic lifecycle automation for agent sessions. Use hooks to enforce policy, automate validation, and inject runtime context.

Locations

PathScope
.github/hooks/*.jsonWorkspace (team-shared)
.claude/settings.local.jsonWorkspace local (not committed)
.claude/settings.jsonWorkspace
~/.claude/settings.jsonUser profile

Hooks from all configured locations are collected and executed; workspace and user hooks do not override each other.

Hook Events

EventTrigger
SessionStartFirst prompt of a new agent session
UserPromptSubmitUser submits a prompt
PreToolUseBefore tool invocation
PostToolUseAfter successful tool invocation
PreCompactBefore context compaction
SubagentStartSubagent starts
SubagentStopSubagent ends
StopAgent session ends

Configuration Format

{ "hooks": { "PreToolUse": [ { "type": "command", "command": "./scripts/validate-tool.sh", "timeout": 15 } ] } }

Each hook command supports:

  • type (must be command)

  • command (default)

  • windows, linux, osx (platform overrides)

  • cwd, env, timeout

Input / Output Contract

Hooks receive JSON on stdin and can return JSON on stdout.

  • Common output: continue, stopReason, systemMessage

  • PreToolUse permissions are read from hookSpecificOutput.permissionDecision (allow | ask | deny)

  • PostToolUse output can block further processing with decision: block

PreToolUse example output:

{ "hookSpecificOutput": { "hookEventName": "PreToolUse", "permissionDecision": "ask", "permissionDecisionReason": "Needs user confirmation" } }

Exit codes:

  • 0 success

  • 2 blocking error

  • Other values produce non-blocking warnings

Hooks vs Other Customizations

PrimitiveBehavior
Instructions / Prompts / Skills / AgentsGuidance (non-deterministic)
HooksRuntime enforcement and deterministic automation

Use hooks when behavior must be guaranteed (for example: block dangerous commands, force validation, auto-inject context).

Core Principles

  1. Keep hooks small and auditable

  2. Validate and sanitize hook inputs

  3. Avoid hardcoded secrets in scripts

  4. Prefer workspace hooks for team policy, user hooks for personal automation

Anti-patterns

  • Running long hooks that block normal flow

  • Using hooks where plain instructions are sufficient

  • Letting agents edit hook scripts without approval controls