Hooks
Hooks are registered through the phase-based HookRegistry. The old
single-slot AgentHooks dataclass is no longer part of the public API.
Quick start
Section titled “Quick start”from agiwo.agent import ( Agent, AgentConfig, HookPhase, HookRegistry, observe, transform,)
async def add_prelude(payload: dict) -> dict: return {"prelude_text": "Please be concise."}
async def log_step(payload: dict) -> None: step = payload["step"] print(f"{step.role.value} step committed: {step.sequence}")
hooks = HookRegistry( [ transform(HookPhase.PREPARE, "add_prelude", add_prelude), observe(HookPhase.AFTER_STEP_COMMIT, "log_step", log_step), ])
agent = Agent( AgentConfig(name="assistant", description="Helpful assistant"), model=model, hooks=hooks,)Core types
Section titled “Core types”HookPhase: lifecycle phase enumHookCapability:observe_only,transform,decision_supportHookGroup: execution group orderingHookRegistration: one registered phase handlerHookRegistry: ordered collection of registrations
Ordering and failures
Section titled “Ordering and failures”- Execution order is
group -> order -> registration order - Only early phases may be marked
critical=True - Non-critical hook failures are isolated, logged, and recorded as
HookFailedrun-log facts - Critical hook failures are re-raised and fail the run
Public phases
Section titled “Public phases”prepareassemble_contextbefore_llmafter_llmbefore_tool_callafter_tool_callbefore_compactionafter_compactionbefore_retrospectafter_retrospectbefore_terminationafter_terminationafter_step_commitrun_finalizedmemory_persistcompaction_failed
Payload model
Section titled “Payload model”Every handler receives a single payload: dict[str, Any].
- Transform phases may return a partial dict that only changes allowlisted fields
- Decision-support phases may only write the phase-specific
*_advicefield - Observe-only phases should not return anything meaningful
Good uses for hooks
Section titled “Good uses for hooks”- logging and tracing
- message rewrites before LLM calls
- lightweight guardrails
- external memory integration
- debugging committed step flow
Prefer observe(...), transform(...), and decision_support(...) over
constructing HookRegistration directly.