| title | Hooks System Documentation |
|---|---|
| description | How Claude Code hooks enable automatic context loading and skill activation |
| category | reference |
Finance Guru uses Claude Code hooks to enable automatic context loading, skill activation, and validation. This document explains how the hooks system works and how to customize it.
Hooks are scripts that run at specific points in Claude's workflow:
| Hook Type | When It Runs | Purpose |
|---|---|---|
| SessionStart | When session begins | Load context, initialize state |
| UserPromptSubmit | When user submits prompt | Suggest skills, modify context |
| PreToolUse | Before tool executes | Validate, gate actions |
| PostToolUse | After tool completes | Track changes, update state |
| Stop | When stop requested | Validate, cleanup |
Located in .claude/settings.json:
{
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "npx tsx $CLAUDE_PROJECT_DIR/.claude/hooks/load-fin-core-config.ts"
}
]
}
],
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/skill-activation-prompt.sh"
}
]
}
],
"PostToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/post-tool-use-tracker.sh"
}
]
}
],
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/stop-build-check-enhanced.sh"
},
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/error-handling-reminder.sh"
}
]
}
]
}
}Purpose: Loads Finance Guru context at session start.
Location: .claude/hooks/load-fin-core-config.ts
-
fin-core skill (
.claude/skills/fin-core/SKILL.md)- Core Finance Guru knowledge
- Agent routing rules
- Compliance requirements
-
System configuration (
fin-guru/config.yaml)- Agent roster (13 agents)
- Task definitions (21 tasks)
- Tool integrations
- Workflow pipeline
-
User profile (
fin-guru/data/user-profile.yaml)- Portfolio structure
- Risk tolerance (aggressive)
- Investment strategy
- Current holdings
-
System context (
fin-guru/data/system-context.md)- Repository structure
- Privacy rules
- Agent team overview
-
Latest portfolio data (
notebooks/updates/)Balances_for_Account_{account_id}.csv- Account balances, margin dataPortfolio_Positions_MMM-DD-YYYY.csv- Current holdings
The hook checks file age and alerts if data is stale:
⚠️ OUTDATED: Balances file is older than 7 days
⚠️ OUTDATED: Portfolio positions file is older than 7 days
📥 ACTION REQUIRED:
Please update your portfolio data by downloading the latest files from Fidelity
// 1. Reads session info from stdin
const input: HookInput = JSON.parse(inputData);
// 2. Loads all context files
const skillContent = loadFile(skillPath);
const configContent = loadFile(configPath);
const profileContent = loadFile(profilePath);
const systemContext = loadFile(systemContextPath);
// 3. Finds latest portfolio files (by date in filename)
const latestBalances = getLatestFile(updatesDir, /^Balances_.*\.csv$/);
const latestPositions = getLatestPositionsFile(updatesDir);
// 4. Outputs formatted context (injected as system-reminder)
console.log(output);Purpose: Automatically suggests relevant skills based on user prompts and file context.
Location: .claude/hooks/skill-activation-prompt.sh
- Reads user prompt from stdin
- Loads
skill-rules.jsonfor activation triggers - Matches prompt against:
- Keywords: "backend", "API", "route"
- Intent patterns: Regex matching user intent
- File paths: What files user is working with
- Content patterns: Code patterns in files
- Injects skill suggestions into Claude's context
{
"skill-name": {
"type": "domain | guardrail",
"enforcement": "suggest | block",
"priority": "high | medium | low",
"promptTriggers": {
"keywords": ["keyword1", "keyword2"],
"intentPatterns": ["regex.*pattern"]
},
"fileTriggers": {
"pathPatterns": ["src/api/**/*.ts"],
"contentPatterns": ["import.*Prisma"]
}
}
}- suggest: Skill appears as suggestion, Claude can choose to use it
- block: Must use skill before proceeding (guardrail)
Purpose: Tracks file changes to maintain context across sessions.
Location: .claude/hooks/post-tool-use-tracker.sh
Triggers on: Edit, MultiEdit, Write tool calls
- Monitors file modifications
- Records which files were changed
- Creates cache for context management
- Auto-detects project structure
Helps Claude understand what parts of your codebase are actively being modified, which improves context relevance.
Purpose: Validates TypeScript compilation before session ends.
Use when: You want to ensure no type errors before completing work.
Customization required: Edit service detection for your project structure.
Purpose: Ensures error handling is in place before completion.
Runs: TypeScript hook via tsx that checks for error patterns.
#!/bin/bash
# .claude/hooks/my-custom-hook.sh
# Read stdin (JSON with session context)
input=$(cat)
# Process and output
echo "My custom hook ran"
exit 0chmod +x .claude/hooks/my-custom-hook.sh{
"hooks": {
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/my-custom-hook.sh"
}
]
}
]
}
}echo '{"session_id": "test"}' | ./.claude/hooks/my-custom-hook.shHooks receive JSON on stdin:
{
"session_id": "abc123",
"event": "session_start",
"prompt": "user's prompt text", // UserPromptSubmit only
"tool_name": "Edit", // PreToolUse/PostToolUse only
"tool_input": {...} // PreToolUse/PostToolUse only
}Output is injected into Claude's context as <system-reminder>:
Your hook output here will appear in Claude's context
- 0: Success, continue
- Non-zero: Failure, may block depending on hook type
# Test a hook manually
echo '{"session_id": "test", "event": "session_start"}' | \
npx tsx .claude/hooks/load-fin-core-config.tsls -la .claude/hooks/*.sh | grep rwxcat .claude/settings.json | jq .| Issue | Solution |
|---|---|
| Hook not running | Check file is executable (chmod +x) |
| Permission denied | Check file permissions |
| Hook hangs | Ensure script exits (doesn't wait for input) |
| Output not appearing | Check stdout, not stderr |
| File | Type | Purpose |
|---|---|---|
load-fin-core-config.ts |
SessionStart | Load Finance Guru context |
skill-activation-prompt.sh |
UserPromptSubmit | Auto-suggest skills |
skill-activation-prompt.ts |
(Called by .sh) | TypeScript logic |
post-tool-use-tracker.sh |
PostToolUse | Track file changes |
stop-build-check-enhanced.sh |
Stop | TypeScript validation |
error-handling-reminder.sh |
Stop | Error pattern check |
error-handling-reminder.ts |
(Called by .sh) | TypeScript logic |
tsc-check.sh |
(Optional) | TypeScript compilation |
trigger-build-resolver.sh |
(Optional) | Auto-fix build errors |
- Keep hooks fast: They run synchronously and can slow down the session
- Exit cleanly: Always
exit 0for success - Output to stdout: Claude only sees stdout, not stderr
- Handle missing files gracefully: Don't crash on missing optional files
- Test manually first: Run hooks standalone before adding to settings.json
- Use TypeScript for complexity: Wrap complex logic in .ts files called by .sh wrappers
The hooks system is designed for token efficiency:
- Context is loaded once at session start, not on every prompt
- Skills load on-demand, not all upfront
- File tracking helps Claude stay focused on relevant code
- Heavy computation (CLI tools) happens outside context window
Typical session uses only 26% of context (52k/200k tokens) with full Finance Guru loaded.