Introduction
In our last lesson, we built the core mechanism for an agent to act upon the world: function calling. You learned how to define tool schemas, how the LLM generates a structured tool-call request, and how your application can parse this request, execute the corresponding function, and feed the result back to the model. This single-call-and-response loop is the foundational unit of action for any agent.
This lesson builds directly on that foundation to unlock a significant leap in efficiency. Modern LLMs are capable of generating not just one, but multiple tool-call requests in a single response. This capability, often called parallel function calling, allows an agent to perform several independent actions at once. We will explore how to process and dispatch these multiple calls, leveraging your background in asynchronous programming to build a non-blocking, concurrent tool executor. This moves us from a simple request-response pattern to a more sophisticated fan-out/fan-in orchestration, a core pattern in building performant systems.
The "Why": Efficiency Through Parallelism
Imagine a user asks, "What is the stock price for Apple, and what's the weather in Paris?"
With the single-call loop from our previous lesson, an agent would have to handle this in a sequence of turns:
- Turn 1: The LLM requests
get_stock_price(ticker="AAPL"). - Your application executes it and returns the price.
- Turn 2: The LLM, now knowing the stock price, requests
get_weather(location="Paris"). - Your application executes it and returns the weather.
- Turn 3: The LLM synthesizes both results into a final answer.
This sequential process is slow and costly. It requires multiple round-trips to the LLM, and the total latency is the sum of all latencies (two LLM inferences + two tool executions).
Parallel function calling streamlines this dramatically. The LLM can recognize that the two parts of the query are independent and generate both tool calls simultaneously.
This approach has clear benefits that are central to engineering production-grade systems:
- Reduced Latency: The execution time for the tools is now determined by the longest-running tool, not the sum of all tools.
- Lower Cost: It consolidates multiple LLM reasoning steps into one, reducing the number of inference calls and overall token usage.
The performance gains are significant, especially as the number of independent tasks grows.
LLM Function Calling: Complete Implementation Guide (2026)
This article section provides a concise overview of parallel function calling and quantifies its performance impact.
Read the section titled Parallel Function Calling. The table comparing sequential and parallel latency starkly illustrates the efficiency gains. This is the core engineering motivation for implementing this pattern.
The "How": Anatomy of a Multi-Tool-Call Response
When an LLM decides to issue multiple calls, the structure of its response is a natural extension of the single-call case. Instead of a single tool_calls object, the message from the assistant will contain a list of these objects.
The video below provides a clear demonstration of how a more complex user query leads to the generation of multiple, distinct tool calls in the LLM's response.
LLM Function Calling - AI Tools Deep Dive
This segment from a deep dive by Adam Lucek shows how a single prompt asking for multiple, unrelated pieces of information results in an LLM response containing a list of three distinct tool calls.
Watch the section from this timestamp. Observe the user prompt, which combines requests for two exchange rates and a news search. Then, examine the LLM's response. Notice that tool_calls is now a list, with each element being a complete tool call object containing a unique id, the function.name, and the function.arguments.
Each tool call in the list is a self-contained request, and crucially, each has a unique tool_call_id. This ID is essential. After you execute all the functions, you must use this ID to associate each result with its original request when you report the outcomes back to the model.
Implementation: From Sequential to Concurrent Dispatch
Now, let's focus on the implementation. How do we process this list of tool calls?
A. The Naive Sequential Approach
The simplest way to handle a list of tool calls is to iterate through it and execute each one in sequence. This is a direct extension of the code from our previous lesson.
The following article demonstrates this approach. While it successfully executes all the requested tools, it fails to capitalize on the performance benefits of parallelism.
Parallel Function Calling in OpenAI using ChatGPT
This article walks through a complete implementation of handling multiple tool calls. The code in "Step 6" provides a clear example of a sequential dispatch loop.
Focus on the code block in Step 6. Trace the logic: the code receives the response_message, checks if tool_calls exists, and then enters a for tool_call in tool_calls: loop. Inside the loop, it executes one function, appends its result to the message history, and only then moves to the next tool_call. This is functionally correct but serially executes I/O-bound operations.
This sequential loop is a bottleneck. If one API call takes 2 seconds and another takes 3 seconds, the total tool execution time is 5 seconds, plus the overhead of the loop itself. Your experience with async programming immediately flags this as a place for optimization.
B. The Performant Concurrent Approach
To properly implement parallel function calling, we must dispatch these calls concurrently. Since most tool calls involve I/O (network requests to APIs, database queries, file system access), Python's asyncio is the perfect instrument for this task. The goal is to start all the tool-call tasks and then wait for them all to complete, a classic fan-out/fan-in pattern.
Let's design an asynchronous executor. The process will be:
- Iterate through the list of
tool_callsfrom the LLM's response. - For each call, create a coroutine that will execute the corresponding function.
asyncio.create_task()is ideal for this, as it schedules the coroutine to run on the event loop. - Store these tasks in a list.
- Use
awaitto wait for all the tasks to complete, gathering their results. - Format each result into the required
toolmessage structure, making sure to include the correcttool_call_id.
The article we looked at earlier contains an excellent, concise implementation of exactly this pattern.
LLM Function Calling: Complete Implementation Guide (2026)
This section provides a clean, production-oriented Python snippet for handling parallel calls using asyncio.
Study the code block under the heading Handling Parallel Calls. This execute_tools_parallel function is a model implementation. It shows how to create a list of tasks, await their completion, and then package the results correctly. This is the core logic you'll integrate into your agent's execution loop.
By adopting this asyncio-based approach, the 5-second execution time from the previous example (2s + 3s) is reduced to just over 3 seconds (the duration of the longest task).
Completing the Loop: Reporting Results
After your concurrent executor has gathered all the results, the final step is to report them back to the LLM. You must construct a list of tool messages and append them to the conversation history before making the next API call.
Each message in this list must contain:
role: "tool"content: The output of the function call (typically a JSON string).tool_call_id: The unique ID from the original tool call request.
The following video segment walks through this entire multi-step process, from receiving the tool calls to executing them and appending the formatted results for the final synthesis.
LLM Function Calling - AI Tools Deep Dive
This final segment demonstrates the full end-to-end flow for handling multiple tool calls, emphasizing the structure of the messages sent back to the model.
Watch from this point. The narrator explains how to parse the tool calls, execute the functions, and crucially, how to format the tool message with the tool_call_id and content. Pay close attention to the structure of the messages list before the second LLM call is made. It contains the user's prompt, the assistant's first response (with the tool call requests), and now, the list of all the tool outputs. This complete context allows the LLM to generate a comprehensive final answer.
A final point: what if some tasks are dependent on others? For example, "Find the ID for the latest user, then fetch their profile." Modern models like GPT-4o and Claude 3.5 Sonnet are generally smart enough to handle this. They will not emit dependent calls in a single parallel block. Instead, they will call the first tool, wait for your application to provide the result, and then, in the next turn, issue the second tool call using the information from the first. For advanced cases, some APIs also provide a parallel_tool_calls=False flag to explicitly force sequential execution if your workflow demands it.
Conclusion
In this lesson, we have leveled up our agent's execution capabilities from a simple sequential process to a powerful, concurrent one. By understanding and implementing parallel function calling, you've unlocked significant performance improvements that are critical for production-grade applications.
Key takeaways include:
- Parallelism for Efficiency: LLMs can return a list of independent tool calls in a single turn, enabling concurrent execution that drastically reduces latency and cost.
- Async is Key: The most effective way to handle these calls is with asynchronous programming, using patterns like
asyncio.gatherto manage a fan-out/fan-in of I/O-bound tasks. - The ID is the Link: The
tool_call_idis the critical piece of information that allows you to associate each tool's result with its original request when reporting back to the LLM. - The Loop Structure: The full loop involves receiving a list of calls, dispatching them concurrently, gathering the results, formatting them into a list of
toolmessages, and sending everything back for final synthesis by the model.
You are now able to build an agent that can reason about and execute multiple tasks in parallel. In our next lesson, we will shift our focus to the quality of the tools themselves. We will explore how to design clear tool descriptions and parameter schemas to minimize LLM invocation errors, ensuring that as your agent's capabilities grow, its reliability remains high.