Docs/SDK/Lifecycle Hooks

Lifecycle Hooks

Observe step boundaries (start/end) to add lightweight instrumentation: logging, metrics, and UI progress updates.

Hooks are meant to be cheap and side-effect free: record what happened, don’t change what happens.

Hooks give you step-level observability with almost zero coupling.

Use hooks to emit metrics, attach logs to your trace pipeline, or update a UI—without modifying the agent loop.

Table of Contents

  1. Hook signatures
  2. When hooks run
  3. Examples
  4. Best practices

Hook signatures

Hooks receive a small context object (step id/index, goal, attempt, URL, and outcome).

FieldMeaning
stepId / step_idStable identifier for the step.
stepIndex / step_index0-based step counter.
goalNatural language goal for the step.
attemptRetry attempt index within the step.
success / outcome / errorPresent on step end (best-effort), describing the step result.

When hooks run

Hooks are invoked even when tracing is disabled, so you can attach your own telemetry.


Examples

def on_start(ctx):
    print("start", ctx.get("goal"), "step", ctx.get("step_index"))

def on_end(ctx):
    print("end", ctx.get("success"), "outcome", ctx.get("outcome"))

result = agent.act(
    "Click Sign In",
    on_step_start=on_start,
    on_step_end=on_end,
)

Best practices