Agent Engineering / Tool Use
Author agent hooks that are fast, deterministic and fail safe
- MUST attach each hook to the narrowest event and matcher that covers its job (for example
PreToolUsewith matcherBashorEdit|Write), not to every event or tool. - MUST make the hook deterministic: same input, same decision. No model calls in a hook that enforces policy; keep judgement for prompt or agent hooks that only advise.
- MUST parse the event input (JSON on stdin, or the HTTP body) defensively and treat every field — tool input, file paths, prompts — as untrusted data. Never pass it to a shell unquoted or
evalit. - MUST block with the harness’s explicit blocking signal and a reason the agent can act on. In Claude Code, exit code
2with the reason on stderr blocks; any other non-zero exit is a non-blocking error and the action proceeds. - MUST decide what a failure, crash or timeout means for each enforcing hook. In Claude Code a timed-out
PreToolUsehook does not block unless"onFailure": "block"is set; set it for policy hooks. - MUST keep hooks fast. A hook on every tool call adds latency to every step; do slow work asynchronously or in
PostToolUse. - MUST reference scripts by an absolute or project-anchored path (for example
${CLAUDE_PROJECT_DIR}/.claude/hooks/…), and prefer exec form (command plus arguments, no shell) over a shell string. - MUST NOT put secrets in hook configuration or scripts. Read them from the environment; for HTTP hooks, allow only the variables the hook needs.
- MUST test each hook with recorded event payloads: an allowed case, a blocked case, malformed input, and a timeout.
- SHOULD write hook scripts in a language with real JSON parsing (Bun/TypeScript, Python, Go) rather than
grepover raw JSON, and give each script anauthorand purpose header. - SHOULD log each decision (event, tool, decision, reason) where the operator can audit it, without logging secrets or file contents.
- SHOULD use a hook’s ability to rewrite input or add context sparingly, and document it: silent changes to what the agent asked for are hard to debug.
When to apply
Section titled “When to apply”Whenever you write a hook for an agent harness (Claude Code hooks, or the equivalent in other tools). For when to use a hook at all, see BP-0021.
Examples
Section titled “Examples”.claude/settings.json:
{ "hooks": { "PreToolUse": [ { "matcher": "Edit|Write", "hooks": [{ "type": "command", "command": "bun", "args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/protect-paths.ts"], "onFailure": "block" }] } ] }}.claude/hooks/protect-paths.ts:
// author: Stephen Eaton// PreToolUse hook: block edits to generated, vendored and secret paths.const PROTECTED = [/^\.env/, /(^|\/)secrets\//, /^internal\/gen\//, /^vendor\//];
let input: any;try { input = JSON.parse(await Bun.stdin.text()); }catch { console.error('protect-paths: unreadable hook input'); process.exit(2); }
const path = String(input?.tool_input?.file_path ?? '');const rel = path.startsWith(input?.cwd ?? '') ? path.slice((input.cwd ?? '').length + 1) : path;if (PROTECTED.some((re) => re.test(rel))) { console.error(`Blocked: ${rel} is protected (generated, vendored or secret). Edit the source instead.`); process.exit(2);}process.exit(0);Test with recorded payloads:
echo '{"cwd":"/repo","tool_input":{"file_path":"/repo/.env"}}' | bun .claude/hooks/protect-paths.ts; echo $? # 2echo '{"cwd":"/repo","tool_input":{"file_path":"/repo/src/a.ts"}}' | bun .claude/hooks/protect-paths.ts; echo $? # 0echo 'not json' | bun .claude/hooks/protect-paths.ts; echo $? # 2Anti-patterns
Section titled “Anti-patterns”exit 1for a block, which in Claude Code lets the action proceed.- A policy hook that silently allows on crash or timeout.
bash -c "$(jq -r .tool_input.command)"-style handling of agent input.- One catch-all hook on every event that grows into an unreviewable script.
- Hooks that call a model to decide whether to allow a dangerous action.
Checklist
Section titled “Checklist”- Narrowest event and matcher
- Deterministic; input parsed defensively, never executed
- Explicit block signal with an actionable reason
- Failure and timeout behaviour set (
onFailure: blockfor policy hooks) - Fast; project-anchored path; exec form; no secrets
- Tested with allow, block, malformed and timeout payloads