Pin an Approved Model List for Regulated Sessions

A model-governance policy that names which models are approved for a regulated workflow is only as good as its enforcement. This blocks any switch to a model not on the configured list, regardless of how the switch was requested.

Model & Permission Governance Event: PreModelSwitch Claude Code Open source · Free

1. Save this as the hook script

#!/usr/bin/env python3
"""PreModelSwitch hook — blocks switching to a model outside an approved
list."""
import json, sys

data = json.load(sys.stdin)
to_model = data.get("to_model", "")

APPROVED_MODELS = {"claude-sonnet-5", "claude-opus-5-5"}

if to_model not in APPROVED_MODELS:
    print(f"BLOCKED: '{to_model}' is not on the approved model list for "
          f"this project ({sorted(APPROVED_MODELS)}).", file=sys.stderr)
    sys.exit(2)

sys.exit(0)

Save to a path inside your project, e.g. .claude/hooks/pin-approved-model-list.sh, then chmod +x it.

2. Wire it into .claude/settings.json

{
  "hooks": {
    "PreModelSwitch": [
      {
        "hooks": [
          {"type": "command", "command": "python3 .claude/hooks/pin-approved-model-list.py"}
        ]
      }
    ]
  }
}

Before you use this

A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.

Related hooks

← All hooks
FEEDBACK