examples-basic-lifecycle-example.pyThis document provides a technical walkthrough of the examples-basic-lifecycle-example.py script. This script is an excellent example of how to use the agents library to create a multi-agent workflow, complete with custom tools and detailed lifecycle logging.
The script is built around a few key components from the agents library:
Two distinct agents are defined:
start_agent: This is the entry point of our agent workflow. Its instructions are to generate a random number. If the number is even, it should stop. If it's odd, it hands off the execution to the multiply_agent.multiply_agent: This agent's purpose is to take a number, multiply it by two, and return the final result.The agents use two custom tools, defined using the @function_tool decorator:
random_number(max: int) -> int: This function generates a random integer between 0 and the provided max value (inclusive).multiply_by_two(x: int) -> int: This function takes an integer x and returns its value multiplied by two.The script makes extensive use of hooks to provide detailed logging of the agent lifecycle. Two hook classes are defined:
LoggingHooks: This class inherits from AgentHooks and implements the on_start and on_end methods to log the start and end of an agent's turn.ExampleHooks: This class inherits from RunHooks and provides a more detailed view of the entire run. It implements methods to log:
on_agent_start: When an agent starts.on_llm_start and on_llm_end: When the underlying Large Language Model (LLM) is called and when it returns a response.on_tool_start and on_tool_end: When a tool is invoked and when it completes.on_handoff: When one agent hands off control to another.The main function kicks off the process:
Runner with the start_agent.Runner begins execution, and the ExampleHooks start logging the process.Let's trace the execution path based on the provided output:
start_agent begins.random_number tool.random_number tool is executed. In the example output, it generates an odd number (107).start_agent's instructions, decides to hand off to the multiply_agent.multiply_agent starts.multiply_agent.multiply_by_two tool with the number received from the start_agent.multiply_by_two tool is executed, and the result (214) is returned.FinalResult Pydantic model.multiply_agent ends, and the final result is printed.The output file, examples-basic-lifecycle-example.py.txt, provides a clear, step-by-step log of the entire process. Each line, prefixed with ### <event_counter>, corresponds to a specific event in the agent lifecycle, logged by the ExampleHooks. The output shows the agent and LLM interactions, tool calls with their arguments, and the handoff between the start_agent and multiply_agent. The usage statistics (requests, input tokens, output tokens) are also logged at each step.
This example script is a powerful demonstration of the agents library's capabilities. It showcases how to:
By studying this example, developers can gain a solid understanding of how to build robust and observable agent-based applications.