Speeding Up the Agent › Parallel Tool Calls

Michael BrenndoerferAugust 24, 20252 min read
AI Agent HandbookAll chapters
Part 15 of 15Performance and Cost Optimization - Doing More with Less
Chapter 42Speeding Up the Agent › Parallel Tool Calls
Reading section 8 of 15 · Parallel Tool Calls · 2 minSection 8 of 15 · 2 min

Book contents

Section 8 of 15 · 2 min read

Speeding Up the Agent › Parallel Tool CallsOne section · Sections in this chapter8 / 15 ↓

If your agent needs to use multiple tools, doing them sequentially wastes time. Run them in parallel when possible.

Example: Sequential vs Parallel Tool Execution (Claude Sonnet 4.5)

In[8]:
Code
import time
import asyncio
import os
from anthropic import Anthropic

## Simulated tool functions
def get_weather(city):
    """Simulate weather API call."""
    time.sleep(1)  # Simulate network delay
    return f"Weather in {city}: Sunny, 72°F"

def get_news(topic):
    """Simulate news API call."""
    time.sleep(1)  # Simulate network delay
    return f"Latest news on {topic}: [News headlines...]"

def get_stock_price(symbol):
    """Simulate stock API call."""
    time.sleep(1)  # Simulate network delay
    return f"Stock price for {symbol}: $150.25"

## Sequential execution (slow)
def sequential_tools():
    """Call tools one after another."""
    start = time.time()
    
    weather = get_weather("San Francisco")
    news = get_news("technology")
    stock = get_stock_price("AAPL")
    
    elapsed = time.time() - start
    print(f"Sequential execution: {elapsed:.2f}s")
    return weather, news, stock

## Parallel execution (fast)
async def parallel_tools():
    """Call tools simultaneously."""
    start = time.time()
    
    # Run all tools in parallel
    weather_task = asyncio.to_thread(get_weather, "San Francisco")
    news_task = asyncio.to_thread(get_news, "technology")
    stock_task = asyncio.to_thread(get_stock_price, "AAPL")
    
    # Wait for all to complete
    weather, news, stock = await asyncio.gather(
        weather_task, news_task, stock_task
    )
    
    elapsed = time.time() - start
    print(f"Parallel execution: {elapsed:.2f}s")
    return weather, news, stock

## Compare the approaches
print("Testing sequential execution:")
sequential_tools()

print("\nTesting parallel execution:")
await parallel_tools()
Out[8]:
Console
Testing sequential execution:
Sequential execution: 3.01s

Testing parallel execution:
Parallel execution: 1.01s
('Weather in San Francisco: Sunny, 72°F',
 'Latest news on technology: [News headlines...]',
 'Stock price for AAPL: $150.25')

Output:

Testing sequential execution:
Sequential execution: 3.01s

Testing parallel execution:
Parallel execution: 1.01s

The parallel version is three times faster because all three tools run simultaneously. For an agent that frequently uses multiple tools, this can dramatically improve response time.