Notebooks
M
Mistral AI
Parallel Subtask Agentic Workflow

Parallel Subtask Agentic Workflow

agentsnon_frameworkmistral-cookbookagentic_workflowsmistral

Parallel Subtask Agent Workflow

Open In Colab

Introduction

This notebook demonstrates how to create a generic agent workflow that automatically breaks complex tasks into multiple subtasks.

These subtasks are completed using parallel MistralAI LLM calls, enhanced with real-time information from Tavily API.

The results are then synthesized into a comprehensive response.

Workflow Overview

  1. An orchestrator LLM analyzes the main task and breaks it into distinct, parallel subtasks
  2. Each subtask is assigned to a worker LLM with specialized instructions
  3. Workers execute in parallel, using Tavily API for up-to-date information as needed
  4. Results are synthesized into a unified response

NOTE: We will use MistralAI’s LLM for subtask handling and response synthesis, and the Tavily API to retrieve up-to-date real.

Solution Architecture

solution architecture

Installation

[ ]
Requirement already satisfied: mistralai in /usr/local/lib/python3.11/dist-packages (1.5.0)
Collecting mistralai
  Using cached mistralai-1.7.0-py3-none-any.whl.metadata (30 kB)
Requirement already satisfied: eval-type-backport>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from mistralai) (0.2.2)
Requirement already satisfied: httpx>=0.28.1 in /usr/local/lib/python3.11/dist-packages (from mistralai) (0.28.1)
Requirement already satisfied: pydantic>=2.10.3 in /usr/local/lib/python3.11/dist-packages (from mistralai) (2.11.3)
Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from mistralai) (2.8.2)
Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from mistralai) (0.4.0)
Requirement already satisfied: anyio in /usr/local/lib/python3.11/dist-packages (from httpx>=0.28.1->mistralai) (4.9.0)
Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx>=0.28.1->mistralai) (2025.1.31)
Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx>=0.28.1->mistralai) (1.0.8)
Requirement already satisfied: idna in /usr/local/lib/python3.11/dist-packages (from httpx>=0.28.1->mistralai) (3.10)
Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx>=0.28.1->mistralai) (0.14.0)
Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.10.3->mistralai) (0.7.0)
Requirement already satisfied: pydantic-core==2.33.1 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.10.3->mistralai) (2.33.1)
Requirement already satisfied: typing-extensions>=4.12.2 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.10.3->mistralai) (4.13.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->mistralai) (1.17.0)
Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.11/dist-packages (from anyio->httpx>=0.28.1->mistralai) (1.3.1)
Using cached mistralai-1.7.0-py3-none-any.whl (301 kB)
Installing collected packages: mistralai
  Attempting uninstall: mistralai
    Found existing installation: mistralai 1.5.0
    Uninstalling mistralai-1.5.0:
      Successfully uninstalled mistralai-1.5.0
Successfully installed mistralai-1.7.0

Imports

[ ]

Set your API keys

Here we set the API keys for MistralAI and Tavily. You can obtain the keys from the following links:

  1. MistralAI: https://console.mistral.ai/api-keys
  2. Tavily: https://app.tavily.com/home
[ ]

Initialize Mistral client

[ ]

Tavily API configuration

[ ]

Pydantic Models for Structured Data

Pydantic models provide data validation and serialization, ensuring the data we receive from LLMs matches our expected structure. This helps maintain consistency between the orchestrator and worker components.

SubTask: Individual subtask definition - defines a discrete unit of work with its type, description, and optional search query.

TaskList: Output structure from the orchestrator - contains analysis and a list of defined subtasks to be executed in parallel.

[ ]

API Utility Functions

API Utility functions handle communication with external APIs and process the responses, providing clean interfaces for the rest of the workflow.

fetch_information: Retrieves relevant information from Tavily API based on a query and returns structured results.

run_mistral_llm: Executes a standard call to Mistral AI with given prompts, returning the generated content.

parse_structured_output: Uses Mistral's structured output capability to generate and parse responses according to Pydantic models.

[ ]

Async Worker Functions

These functions enable parallel execution of subtasks, allowing the workflow to process multiple components simultaneously for greater efficiency.

run_task_async: Executes a single subtask asynchronously, enhancing it with relevant information from Tavily when needed.

execute_tasks_in_parallel: Manages the parallel execution of all subtasks, ensuring they run concurrently and their results are properly collected.

[ ]

Main Workflow Function

The primary orchestration function that coordinates the entire parallel subtask process from initial request to final synthesized response.

