Skip to content

docs: Add a simple lifecycle hooks example for Python #1384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
18 changes: 18 additions & 0 deletions docs/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ agent = Agent[UserContext](

Sometimes, you want to observe the lifecycle of an agent. For example, you may want to log events, or pre-fetch data when certain events occur. You can hook into the agent lifecycle with the `hooks` property. Subclass the [`AgentHooks`][agents.lifecycle.AgentHooks] class, and override the methods you're interested in.

```python
from agents import Agent, AgentHooks, RunContextWrapper
from typing import Any

class CustomAgentHooks(AgentHooks):
async def on_start(self, context: RunContextWrapper, agent: Agent) -> None:
"""Called when the agent starts its run."""
print(f"Agent '{agent.name}' has started.")

async def on_end(self, context: RunContextWrapper, agent: Agent, output: Any) -> None:
"""Called when the agent completes its run."""
print(f"Agent '{agent.name}' has finished. Final output type: {type(output)}")

agent = Agent(
name="Lifecycle Agent",
hooks=CustomAgentHooks(),
)

## Guardrails

Guardrails allow you to run checks/validations on user input, in parallel to the agent running. For example, you could screen the user's input for relevance. Read more in the [guardrails](guardrails.md) documentation.
Expand Down