Asynchronous execution allows you to run your agents or tools asynchronously in separate threads. This can be useful for shortening response times for certain I/O-bound tasks.
To run each agent in a separate thread, you need to use a special SendMessageAsyncThreading tool class. See Custom Communicaiton Flows for more information.
Copy
Ask AI
from agency_swarm import SendMessageAsyncThreadingfrom agency_swarm import Agencyagency = Agency(agents=[ceo], send_message_tool_class=SendMessageAsyncThreading)
With this mode, the caller agent does not receive an immediate reply. Instead, it first gets a system notification confirming that the message has been sent to the recipient agent. Later, the calling agent can retrieve the actual response from the recipient.
To run each tool in a separate thread, you need to adjust the ToolConfig class for each tool that you want to run asynchronously. See Custom Tools Configuration for more information.
Copy
Ask AI
from agency_swarm import BaseToolclass Tool(BaseTool): # ... class ToolConfig: async_mode = "threading" # ...
With this mode, the agent will still have to wait for the tool to finish before it can continue with the next step in the conversation. So, it only makes sense to use this mode with multiple tools for the same agent that are not dependent on each other.