As your LLM agent becomes more sophisticated, it will often need to use multiple tools to achieve a single, complex goal. The way these tools are executed. whether one after another or several at once. significantly impacts the agent's efficiency and capabilities. This section examines how to implement both sequential and parallel tool use, enabling your agent to tackle more elaborate tasks.
Sequential tool execution is the most straightforward method: tools are run one at a time, in a specific order. This approach is necessary when the output of one tool serves as the input for the next, creating a dependency chain. Think of it like a recipe where each step must be completed before the next can begin.
For example, an agent tasked with "Find the top tech news today, summarize it, and email the summary to me" would likely perform these steps sequentially:
Each tool's successful completion and output are prerequisites for the subsequent tool. The agent, or the logic you define for it, needs to manage this flow, ensuring that data is correctly passed from one tool to the next.
A typical sequential tool execution flow, where the output of one tool feeds into the next.
When to Use Sequential Execution:
Error handling in sequential flows is relatively direct. If a tool fails, the agent can stop the sequence, try an alternative tool if available, or report the failure.
Parallel tool execution involves running multiple tools simultaneously. This approach is highly effective when an agent needs to perform several independent tasks, and the order in which these tasks complete doesn't matter, or when results from multiple sources need to be gathered before proceeding.
Consider an agent asked to "Get the current weather in San Francisco, London, and Tokyo." These are three independent queries. The agent could:
These three calls can be made concurrently. The agent doesn't need to wait for the San Francisco weather report before requesting London's. Once all results are back, the agent can aggregate them.
An illustration of parallel tool execution, where independent tasks are performed concurrently and their results are combined.
When to Use Parallel Execution:
Implementing parallel execution requires more sophisticated management. In Python, asyncio
is well-suited for I/O-bound tasks common in agent tools (like API calls). You'd define your tool functions as asynchronous (using async def
) and use await
for operations that might block. Then, asyncio.gather()
can be used to run multiple awaitable tasks concurrently.
import asyncio
# Assume these are defined elsewhere and are awaitable
# async def call_weather_api(city: str) -> dict: ...
# async def call_news_api(topic: str) -> list: ...
async def fetch_data_in_parallel():
# Example: Fetch weather for two cities and news on a topic
weather_sf_task = call_weather_api("San Francisco")
weather_london_task = call_weather_api("London")
news_tech_task = call_news_api("technology")
# asyncio.gather runs all tasks concurrently
results = await asyncio.gather(
weather_sf_task,
weather_london_task,
news_tech_task,
return_exceptions=True # Important for handling individual task failures
)
# Process results:
# results[0] will be weather for SF or an exception
# results[1] will be weather for London or an exception
# results[2] will be news for technology or an exception
# Further logic to handle and combine results...
# For example, check if isinstance(res, Exception) for each item in results
sf_weather = results[0] if not isinstance(results[0], Exception) else "Error fetching SF weather"
london_weather = results[1] if not isinstance(results[1], Exception) else "Error fetching London weather"
tech_news = results[2] if not isinstance(results[2], Exception) else "Error fetching tech news"
print(f"San Francisco Weather: {sf_weather}")
print(f"London Weather: {london_weather}")
print(f"Tech News: {tech_news}")
# To run this in a script:
# if __name__ == "__main__":
# asyncio.run(fetch_data_in_parallel())
Handling errors in parallel execution requires care. If one tool fails, the agent needs to decide whether other parallel tasks should continue or if the overall operation is compromised. Using return_exceptions=True
with asyncio.gather
allows the agent to get all results or exceptions and handle them individually.
Many complex tasks benefit from a hybrid approach, combining sequential and parallel execution. An agent might perform several data-gathering steps in parallel, then sequentially process and synthesize the collected information.
For instance, an agent planning a trip might:
This structure allows the agent to optimize for speed where possible (parallel data fetching) while maintaining logical order where necessary (analysis and summarization).
A hybrid workflow combining parallel data gathering with sequential processing.
How does an LLM agent decide whether to use tools sequentially or in parallel? This often involves a combination of:
The choice of sequential, parallel, or hybrid execution is a fundamental aspect of tool orchestration. By understanding these patterns and their implementation implications, you can design agents that are not only capable but also efficient in how they utilize their tools to solve problems and complete tasks.
Was this section helpful?
© 2025 ApX Machine Learning