Skip to content

Commit bb2c1f3

Browse files
committed
feat: add lifecycle hooks to agent
1 parent 18cb55e commit bb2c1f3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/agents/lifecycle.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@ class RunHooks(Generic[TContext]):
1010
override the methods you need.
1111
"""
1212

13+
#Two new hook methods added to the RunHooks class to handle LLM start and end events.
14+
#These methods allow you to perform actions just before and after the LLM call for an agent.
15+
#This is useful for logging, monitoring, or modifying the context before and after the LLM call.
16+
async def on_llm_start(
17+
self,
18+
context: RunContextWrapper[TContext],
19+
agent: Agent[TContext],
20+
system_prompt: str | None,
21+
input_items: List[TResponseInputItem]
22+
) -> None:
23+
"""Called just before invoking the LLM for this agent."""
24+
pass
25+
26+
async def on_llm_end(
27+
self,
28+
context: RunContextWrapper[TContext],
29+
agent: Agent[TContext],
30+
response: ModelResponse
31+
) -> None:
32+
"""Called immediately after the LLM call returns for this agent."""
33+
pass
34+
35+
1336
async def on_agent_start(
1437
self, context: RunContextWrapper[TContext], agent: Agent[TContext]
1538
) -> None:

0 commit comments

Comments
 (0)