Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      AI and its impact on the developer experience, or ‘where is the joy?’

      July 23, 2025

      Google launches OSS Rebuild tool to improve trust in open source packages

      July 23, 2025

      AI-enabled software development: Risk of skill erosion or catalyst for growth?

      July 23, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Power bank slapped with a recall? Stop using it now – here’s why

      July 23, 2025

      I recommend these budget earbuds over pricier Bose and Sony models – here’s why

      July 23, 2025

      Microsoft’s big AI update for Windows 11 is here – what’s new

      July 23, 2025

      Slow internet speed on Linux? This 30-second fix makes all the difference

      July 23, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Singleton and Scoped Container Attributes in Laravel 12.21

      July 23, 2025
      Recent

      Singleton and Scoped Container Attributes in Laravel 12.21

      July 23, 2025

      wulfheart/laravel-actions-ide-helper

      July 23, 2025

      lanos/laravel-cashier-stripe-connect

      July 23, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      ‘Wuchang: Fallen Feathers’ came close to fully breaking me multiple times — a soulslike as brutal and as beautiful as it gets

      July 23, 2025
      Recent

      ‘Wuchang: Fallen Feathers’ came close to fully breaking me multiple times — a soulslike as brutal and as beautiful as it gets

      July 23, 2025

      Sam Altman is “terrified” of voice ID fraudsters embracing AI — and threats of US bioweapon attacks keep him up at night

      July 23, 2025

      NVIDIA boasts a staggering $111 million in market value per employee — since it became the world’s first $4 trillion company

      July 23, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»How to Create Smart Multi-Agent Workflows Using the Mistral Agents API’s Handoffs Feature

    How to Create Smart Multi-Agent Workflows Using the Mistral Agents API’s Handoffs Feature

    June 9, 2025

    In this tutorial, we’ll explore how to create smart, multi-agent workflows using the Mistral Agents API’s Handoffs feature. This lets different agents work together by passing tasks to each other, enabling complex problems to be solved in a modular and efficient way. We’ll build a system where agents collaborate to answer inflation-related questions—performing calculations, fetching data online, and creating visualizations—to deliver clear, accurate, and dynamic responses.

    Step 1: Setting up dependencies

    Installing the libraries

    Copy CodeCopiedUse a different Browser
    pip install mistralai pydantic

    Loading the Mistral API Key

    You can get an API key from https://console.mistral.ai/api-keys

    Copy CodeCopiedUse a different Browser
    from getpass import getpass
    MISTRAL_API_KEY = getpass('Enter Mistral API Key: ')

    Step 2: Agent Prerequisites and Setup

    Initializing the Agent

    Copy CodeCopiedUse a different Browser
    from mistralai import CompletionArgs, ResponseFormat, JSONSchema
    from pydantic import BaseModel
    from mistralai import Mistral
    
    client = Mistral(MISTRAL_API_KEY)

    Creating the Custom Function

    The adjust_for_inflation function calculates how much a given amount of money would be worth after accounting for inflation over time. It uses the compound formula based on the number of years and the annual inflation rate. If the end year is before the start year, it returns an error. Otherwise, it returns the adjusted value along with the input details. For example, adjust_for_inflation(1000, 1899, 2025, 10) shows what ₹1000 from 1899 would be worth in 2025 at 10% inflation.

    Copy CodeCopiedUse a different Browser
    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 < start_year:
            return {"error": "End year must be greater than or equal to start year."}
    
        years = end_year - start_year
        adjusted_value = amount * ((1 + annual_inflation_rate / 100) ** years)
    
        return {
            "original_amount": amount,
            "start_year": start_year,
            "end_year": end_year,
            "inflation_rate": annual_inflation_rate,
            "adjusted_value": round(adjusted_value, 2)
        }
    
    adjust_for_inflation(1000, 1899, 2025, 10)

    Creating Structured Output for Mathematical Reasoning

    Copy CodeCopiedUse a different Browser
    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: Creating the Agents

    Defining the different agents

    In this setup, we define a multi-agent system using Mistral Agents API to handle inflation-related economic queries. The main agent (economics-agent) acts as a coordinator that routes tasks to specialized agents. The inflation-agent performs inflation adjustment calculations using a custom function. If the inflation rate is missing from the query, the websearch-agent fetches it from the internet. The calculator-agent handles complex numerical computations with step-by-step reasoning, while the graph-agent uses the code interpreter to visualize inflation trends over time. Together, these agents collaborate via handoffs to deliver accurate, dynamic responses to economic queries.

    Copy CodeCopiedUse a different Browser
    # 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"}]
    )

    Defining the Handoffs Responsibilities

    This configuration defines how agents delegate tasks among each other:

    • The Main Agent (economics_agent) serves as the entry point and delegates queries either to the inflation_agent (for inflation calculations) or the websearch_agent (to fetch missing data like inflation rates).
    • The inflation_agent, after receiving either the user query or web-fetched data, can further pass tasks to the calculator_agent (for detailed math) or graph_agent (to visualize trends).
    • The websearch_agent can pass control to the inflation_agent after retrieving required information, like the inflation rate.
    • calculator_agent and graph_agent are considered terminal agents. However, optional mutual handoff is enabled in case one needs to do follow-up work (e.g., graphing a calculated result or vice versa).
    Copy CodeCopiedUse a different Browser
    # 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: Running the Agent

    Example A: What is the current inflation rate in India?

    In this example, the prompt “What is the current inflation rate in India?” is passed to the economics_agent, which is the main entry point for handling economic queries. Since the question requires real-time data that isn’t included in the agent’s static knowledge, the economics_agent automatically hands off the query to the websearch_agent, which is equipped with web search capabilities.

    Copy CodeCopiedUse a different Browser
    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 inflation-adjusted value of 5,000 from the year 2010 to 2023 with an annual inflation rate of 6.5%. Explain calculation steps and plot a graph with data labels

    This code block sends the prompt to an economics agent, checks if the agent triggers a specific function call (adjust_for_inflation), executes that function locally with the provided arguments, and then returns the computed result back to the agent. Finally, it prints the agent’s response, which includes the inflation calculation explanation, along with the Python code to plot the trend.

    Copy CodeCopiedUse a different Browser
    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 following code block was returned by the agent to plot the trend of inflation-adjusted value over time.

    Copy CodeCopiedUse a different Browser
    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 out the Notebook. All credit for this research goes to the researchers of this project. Also, feel free to follow us on Twitter and don’t forget to join our 98k+ ML SubReddit and Subscribe to our Newsletter.

    The post How to Create Smart Multi-Agent Workflows Using the Mistral Agents API’s Handoffs Feature appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleBreaking the QA Barrier: Build a Test Automation CoE That Scales Excellence
    Next Article ALPHAONE: A Universal Test-Time Framework for Modulating Reasoning in AI Models

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    July 23, 2025
    Machine Learning

    FastVLM: Efficient Vision Encoding for Vision Language Models

    July 23, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2024-6030 – Tesla Model S oFono Privilege Escalation Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Cisco Issues Urgent Patch for Critical Unified CM Vulnerability (CVE-2025-20309)

    Development

    Le notizie minori del mondo GNU/Linux e dintorni della settimana nr 25/2025

    Linux

    CVE-2025-6059 – WordPress Seraphinite Accelerator CSRF

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    CVE-2025-32441 – Rack Session Pool Session Hijacking Vulnerability

    May 7, 2025

    CVE ID : CVE-2025-32441

    Published : May 7, 2025, 11:15 p.m. | 20 minutes ago

    Description : Rack is a modular Ruby web server interface. Prior to version 2.2.14, when using the `Rack::Session::Pool` middleware, simultaneous rack requests can restore a deleted rack session, which allows the unauthenticated user to occupy that session. Rack session middleware prepares the session at the beginning of request, then saves is back to the store with possible changes applied by host rack application. This way the session becomes to be a subject of race conditions in general sense over concurrent rack requests. When using the `Rack::Session::Pool` middleware, and provided the attacker can acquire a session cookie (already a major issue), the session may be restored if the attacker can trigger a long running request (within that same session) adjacent to the user logging out, in order to retain illicit access even after a user has attempted to logout. Version 2.2.14 contains a patch for the issue. Some other mitigations are available. Either ensure the application invalidates sessions atomically by marking them as logged out e.g., using a `logged_out` flag, instead of deleting them, and check this flag on every request to prevent reuse; or implement a custom session store that tracks session invalidation timestamps and refuses to accept session data if the session was invalidated after the request began.

    Severity: 4.2 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    The Secret to Instagram Success Using a Cohesive Visual Style

    July 11, 2025

    Windows Administrators Blog Named One of FeedSpot’s Top 25 Microsoft Windows Blogs

    May 10, 2025

    Discovering novel algorithms with AlphaTensor

    May 13, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.