Skip to main content
Explainer Last reviewed:

Agent Lifecycle Hooks

Direct answer

A lifecycle hook is a point in an agent's execution — before it submits a prompt, before it calls a tool, before it compacts its memory — where code can observe what's about to happen and choose to allow it, block it, or change it before it happens. Hooks are how a harness turns "the agent decided to do this" into a moment a human or a policy can actually intervene on, rather than something that's only visible in a log afterward.

Where do hooks sit in the agent lifecycle?

Every agent, regardless of harness, moves through a broadly consistent sequence from instruction to action. Two categories of control — identity/authorization and gateways — sit at the outer edges of that sequence, where the agent crosses into something that exists independent of it. Hooks are what reaches into the middle.

Figure 1 · The agent lifecycle
01 · INTERNAL Design-time & system prompts 02 · HUMAN User input / prompts 03 · INTERNAL Memory & context 04 · INTERNAL Tool calls & their context 05 · BOUNDARY Identity & authorization 06 · INTERNAL Runtime decisions 07 · HUMAN User feedback 08 · BOUNDARY Gateways Internal — inside the agent's own loop Human-mediated — a person is directly in the loop Boundary — where the agent crosses an edge independent of it ● commonly hook-instrumented stage
Identity/authorization and gateways sit at the two outer edges — boundaries that already exist independent of the agent. Hooks are the mechanism that reaches into the four internal stages in between, which is why they're the one category present at nearly every point in the sequence rather than confined to its edges.

What’s the difference between a hook that fires before an action and one that fires after?

This is the single most consequential design choice in how a hook is used. A hook that fires before an action — Claude Code and the Claude Agent SDK call this PreToolUse, the OpenAI Agents SDK’s on_tool_start and tool guardrails work the same way — can still change the outcome: deny it, modify it, or hold it for approval. A hook that fires afterPostToolUse in Claude Code, or an audit log in general — can only record what already happened.

Figure 2 · Before vs. after
Agent decides to call a tool Tool executes Before-action hook outcome can still change After-action hook records only — fixed
Anything that must never happen has to be caught at the before-action checkpoint. By the after-action checkpoint, the only thing left to do is log it.

Anything that must never happen needs to be caught before it executes, not audited after. This sounds obvious stated plainly, but it’s a common gap in practice: a team sets up logging on every tool call, sees that as “coverage,” and only later realizes logging tells you what went wrong, not that it stopped it from going wrong. The credible pattern across every platform that documents this well is to treat the before-action hook as the enforcement point, and the after-action hook as evidence — useful for audit and forensics, but not a control.

Are hooks themselves a security risk?

Yes, in a specific and worth-understanding way: a hook is code, configured by a team, and it inherits whatever trust boundary that configuration sits inside. Two documented cases make this concrete. A vulnerability tracked as CVE-2025-59536 allowed repo-defined MCP configuration to execute shell commands on tool initialization without explicit consent — a trust-boundary gap in when consent was actually enforced relative to when code could run. A related issue, CVE-2026-21852, allowed a malicious repository to redirect API traffic, including leaking API keys, before the trust prompt was even shown. Both were patched, and both are useful less as isolated bugs than as an illustration of the pattern: a hook or a permission check is only as trustworthy as the boundary condition around exactly when it fires relative to when the thing it’s meant to gate can actually run.

The other side of this is explicit rather than accidental: permissive modes that skip hook enforcement entirely are typically documented with a plain warning rather than hidden. Anthropic’s own documentation flags bypassPermissions mode as granting full system access and states it should be used “with extreme caution” — a case where a platform states outright that the safety property depends on the deployment choice a team makes, not on default behavior.

Can a hook be bypassed by prompt injection?

This is the more structural limitation, and it shows up consistently across platforms rather than being specific to any one of them. A hook that fires based on what the model decided to do is only as trustworthy as the context that produced that decision. Independent security research has demonstrated this pattern in more than one setting: a four-stage attack against a multi-agent supervisor architecture extracted system instructions and tool schemas through inter-agent messaging; separate research has described techniques aimed specifically at circumventing human-approval checkpoints, not just at unauthorized data access. One major agent vendor has stated directly, in its own published research, that prompt injection against browser and computer-use agents may not be fully solvable — publishing a concrete internal case where a malicious email with hidden instructions caused an agent asked to draft an out-of-office reply to instead send a resignation letter to the user’s CEO.

None of this is a reason to skip hooks — every documented case above was mitigated once the right control was enabled, and a hook the model can be talked out of triggering is still a meaningfully stronger guarantee than no interception point at all. It’s a reason to treat a hook as one layer, not the whole answer: a control living entirely inside the model’s own context carries a different kind of risk than one enforced independent of the model’s own reasoning, and a mature posture typically draws on both.

How do hooks compare across major platforms?

The specific mechanisms vary in name and maturity, but the shape is consistent: every major harness has built real, documented interception points, and in every case they’re opt-in per tool or per action rather than a default posture of reviewing everything before it happens.

  • Anthropic documents 31 hook events spanning session lifecycle, per-turn processing, tool execution, subagents, and context compaction, with PreToolUse and the canUseTool callback evaluated first in the permission pipeline — ahead of static allow/deny rules.
  • OpenAI’s Agents SDK fires RunHooks/AgentHooks around every model call, tool call, and agent handoff, with a documented approval model that pauses and serializes run state pending human review.
  • Amazon Bedrock’s closest equivalent is Return of Control: an action group can be configured to return a proposed function and its parameters to the calling application instead of auto-executing — a genuine pre-execution interception point, but opt-in per action group rather than default.
  • Microsoft exposes three named custom triggers in Copilot Studio and per-tool approval gating in Azure AI Foundry, each requiring deliberate, per-topic or per-tool configuration rather than a global checkpoint.

Common questions

What's the difference between a hook and a gateway?

A gateway sits at the network or API boundary, intercepting traffic as it leaves the agent's environment toward a tool or the outside world. A hook sits inside the agent's own execution loop, firing on an internal event — a prompt being submitted, a tool being called, memory being compacted — regardless of whether that event ever produces outbound traffic. A gateway can stop a call to a tool that shouldn't be reachable at all; a hook can stop, modify, or redirect a specific call to a tool the agent is otherwise allowed to use. Most mature postures use both, because each covers ground the other doesn't reach.

Do hooks replace identity and access controls?

No. Identity and authorization define what an agent is allowed to touch at all, under whose credentials; hooks operate inside that boundary, deciding whether a specific action the agent is otherwise authorized to take should actually proceed right now. An agent with well-scoped credentials and no hooks can still take an unwanted action within its own scope. An agent with extensive hooks and poorly scoped credentials can still be a large blast radius if a hook fails or is bypassed. They cover different failure modes.

Which lifecycle stage should get a hook first?

The tool-call stage, specifically the moment immediately before execution. It's the point every platform above treats as its primary enforcement location — Anthropic's PreToolUse, Bedrock's Return of Control, OpenAI's needsApproval — because it's where an agent's decision turns into an action with a real-world effect. Logging or reviewing after the fact has a role, but for anything that must never happen, the pre-execution tool-call hook is the one control point that's consistently treated as non-optional across every platform that documents this well.

Keep reading

More from the Knowledge Center

Explainer Observability & Detection

Agent Harnesses

What actually turns a model into an agent — and why security controls have to target that layer, not the model itself.

Comparison Protocols & Interoperability

MCP vs Skills

MCP servers and agent skills solve different problems and are often confused as competing standards. Here's the actual architectural difference, and the security distinction most comparisons miss.