parallel_subtask_workflow: Manages the complete workflow by orchestrating task decomposition, parallel execution of subtasks, and final synthesis of results into a comprehensive response.

Steps:

  1. Task Analysis: The orchestrator analyzes the user's query and breaks it into distinct subtasks

  2. Subtask Definition: Each subtask is defined with a unique ID, type, description, and search query

  3. Parallel Execution: Subtasks are executed concurrently by worker agents Information Enhancement: Workers retrieve relevant information from Tavily when needed

  4. Result Collection: Outputs from all workers are gathered

  5. Synthesis: Individual results are combined into a comprehensive final response

  6. Final Response: Complete workflow results are returned, including both individual analyses and the synthesized answer

[ ]

Run workflow with an example task

Here we run the worklow with a sample example task comparing mobile phones recommendation

[ ]
[ ]
=== USER TASK ===

Compare the iPhone 16 Pro, iPhone 15 Pro, and Google Pixel 9 Pro, and recommend which one I should purchase.

Orchestrating task decomposition...

=== ORCHESTRATOR OUTPUT ===

ANALYSIS:
To effectively compare the iPhone 16 Pro, iPhone 15 Pro, and Google Pixel 9 Pro, and recommend the best purchase, we need to break down the task into specialized subtasks that cover different aspects of the comparison. These aspects include performance, camera quality, user reviews, and pricing. Each subtask will focus on gathering and analyzing specific information to provide a comprehensive comparison.

SUBTASKS:
- TASK001: Performance Analysis - Compare the performance specifications of the iPhone 16 Pro, iPhone 15 Pro, and Google Pixel 9 Pro, ...
- TASK002: Camera Quality Assessment - Evaluate the camera specifications and capabilities of the iPhone 16 Pro, iPhone 15 Pro, and Google ...
- TASK003: User Reviews and Ratings - Gather and analyze user reviews and ratings for the iPhone 16 Pro, iPhone 15 Pro, and Google Pixel 9...
- TASK004: Pricing and Value Analysis - Compare the pricing of the iPhone 16 Pro, iPhone 15 Pro, and Google Pixel 9 Pro, and assess the valu...

Executing subtasks in parallel...
Fetching information for: iPhone 16 Pro vs iPhone 15 Pro vs Google Pixel 9 Pro performance comparison
Fetching information for: iPhone 16 Pro vs iPhone 15 Pro vs Google Pixel 9 Pro camera comparison
Fetching information for: iPhone 16 Pro vs iPhone 15 Pro vs Google Pixel 9 Pro user reviews
Fetching information for: iPhone 16 Pro vs iPhone 15 Pro vs Google Pixel 9 Pro pricing comparison

=== WORKER RESULT (Performance Analysis) ===
### Performance Analysis of iPhone 16 Pro, iPhone 15 Pro, and Google Pixel 9 Pro

#### Processor
- **iPhone 16 Pro**: Powered by the Apple A18 Pro chipset, which offers up to 15% faster CPU performanc...


=== WORKER RESULT (Camera Quality Assessment) ===
### Camera Quality Assessment for iPhone 16 Pro, iPhone 15 Pro, and Google Pixel 9 Pro

#### iPhone 16 Pro
- **Main Camera:** 48MP stacked sensor
- **Ultrawide Camera:** 48MP
- **Telephoto Camera:** 1...


=== WORKER RESULT (User Reviews and Ratings) ===
To gather and analyze user reviews and ratings for the iPhone 16 Pro, iPhone 15 Pro, and Google Pixel 9 Pro, I will focus on reputable sources such as tech review websites, consumer review platforms, ...


=== WORKER RESULT (Pricing and Value Analysis) ===
### Pricing and Value Analysis

#### Pricing Comparison

1. **Base Model (128GB):**
   - **iPhone 16 Pro:** $999
   - **iPhone 15 Pro:** $999
   - **Google Pixel 9 Pro:** $999

2. **256GB Model:**
   ...


Synthesizing final response...

Final Response

[ ]

=== FINAL SYNTHESIZED RESPONSE ===

Examining Orchestrator Analysis, Subtask information and responses

We can examine the Orchestrator Analysis, subtasks created, the corresponding search queries, and the individual responses.

[ ]

=== Orchestrator Analysis ===

[ ]

=== SUBTASKS CREATED ===

[ ]

=== SUBTASKS RESULTS ===

[ ]

=== FINAL SYNTHESIZED RESPONSE ===