How to create an intelligent multi-agent workflow using the handover feature of the Mistral Agent API

In this tutorial, we will explore how to use the handover capabilities of the Mistral Adments API to create intelligent, multi-agent workflows. This allows different agents to solve complex problems in a modular and efficient way by passing tasks to each other. We will build a system where agents collaborate on answering inflation-related questions (performance calculations, getting data online and creating visualizations) to provide clear, accurate and dynamic responses.
Step 1: Set up the dependency
Install the library
pip install mistralai pydantic
Loading the Mistral API key
You can get an API key from it
from getpass import getpass
MISTRAL_API_KEY = getpass('Enter Mistral API Key: ')
Step 2: Agent Prerequisites and Settings
Initialize the proxy
from mistralai import CompletionArgs, ResponseFormat, JSONSchema
from pydantic import BaseModel
from mistralai import Mistral
client = Mistral(MISTRAL_API_KEY)
Create custom features
The ADPACT_FOR_INFLATION function calculates how much the given amount is worth after taking into account the time of inflation. It uses compound formulas based on the number of years and annual inflation rate. If the end year is before the start year, an error is returned. Otherwise, it returns the adjusted value as well as input details. For example, the Addjud_for_inflation (1000, 1899, 2025, 10) shows the value of 10% inflation in 2025 to Rs 1,000 for 2025.
def adjust_for_inflation(amount: float, start_year: int, end_year: int, annual_inflation_rate: float):
"""
Calculates inflation-adjusted value using compound formula.
"""
if end_year
Create structured output for mathematical reasoning
class CalcResult(BaseModel):
reasoning: str
result: str
inflation_tool = {
"type": "function",
"function": {
"name": "adjust_for_inflation",
"description": "Calculate the value of money adjusted for inflation over a time period.",
"parameters": {
"type": "object",
"properties": {
"amount": {
"type": "number",
"description": "Original amount of money"
},
"start_year": {
"type": "integer",
"description": "The starting year for inflation adjustment"
},
"end_year": {
"type": "integer",
"description": "The ending year for inflation adjustment"
},
"annual_inflation_rate": {
"type": "number",
"description": "Annual inflation rate in percent"
}
},
"required": ["amount", "start_year", "end_year", "annual_inflation_rate"]
}
}
}
Step 3: Create an agent
Define different agents
In this setup, we use the Mistral Adments API to define a multi-proxy system to handle economic queries related to inflation. The main agent (economics agent) is the coordinator who extends the task to special agents. Inflation agents use custom functions to perform inflation-adjusted calculations. If the inflation rate is missing in the query, the WebSearch-Agent gets it from the Internet. Calculator agents process complex numerical calculations by step-by-step reasoning, while graphical agents use code interpreters to visualize inflation trends over time. These agents jointly cooperate through handover to produce accurate and dynamic responses to economic inquiries.
# Main Agent
economics_agent = client.beta.agents.create(
model="mistral-large-latest",
name="economics-agent",
description="Handles economic queries and delegates inflation calculations.",
)
# Inflation Function Agent
inflation_agent = client.beta.agents.create(
model="mistral-large-latest",
name="inflation-agent",
description="Agent that calculates inflation-adjusted value using a custom function.",
tools=[inflation_tool],
)
# Web Search Agent
websearch_agent = client.beta.agents.create(
model="mistral-large-latest",
name="websearch-agent",
description="Agent that can search the internet for missing economic data such as inflation rates.",
tools=[{"type": "web_search"}]
)
# Calculator Agent
from pydantic import BaseModel
class CalcResult(BaseModel):
reasoning: str
result: str
calculator_agent = client.beta.agents.create(
model="mistral-large-latest",
name="calculator-agent",
description="Agent used to make detailed calculations.",
instructions="When doing calculations, explain step by step.",
completion_args=CompletionArgs(
response_format=ResponseFormat(
type="json_schema",
json_schema=JSONSchema(
name="calc_result",
schema=CalcResult.model_json_schema(),
)
)
)
)
# Graph Agent
graph_agent = client.beta.agents.create(
model="mistral-large-latest",
name="graph-agent",
description="Agent that generates graphs using code interpreter.",
instructions="Use code interpreter to draw inflation trends.",
tools=[{"type": "code_interpreter"}]
)
Define handover responsibility
This configuration defines how agents delegate tasks to each other:
- The main agent (Economics_agent) is used as the entry point and delegates an inquiry on inflation (for inflation calculations) or a websearch_agent (to get missing data, such as inflation rate).
- Inflation_agent After receiving user queries or data extracted by the network, the task can be further passed to the Calculator_agent (for detailed math) or graph_agent (for visualizing trends).
- WebSearch_agent can pass control to inflation_AGENT after retrieving the required information, such as inflation rate.
- Calculator_agent and graph_agent are considered terminal agents. However, if subsequent work is required (e.g., plotting the calculation results, and vice versa), optional inter-handling is enabled.
# Main Agent hands off to inflation_agent and websearch_agent
economics_agent = client.beta.agents.update(
agent_id=economics_agent.id,
handoffs=[inflation_agent.id, websearch_agent.id]
)
# Inflation Agent can delegate to calculator_agent or graph_agent if deeper analysis or visualization is needed
inflation_agent = client.beta.agents.update(
agent_id=inflation_agent.id,
handoffs=[calculator_agent.id, graph_agent.id]
)
# Web Search Agent can hand off to inflation_agent (after finding the missing rate)
websearch_agent = client.beta.agents.update(
agent_id=websearch_agent.id,
handoffs=[inflation_agent.id]
)
# Calculator and Graph agents are terminal--they don't hand off further
# But if needed, we could let them hand off to each other:
calculator_agent = client.beta.agents.update(
agent_id=calculator_agent.id,
handoffs=[graph_agent.id] # Optional
)
graph_agent = client.beta.agents.update(
agent_id=graph_agent.id,
handoffs=[calculator_agent.id] # Optional
)
Step 4: Run the Agent
Example Answer: What is the current inflation rate in India?
In this example, the timely “What is India’s current inflation rate?” is passed to the Economics_agent, which is the main entry point for handling economic queries. Since this problem requires real-time data not included in the agent’s static knowledge, the Economics_agent will automatically hand over the query to a WebSearch_agent with WebSearch_agent, which is equipped with WebSearch_agent, which is equipped with WebSearch function.
prompt = "What is the current inflation rate in India?"
response = client.beta.conversations.start(
agent_id=economics_agent.id,
inputs=prompt
)
print(response.outputs[-1].content[0].text)
Example B: What is the value of the 5,000 inflation-adjusted inflation from 2010 to 2023, with an annual inflation rate of 6.5%. Explain the calculation steps and draw a graph with data labels
This code block will send a prompt to the economic agent, checking whether the agent triggers a specific function call (adpad_for_inflation), performs the function locally in the provided parameters, and then returns the calculation result to the agent. Finally, it prints the proxy’s response, which includes inflation calculation instructions, and python code to plot the trend.
import json
from mistralai.models import FunctionResultEntry
prompt = """What is the inflation-adjusted value of 5,000 from the year 2010 to 2023 with annual inflation rate of 6.5%.
Explain calculation steps and plot a graph with data labels"""
response = client.beta.conversations.start(
agent_id=economics_agent.id,
inputs=prompt
)
# Check for function call
if response.outputs[-1].type == "function.call" and response.outputs[-1].name == "adjust_for_inflation":
args = json.loads(response.outputs[-1].arguments)
# Run local function
function_result = json.dumps(adjust_for_inflation(**args))
# Return result to Mistral
result_entry = FunctionResultEntry(
tool_call_id=response.outputs[-1].tool_call_id,
result=function_result
)
response = client.beta.conversations.append(
conversation_id=response.conversation_id,
inputs=[result_entry]
)
print(response.outputs[-1].content)
else:
print(response.outputs[-1].content)
The agent returns the following code block to plot the trend of inflation-adjusted values.
import matplotlib.pyplot as plt
import numpy as np
# Parameters
original_amount = 5000
start_year = 2010
end_year = 2023
inflation_rate = 6.5 / 100 # Convert percentage to decimal
# Calculate the number of years
num_years = end_year - start_year + 1
# Calculate the adjusted value for each year
years = np.arange(start_year, end_year + 1)
adjusted_values = original_amount * (1 + inflation_rate) ** (years - start_year)
# Plot the graph
plt.figure(figsize=(10, 6))
plt.plot(years, adjusted_values, marker="o", linestyle="-", color="b")
# Add data labels
for year, value in zip(years, adjusted_values):
plt.text(year, value, f'${value:.2f}', ha="right")
# Add titles and labels
plt.title('Inflation-Adjusted Value Over Time')
plt.xlabel('Year')
plt.ylabel('Adjusted Value')
# Save the plot as an image
plt.savefig('inflation_adjusted_value.png')
# Show the plot
plt.show()
Check notebook. All credits for this study are to the researchers on the project. Also, please stay tuned for us twitter And don’t forget to join us 98k+ ml reddit And subscribe Our newsletter.

I am a civil engineering graduate in Islamic Islam in Jamia Milia New Delhi (2022) and I am very interested in data science, especially neural networks and their applications in various fields.
