Agent lifecycle hooks
Attach RunHooks and AgentHooks to observe and react to agent lifecycle events.From its SKILL.md
npx -y skills add kjuhwa/skills-hub --skill agent-lifecycle-hooksAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
2.3 KB, 450 tokens by cl100k_base, as published. Nobody here has run it
agent-lifecycle-hooks
Implement RunHooks (run-level) or AgentHooks (agent-level) to observe agent start/end, LLM calls, tool calls, and handoffs. Pass hooks to Runner.run(hooks=...) or Agent(hooks=...).
When to apply
Logging, usage tracking, debugging, or triggering side effects (e.g., saving state) at each lifecycle step. Alternative to custom tracing processors for lightweight observation.
Core snippet
from agents import Agent, RunHooks, AgentHooks, RunContextWrapper, AgentHookContext, Tool, Runner
class ExampleHooks(RunHooks):
async def on_agent_start(self, context: AgentHookContext, agent: Agent) -> None:
print(f"Agent {agent.name} started. Input: {context.turn_input}")
async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output) -> None:
print(f"Agent {agent.name} ended with: {output}")
async def on_tool_start(self, context: RunContextWrapper, agent: Agent, tool: Tool) -> None:
print(f"Tool {tool.name} called")
async def on_tool_end(self, context: RunContextWrapper, agent: Agent, tool: Tool, result: str) -> None:
print(f"Tool {tool.name} result: {result}")
async def on_handoff(self, context: RunContextWrapper, from_agent: Agent, to_agent: Agent) -> None:
print(f"Handoff: {from_agent.name} -> {to_agent.name}")
hooks = ExampleHooks()
async def main():
agent = Agent(name="Assistant", instructions="Be helpful.")
result = await Runner.run(agent, "Hello!", hooks=hooks)
Key notes
RunHooksapplies to the entire run;AgentHooksscopes to a single agenton_tool_start/endonly fires for local function tools, not hosted toolscontext.usagetracks cumulative token usage across the runcontext.turn_inputinon_agent_startgives the agent's current input
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.