Introduction & Context

Welcome back! You've mastered sequential workflows with prompt chaining and conditional workflows with intelligent routing. Now it's time to unlock dramatic performance improvements by learning parallel processing — executing multiple independent Claude API calls simultaneously instead of waiting for each one to complete.

In this lesson, you'll discover how to transform workflows that take minutes into operations that complete in seconds. You'll learn the difference between synchronous and asynchronous programming, master Python's asyncio library, and build a system that asks multiple questions to Claude at the same time.

The Parallelization Workflow Pattern

Before diving into the technical details, let's understand the high-level pattern we'll be implementing. This workflow has two distinct phases that work together to provide both speed and comprehensive results:

Phase 1: Parallel Research Gathering

  • Launch multiple independent Claude API calls simultaneously
  • Each call researches a different aspect of your topic (attractions, transportation, culture)
  • All questions run concurrently, completing in roughly the time of the slowest individual request
  • Results are collected and preserved in their original order

Phase 2: Sequential Result Synthesis

  • Combine all parallel research into a single comprehensive dataset
  • Send the aggregated information to Claude with instructions for synthesis
  • Generate a unified, actionable final result (like a complete travel guide)
  • This sequential step ensures all information is properly integrated

This two-phase approach maximizes both efficiency and quality: you get the speed benefits of parallel processing for data gathering, while maintaining coherent analysis through sequential aggregation. It's particularly powerful for research tasks, analysis workflows, and any scenario where you need to quickly gather diverse information and synthesize it into actionable insights.

Understanding Sync vs Async Anthropic Clients

When working with the Anthropic API, you can choose between two client types: one for synchronous (step-by-step) operations and one for asynchronous (parallel) operations. The difference between them determines whether your program waits for each Claude response before moving on, or whether it can send multiple requests at once.

With the standard Anthropic client, each API call is synchronous—your code waits for a response before continuing. This is simple, but can be slow if you have many independent tasks.

from anthropic import Anthropic

# Initialize the standard Anthropic client
client = Anthropic()

# Synchronous approach - each call waits for the previous one to finish
response1 = client.messages.create(...)  # Wait for this to complete
response2 = client.messages.create(...)  # Then wait for this to complete

In contrast, the AsyncAnthropic client supports asynchronous operations. This means you can start several Claude API calls at the same time, and your program will continue running while waiting for responses. This is ideal for running many independent tasks in parallel.

from anthropic import AsyncAnthropic

# Initialize the async Anthropic client
client = AsyncAnthropic()

# Asynchronous approach - all calls can run simultaneously (inside an async function)
response1 = await client.messages.create(...)  # Start this
response2 = await client.messages.create(...)  # Start this too

In summary:

  • Use the synchronous client for simple, sequential workflows where each step depends on the previous one.
  • Use the asynchronous client when you want to launch multiple independent Claude API calls at once, dramatically improving performance for batch or parallel tasks.

Choosing the right client type is the key to optimizing your Claude workflows for both simplicity and speed.

AsyncIO Fundamentals for Claude Workflows

Python's asyncio library provides an event loop that manages multiple operations simultaneously, switching between them efficiently rather than blocking on any single operation. The async keyword transforms a regular function into a coroutine that can be paused and resumed, while await pauses execution until an asynchronous operation completes.

import asyncio
from anthropic import AsyncAnthropic

# Initialize the async Anthropic client
client = AsyncAnthropic()

async def your_async_function(question):
    # The 'await' allows other tasks to run while waiting for the API response
    response = await client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=2000,
        messages=[{"role": "user", "content": question}]
    )
    
    return response.content[0].text

This approach is particularly effective for I/O-bound operations like API calls, where much of the time is spent waiting for network responses.

Running Async Code with asyncio.run()

To execute async functions, you need an event loop. asyncio.run() creates an event loop, runs your async function, and cleans up afterward. This is the standard entry point for async programs:

async def main():
    # Call your async function and await the result
    result = await your_async_function("What are the top 3 must-see attractions in Paris?")

# Run the async main function - this creates and manages the event loop
if __name__ == "__main__":
    asyncio.run(main())

