Phase 2: Tool Design & Function Calling · 55 min · Python · asyncio · aiohttp
Async Tool Execution
Sequential tool calls are a free 4x slowdown. asyncio.gather() costs you nothing.
Hiring signal: Async programming is expected at the senior AI engineer level. Production systems at Stripe and Notion routinely parallelize 3–5 tool calls per request. 'How would you optimize an agent that makes multiple API calls?' is a common interview question with async as the expected answer.
What you will learn
- Implement parallel tool execution using Python asyncio
- Handle dependencies between tool calls using a dependency graph
- Design a tool execution plan that maximizes parallelism while respecting dependencies
The Problem
An agent needs to call 4 tools: check inventory, get pricing, look up shipping time, and verify payment method. Each takes ~800ms. Done sequentially: 4 × 800ms = 3.2 seconds. Done in parallel: 800ms. The information is completely independent — there's no reason to wait for inventory before starting pricing.
Async tool execution is a free 4x speedup available to every agent that makes multiple independent API calls. It's something most beginners never think about — and something every production AI engineer does automatically.
Sequential vs. Parallel Tool Execution
Most LLM APIs now support returning multiple tool calls in a single model response. When the model calls check_inventory, get_pricing, and get_shipping_time simultaneously, your job is to execute them concurrently.
Sequential execution:
[tool_1 800ms][tool_2 800ms][tool_3 800ms][tool_4 800ms] = 3200ms total
Parallel execution:
[tool_1 800ms]
[tool_2 800ms] All start at the same time
[tool_3 800ms]
[tool_4 800ms]
= 800ms total (+ ~50ms overhead)
In Python, asyncio.gather() runs multiple coroutines concurrently and collects results when all complete:
import asyncio
results = await asyncio.gather(
check_inventory("PROD-001"),
get_pricing("PROD-001"),
get_shipping_time("US", "CA"),
verify_payment("cus_123"),
)
# All 4 ran concurrently — took ~800ms, not 3200ms
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Dependency Graphs for Tool Execution, Timeout Management, Build It, What to Practice — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy