Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion examples/basic/lifecycle_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,36 @@

from pydantic import BaseModel

from agents import Agent, RunContextWrapper, RunHooks, Runner, Tool, Usage, function_tool
from agents import (
Agent,
AgentHooks,
RunContextWrapper,
RunHooks,
Runner,
Tool,
Usage,
function_tool,
)
from agents.items import ModelResponse, TResponseInputItem


class LoggingHooks(AgentHooks[Any]):
async def on_start(
self,
context: RunContextWrapper[Any],
agent: Agent[Any],
) -> None:
print(f"#### {agent.name} is starting.")

async def on_end(
self,
context: RunContextWrapper[Any],
agent: Agent[Any],
output: Any,
) -> None:
print(f"#### {agent.name} produced output: {output}.")


class ExampleHooks(RunHooks):
def __init__(self):
self.event_counter = 0
Expand Down Expand Up @@ -92,6 +118,7 @@ class FinalResult(BaseModel):
instructions="Multiply the number by 2 and then return the final result.",
tools=[multiply_by_two],
output_type=FinalResult,
hooks=LoggingHooks(),
)

start_agent = Agent(
Expand All @@ -100,6 +127,7 @@ class FinalResult(BaseModel):
tools=[random_number],
output_type=FinalResult,
handoffs=[multiply_agent],
hooks=LoggingHooks(),
)


Expand Down