This pattern of wrapping your async code in a main() function and calling it with asyncio.run() is the standard approach for async programs. The asyncio.run() function handles all the event loop management automatically, making it the simplest way to execute async code.

Concurrent Execution with asyncio.gather()

The real power of async programming comes from running multiple operations concurrently. asyncio.gather() starts multiple coroutines simultaneously and waits for all of them to complete, returning results in the original order:

async def main():
    # Create coroutine objects (these don't run yet)
    tasks = [
        your_async_function("What are the top attractions in Paris?"),
        your_async_function("How do I get around Paris?"), 
        your_async_function("What are French cultural norms?")
    ]
    
    # Execute all tasks concurrently and wait for all to complete
    # Note: Results are returned in the same order as the original tasks
    results = await asyncio.gather(*tasks)

if __name__ == "__main__":
    asyncio.run(main())

The key insight: while one API call waits for Claude's response, the event loop can initiate or continue processing other API calls. This transforms sequential waiting time into concurrent execution time.

Creating Async Functions for Claude Calls

Now that you understand the fundamentals, let's build the foundation of our parallel workflow by creating an async function specifically designed for Claude API calls. This function will handle individual questions while being optimized for concurrent execution.

from anthropic import AsyncAnthropic

# Initialize the async Anthropic client
client = AsyncAnthropic()

async def ask_question(question):
    """
    Async function to ask Claude a single question
    """
    print(f"🔄 Asking: {question}")
    
    # Send async request to Claude with system prompt
    response = await client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=2000,
        system="You are a travel expert. Give brief, helpful answers.",
        messages=[{"role": "user", "content": question}]
    )
    
    # Extract the answer
    answer = response.content[0].text
    
    print(f"✅ Answered: {question}")
    
    return question, answer

The print statements help visualize when each question starts and completes, while returning a tuple of (question, answer) makes it easy to match responses back to their original questions when processing parallel results. The system prompt ensures consistent, focused responses from Claude.

Preparing the List of Questions

With our async function ready, let's define the independent research questions that will form the parallel component of our workflow. Parallel processing shines when you have independent problems that don't rely on each other's answers:

# List of independent questions for Paris trip planning
questions = [
    "What are the top 3 must-see attractions in Paris?",
    "What is the most efficient way to get around Paris as a tourist?",
    "What are important cultural etiquette tips for visitors to France?"
]

These questions cover different aspects of travel planning (attractions, transportation, culture) and are completely independent of each other, making them perfect candidates for parallel execution.

Building Parallel Task Collections

Now let's put asyncio.gather() to work by creating multiple tasks that execute simultaneously. This is where the parallel magic happens:

async def main():
    """
    Execute parallel research and create comprehensive travel plan
    """
    print(f"Starting {len(questions)} research questions in parallel...")
    
    # Create tasks for all questions
    tasks = [ask_question(question) for question in questions]
    
    # Execute all tasks in parallel
    results = await asyncio.gather(*tasks)
    
    print("\nResearch completed!")

The list comprehension creates coroutine objects representing work to be done, while asyncio.gather(*tasks) starts all coroutines simultaneously and returns results in the original order regardless of completion sequence. Each result is a tuple containing the question and its corresponding answer.

Aggregating Results for Final Analysis

With all our parallel research complete, let's build the aggregation phase that synthesizes everything into a comprehensive result. This sequential step ensures all information is properly integrated:

async def create_travel_plan(results):
    """
    Aggregate all research results into a comprehensive travel plan
    """
    # Combine all Q&A pairs into a formatted string
    combined_research = ""
    for question, answer in results:
        combined_research += f"{question}\n{answer}\n\n"
    
    # Create aggregator prompt
    aggregator_prompt = f"Create a brief Paris travel guide based on this research:\n\n{combined_research}\n\nMake it concise and practical."
    
    # Send to Claude for final synthesis
    response = await client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=4000,
        system="You are a travel planner. Create brief, useful guides.",
        messages=[{"role": "user", "content": aggregator_prompt}]
    )
    
    return response.content[0].text
Running the Complete Parallel Workflow

