Hooks, Task Decomposition & Session Management

Estimated time: 30 minutes

This lesson covers three related Domain 1 topics: Agent SDK hooks for deterministic policy enforcement, task decomposition strategies (fixed vs adaptive), and session management techniques (resume, fork, fresh-start) for long-running work.

Agent SDK Hooks

Hooks intercept the agent loop at specific points. PostToolUse hooks run after a tool returns but before Claude sees the result, allowing you to normalize or rewrite the output. Tool call interception hooks run before a tool executes, allowing you to block the call or redirect.

Hooks provide deterministic guarantees. Prompts provide probabilistic compliance. The pattern: anything you absolutely need to happen, encode as a hook. Anything that's a soft preference, encode in the system prompt.

Common Hook Use Cases

  • PostToolUse normalization. Three weather APIs return Unix timestamps, ISO 8601 strings, and numeric status codes. A PostToolUse hook normalizes all three into a single canonical format before Claude sees them.
  • Policy enforcement. Block process_refund calls where amount > $500 and instead route to escalate_to_human. The model's prompt may say "escalate large refunds," but the hook guarantees it.
  • Audit logging. Capture every tool call's parameters and outcome for compliance review.

Task Decomposition: Fixed vs Adaptive

Two decomposition strategies exist, suited to different problem shapes:

StrategyWhen to use
Fixed sequential pipelinePredictable multi-aspect work like code review (security pass → performance pass → style pass)
Dynamic adaptive decompositionOpen-ended investigation where subtasks are determined by what's discovered in earlier steps

For a code review of a known repository, fixed pipelines win — you know what passes you need. For research investigating an unfamiliar codebase, adaptive wins — you can't enumerate the right subtasks until you've mapped the structure.

Session Management

Three techniques for handling continuity across long-running work:

TechniqueWhen to use
--resume <session-name>Prior context is mostly valid. Continue an existing conversation.
Fresh session + structured summaryPrior tool results are stale (files have changed significantly). Resuming with stale state causes the model to act on wrong assumptions.
fork_sessionExploring divergent approaches from a shared analysis baseline. Each fork can experiment without polluting the others.

Critical: When resuming after code modifications, you must inform the agent that files have changed. Resuming a session that has tool results referencing files the user has since edited gives the agent a stale model of reality. Starting fresh with a structured summary is more reliable than resuming.

Skills to Develop

  1. Choose hooks over prompt instructions when guaranteed compliance is required.
  2. Use PostToolUse to normalize heterogeneous tool outputs before they enter the model's context.
  3. Pick fixed pipelines for predictable multi-aspect work; adaptive decomposition for open-ended investigation.
  4. Choose between --resume, fresh start with summary, and fork_session based on whether prior tool results are still valid.
Exam tip: If a question describes the need for guaranteed compliance with a business rule (refund limit, escalation criteria, security policy), the correct answer involves a hook or programmatic enforcement — never "improve the system prompt".