Skip to content

Commit 42422fb

Browse files
committed
feat: add lifecycle hooks to agent
1 parent 857c70e commit 42422fb

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/agents/lifecycle.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
1-
from typing import Any, Generic
1+
from typing import Any, Generic, List
22

33
from typing_extensions import TypeVar
44

55
from .agent import Agent, AgentBase
66
from .run_context import RunContextWrapper, TContext
77
from .tool import Tool
8+
from .items import TResponseInputItem, ModelResponse
89

9-
TAgent = TypeVar("TAgent", bound=AgentBase, default=AgentBase)
1010

11+
TAgent = TypeVar("TAgent", bound=AgentBase, default=AgentBase)
1112

1213
class RunHooksBase(Generic[TContext, TAgent]):
1314
"""A class that receives callbacks on various lifecycle events in an agent run. Subclass and
1415
override the methods you need.
1516
"""
1617

17-
async def on_agent_start(self, context: RunContextWrapper[TContext], agent: TAgent) -> None:
18+
async def on_llm_start(
19+
self,
20+
context: RunContextWrapper[TContext],
21+
agent: TAgent,
22+
system_prompt: str | None,
23+
input_items: List[TResponseInputItem]
24+
) -> None:
25+
"""Called just before invoking the LLM for this agent."""
26+
pass
27+
28+
async def on_llm_end(
29+
self,
30+
context: RunContextWrapper[TContext],
31+
agent: TAgent,
32+
response: ModelResponse
33+
) -> None:
34+
"""Called immediately after the LLM call returns for this agent."""
35+
pass
36+
37+
38+
39+
40+
async def on_agent_start(
41+
self, context: RunContextWrapper[TContext], agent: TAgent
42+
) -> None:
1843
"""Called before the agent is invoked. Called each time the current agent changes."""
1944
pass
2045

0 commit comments

Comments
 (0)