Let's bring it all together into a complete workflow that demonstrates the full power of parallel processing followed by intelligent aggregation:

async def main():
    """
    Execute parallel research and create comprehensive travel plan
    """
    print(f"Starting {len(questions)} research questions in parallel...")
    
    # Create tasks for all questions
    tasks = [ask_question(question) for question in questions]
    
    # Execute all tasks in parallel
    results = await asyncio.gather(*tasks)
    
    print("\nResearch completed!")
    
    # Aggregate results into final travel plan
    travel_plan = await create_travel_plan(results)
    
    print("\nParis Travel Guide:")
    print(travel_plan)

# Run the parallel workflow
if __name__ == "__main__":
    asyncio.run(main())

When you run this workflow, you'll see the power of parallel execution unfold in three distinct stages:

  1. Instant Launch: All three "🔄 Asking" messages appear immediately as the API calls fire off simultaneously
  2. Concurrent Completion: The "✅ Answered" messages arrive as Claude finishes each response—often in a different order than they were asked, proving your requests are truly running in parallel
  3. Intelligent Synthesis: All this concurrent research gets woven together into a comprehensive travel guide that combines the speed benefits of parallel processing with thoughtful analysis

This visual progression clearly demonstrates how your requests execute concurrently rather than waiting for each other, transforming what could be a slow sequential process into a fast, efficient workflow that delivers both speed and quality.

Starting 3 research questions in parallel...
🔄 Asking: What are the top 3 must-see attractions in Paris?
🔄 Asking: What is the most efficient way to get around Paris as a tourist?
🔄 Asking: What are important cultural etiquette tips for visitors to France?
✅ Answered: What are the top 3 must-see attractions in Paris?
✅ Answered: What is the most efficient way to get around Paris as a tourist?
✅ Answered: What are important cultural etiquette tips for visitors to France?

Research completed!

Paris Travel Guide:
# Brief Paris Travel Guide

## Must-See Attractions
**Top 3 Essentials:**
1. **Eiffel Tower** - Best views at night when illuminated
2. **Louvre Museum** - Home to Mona Lisa; book tickets online
3. **Notre-Dame** - Gothic masterpiece (under restoration but exterior viewable)

*Tip: All three are within walking distance along the Seine - plan 1-2 days*

## Getting Around
**Best Option: Metro + Walking**
- €1.90 per ride or get a day pass
- Trains every 2-7 minutes
- Download Citymapper app
- Avoid rush hours (7:30-9:30am, 5:30-8pm)
- Most attractions within 500m of Metro stations

## Cultural Etiquette Essentials
**Must-Do's:**
- Say "Bonjour" entering shops, "Au revoir" leaving
- Dress nicely (no flip-flops/athletic wear)
- Keep hands visible while dining
- Learn basic French phrases - effort counts!

**Don't:**
- Smile at strangers (considered odd)
- Talk loudly in public
- Expect shops open 12-2pm (lunch break)

## Quick Tips
- Book Louvre tickets online to skip lines
- Bring shopping bags (stores don't provide them)
- Restaurants don't modify dishes - order as-is
- Evening Eiffel Tower visits are most magical

*Bon voyage!*
Performance Benefits and Use Cases

This two-stage approach provides significant performance benefits while maintaining result quality. The parallel research phase completes in roughly the time of the slowest individual question, while the aggregation phase ensures all information is properly synthesized into a usable travel plan.

This pattern works well for any scenario where you need to:

  • Research multiple independent topics quickly
  • Aggregate diverse information into a unified result
  • Balance speed with comprehensive analysis

The performance benefits are most significant when you have many independent research topics or when individual API calls have high latency.

Summary & Practice Preparation

You've mastered parallel processing patterns that transform slow sequential workflows into lightning-fast concurrent operations. The combination of parallel research gathering and sequential result synthesis provides both speed and quality, making it ideal for complex analysis tasks like travel planning, market research, or technical evaluations.

In the upcoming exercises, you'll apply these patterns to real-world scenarios and learn to handle the nuances of concurrent Claude workflows. Remember: use parallel processing for independent research tasks, then aggregate results sequentially for comprehensive final analysis.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal