My project uses uv to manage Python dependencies, but Claude Code habitually defaults to python or pip install. I tried using Skills and Hooks to enforce this standard and encountered quite a few pitfalls.
Goal
- Create a Skill: Inform Claude that the project uses uv
- Create a Hook: Intercept
python/pipcommands - Verify effectiveness
Troubleshooting Journey
First Attempt: Wrong Skill File Structure (Commit 8a05759)
| |
The frontmatter also needed changes:
| |
Key points:
- Filename must be
SKILL.md, placed in the corresponding directory - Frontmatter requires a
namefield descriptionshould be detailed to help Claude identify when to trigger
Second Attempt: Hook Only Warns Without Blocking (Commit d250c3b)
Initially wrote the Hook in Bash, which only displayed warnings but didn’t prevent execution. Also tried configuring environment.PATH, which didn’t work.
Third Attempt: Wrong Hook Exit Code (Commit d3790a4)
Tried using exit 1 to block commands, but it still didn’t work.
Correct exit codes:
exit 0: Allow executionexit 1: Hook fails, but doesn’t block the toolexit 2: Actually blocks tool execution ✅
Fourth Attempt: Fixed Skill Format (Commit 2595b68)
Found the file structure was wrong, changed to the correct skills/xxx/SKILL.md format.
Fifth Attempt: Rewrote Hook in Python (Commit dcc726d)
Bash JSON parsing was too fragile, ultimately rewrote in Python:
| |
Configuration file (.claude/settings.json):
| |
Notes:
matchercan directly specify the tool name, no need for expressions- Use
$CLAUDE_PROJECT_DIRto reference the project path environment.PATHconfiguration doesn’t work in Hooks, don’t waste time on it
Final File Structure
| |
Testing
✅ Block regular commands:
| |
✅ Allow version checks:
| |
✅ Skill active:
When asked “how to run Python scripts,” Claude will proactively suggest using uv run.
Key Points
Claude Code Sometimes Doesn’t Proactively Query Specifications
I explicitly requested creating Hooks and Skills, but Claude Code started writing code without first checking the official documentation. This led to:
- File structure errors multiple times
- Wrong exit codes
- Incorrect JSON parsing approach
If it had used WebFetch to read the official Hook and Skill documentation before starting, all these pitfalls could have been avoided.
This isn’t about users needing to read documentation, but rather that AI agents should check specifications before executing unfamiliar tasks.
Skills and Hooks Can Indeed Enforce Claude Code’s Behavior
Hooks can constrain Python commands to provide the correct command suggestions. This is also the approach used in Kilo Code.