Separating Setup and Run
Learn about handling the agent setup and run in production.
Why separate setup
and run
invocation
In the Step 2 of the Quickstart example, the agent.setup()
and agent.run()
are written in the same script and executed in the same process. This method is good enough for a quick "Hello World".
However, in production, you might want to embed these invocations in separate flows.
The agent.setup()
method needs to be invoked only when there are updates to the instructions or tools.
For example, you might trigger agent.setup()
as part of your CI/CD pipeline, or every time your customer
updates an agent’s instructions or tools in cases where your platform allows customer-configured agents.
On the other hand, agent.run()
needs to run when you receive requests to be handled by the agent. For example, you might trigger the run as part of a web application or a Slack chatbot or other application request handling flows for the end users. Such flows are typically not your agent configuration flow.
Separate invocation while reuse code
With ADK, you can invoke agent.setup()
and agent.run()
separately in their appropriate context or flows,
while using the same agent definition. Example:
agent_def.py
# Shared agent definition component using ADK
# To be imported and used by agent_setup.py and agent_run.py
@tool
def get_weather(location: str) -> Dict[str, str]:
"""Get the weather for a given location"""
return {"location": location, "temperature": 72, "unit": "F"}
client = AgentClient(
auth_type="api_key",
profile="DEFAULT",
region="us-chicago-1",
)
weather_agent = Agent(
client=client,
agent_endpoint_id="YOUR_AGENT_ENDPOINT_ID",
instructions="You perform weather queries using tools.",
tools=[get_weather]
)
agent_setup.py
# The setup code
# Invoke when agent definition changes
from agent_def import weather_agent
weather_agent.setup()
agent_run.py
# The run code
# Invoke when there's a request to handle
from agent_def import weather_agent
input = "Is it cold in Seattle?"
response = agent.run(input)
response.pretty_print()