Skip to content

Add global on_run_start and on_run_end lifecycle hooks to RunHooksBase #2016

@dannyhorcajo

Description

@dannyhorcajo

Please read this first

  • Have you read the docs? Agents SDK docs
  • Have you searched for related issues? Others may have had similar requests

Describe the feature

Feature

Add two optional async methods to RunHooksBase:

async def on_run_start(
    context: RunContextWrapper[TContext]
) -> None: 
    ...
async def on_run_end(
    context: RunContextWrapper[TContext],
    result: Optional[Any],
) -> None:
    ...

Called exactly once per run (before first agent starts; after final agent finishes).

Why

Current hooks are per-agent or per-tool. Missing global boundaries makes it hard to:

  • Start/end a root trace span
  • Capture total run latency and aggregated metrics
  • Initialize and tear down shared resources
  • Emit a single audit/compliance record

Although it is technically possible to simulate run-level start/end by calling custom pre/post functions outside Runner.run(), that splits lifecycle logic across two places: global boundaries outside the runner and per‑agent/tool events inside hooks.

First‑class on_run_start/ on_run_end centralizes all lifecycle concerns in one hooks class, and keeps instrumentation symmetrical.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-more-infoWaiting for a reply/more info from the authorquestionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions