> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Math AI Agent

> Learn how to create AI agents that can perform complex mathematical calculations, unit conversions, and financial computations.

Math agents evaluate expressions, solve equations, convert units, and run statistical or financial calculations through specialised tools on a single `Agent`.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent

agent = Agent(name="Math Tutor", instructions="Solve math problems step by step.")
agent.start("What is 15 percent of 240?")
```

The user asks a quantitative question; the math agent selects the right calculation tools and returns a clear answer.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    U[👤 User] --> A[🧮 Math Agent]
    A --> E[evaluate]
    A --> S[solve_equation]
    A --> C[convert_units]
    A --> F[calculate_financial]

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff

    class U,A agent
    class E,S,C,F tool
```

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Feature as Math AI Agent

    User->>Agent: Request
    Agent->>Feature: Process request
    Feature-->>Agent: Result    Agent-->>User: Response
```

## Quick Start

<Tabs>
  <Tab title="Code">
    <Steps>
      <Step title="Install Package">
        First, install the PraisonAI Agents package:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        pip install praisonaiagents sympy pint
        ```
      </Step>

      <Step title="Set API Key">
        Set your OpenAI API key as an environment variable in your terminal:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        export OPENAI_API_KEY="${OPENAI_API_KEY:?Set OPENAI_API_KEY in your shell}"
        ```
      </Step>

      <Step title="Create a file">
        Create a new file `app.py` with the basic setup:

        ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        from praisonaiagents import Agent, Task, AgentTeam
        from praisonaiagents import (
            evaluate, solve_equation, convert_units,
            calculate_statistics, calculate_financial
        )

        # Create math agent
        math_agent = Agent(
            role="Math Expert",
            goal="Perform complex mathematical calculations",
            backstory="Expert in mathematical computations and analysis",
            tools=[
                evaluate, solve_equation, convert_units,
                calculate_statistics, calculate_financial
            ],
            
        )

        # Create a task
        task = Task(
            description="Calculate compound interest and statistical analysis",
            expected_output="Detailed mathematical analysis",
            agent=math_agent
        )

        # Create and start the agents
        agents = AgentTeam(
            agents=[math_agent],
            tasks=[task],
            process="sequential",
            verbose=2
        )

        # Start execution
        agents.start()
        ```
      </Step>

      <Step title="Start Agents">
        Type this in your terminal to run your agents:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        python app.py
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab title="No Code">
    <Steps>
      <Step title="Install Package">
        Install the PraisonAI package:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        pip install praisonai sympy pint
        ```
      </Step>

      <Step title="Set API Key">
        Set your OpenAI API key as an environment variable in your terminal:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        export OPENAI_API_KEY="${OPENAI_API_KEY:?Set OPENAI_API_KEY in your shell}"
        ```
      </Step>

      <Step title="Create a file">
        Create a new file `agents.yaml` with the basic setup:

        ```yaml theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        framework: praisonai
        process: sequential
        topic: perform mathematical analysis
        agents:  # Canonical: use 'agents' instead of 'roles'
          mathematician:
            instructions:  # Canonical: use 'instructions' instead of 'backstory' Expert in mathematical computations and analysis.
            goal: Perform complex mathematical calculations
            role: Math Expert
            tools:
              - evaluate
              - solve_equation
              - convert_units
              - calculate_statistics
              - calculate_financial
            tasks:
              math_task:
                description: Calculate compound interest and perform statistical analysis.
                expected_output: Detailed mathematical analysis with results.
        ```
      </Step>

      <Step title="Start Agents">
        Type this in your terminal to run your agents:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        praisonai agents.yaml
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>

<Note>
  **Requirements**

  * Python 3.10 or higher
  * OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
  * sympy and pint packages (automatically installed when needed)
</Note>

## Understanding Math Agents

<Card title="What are Math Agents?" icon="question">
  Math agents are specialized AI agents that can:

  * Evaluate mathematical expressions
  * Solve equations and systems of equations
  * Perform unit conversions
  * Calculate statistical metrics
  * Handle financial calculations
</Card>

## Features

<CardGroup cols={2}>
  <Card title="Expression Evaluator" icon="function">
    Evaluates mathematical expressions and formulas.
  </Card>

  <Card title="Equation Solver" icon="equals">
    Solves mathematical equations and systems.
  </Card>

  <Card title="Unit Converter" icon="arrows-rotate">
    Converts between different units of measurement.
  </Card>

  <Card title="Statistical Calculator" icon="chart-simple">
    Calculates statistical metrics and analysis.
  </Card>
</CardGroup>

## Multi-Agent AI Math Analysis

<Tabs>
  <Tab title="Code">
    <Steps>
      <Step title="Install Package">
        First, install the PraisonAI Agents package:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        pip install praisonaiagents
        ```
      </Step>

      <Step title="Set API Key">
        Set your OpenAI API key as an environment variable in your terminal:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        export OPENAI_API_KEY="${OPENAI_API_KEY:?Set OPENAI_API_KEY in your shell}"
        ```
      </Step>

      <Step title="Create a file">
        Create a new file `app.py` with the basic setup:

        ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        from praisonaiagents import Agent, Task, AgentTeam
        from praisonaiagents import (
            evaluate, solve_equation, convert_units,
            calculate_statistics, calculate_financial
        )

        # Create first agent for calculations
        calculator = Agent(
            role="Mathematical Calculator",
            goal="Perform complex calculations and analysis",
            backstory="Expert in mathematical computations",
            tools=[evaluate, solve_equation, convert_units],
            
        )

        # Create second agent for statistics
        statistician = Agent(
            role="Statistical Analyst",
            goal="Perform statistical analysis and interpretation",
            backstory="Expert in statistical analysis",
            tools=[calculate_statistics, calculate_financial],
            
        )

        # Create first task
        calc_task = Task(
            description="Calculate compound interest for $10000 at 5% for 3 years",
            expected_output="Detailed interest calculation",
            agent=calculator
        )

        # Create second task
        stats_task = Task(
            description="Analyze the monthly interest distribution",
            expected_output="Statistical analysis of interest data",
            agent=statistician
        )

        # Create and start the agents
        agents = AgentTeam(
            agents=[calculator, statistician],
            tasks=[calc_task, stats_task],
            process="sequential"
        )

        # Start execution
        agents.start()
        ```
      </Step>

      <Step title="Start Agents">
        Type this in your terminal to run your agents:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        python app.py
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab title="No Code">
    <Steps>
      <Step title="Install Package">
        Install the PraisonAI package:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        pip install praisonai
        ```
      </Step>

      <Step title="Set API Key">
        Set your OpenAI API key as an environment variable in your terminal:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        export OPENAI_API_KEY="${OPENAI_API_KEY:?Set OPENAI_API_KEY in your shell}"
        ```
      </Step>

      <Step title="Create a file">
        Create a new file `agents.yaml` with the basic setup:

        ```yaml theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        framework: praisonai
        process: sequential
        topic: perform financial and statistical analysis
        agents:  # Canonical: use 'agents' instead of 'roles'
          calculator:
            instructions:  # Canonical: use 'instructions' instead of 'backstory' Expert in mathematical computations and analysis.
            goal: Perform complex calculations
            role: Mathematical Calculator
            tools:
              - evaluate
              - solve_equation
              - convert_units
            tasks:
              calc_task:
                description: Calculate compound interest for investment scenario.
                expected_output: Detailed interest calculations.

          statistician:
            instructions:  # Canonical: use 'instructions' instead of 'backstory' Expert in statistical analysis and interpretation.
            goal: Analyze numerical data and patterns
            role: Statistical Analyst
            tools:
              - calculate_statistics
              - calculate_financial
            tasks:
              stats_task:
                description: Analyze the monthly interest distribution.
                expected_output: Statistical analysis report.
        ```
      </Step>

      <Step title="Start Agents">
        Type this in your terminal to run your agents:

        ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
        praisonai agents.yaml
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>

### Configuration Options

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Create an agent with advanced math configuration
agent = Agent(
    role="Math Expert",
    goal="Perform complex mathematical analysis",
    backstory="Expert in mathematical computations",
    tools=[
        evaluate,
        solve_equation,
        convert_units,
        calculate_statistics,
        calculate_financial
    ],
      # Enable detailed logging
    llm="gpt-4o"  # Language model to use
)
```

## Best Practices

<AccordionGroup>
  <Accordion title="Attach only the tools you need">
    Give calculation agents `evaluate` and `solve_equation`; reserve `calculate_statistics` and `calculate_financial` for analysis agents.
  </Accordion>

  <Accordion title="Use clear task descriptions">
    State the formula, units, and expected output format in each `Task` so the agent picks the right tool.
  </Accordion>

  <Accordion title="Validate units before conversion">
    Ensure input values include compatible units — `convert_units` fails on mismatched dimensions.
  </Accordion>
</AccordionGroup>

## Troubleshooting

<CardGroup cols={2}>
  <Card title="Expression Errors" icon="triangle-exclamation">
    If expressions are not evaluating:

    * Check syntax and formatting
    * Verify variable definitions
    * Enable verbose mode for debugging
  </Card>

  <Card title="Calculation Issues" icon="calculator">
    If calculations are incorrect:

    * Review input formats
    * Check unit compatibility
    * Verify formula structure
  </Card>
</CardGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="AutoAgents" icon="robot" href="./autoagents">
    Learn about automatically created and managed AI agents
  </Card>

  <Card title="Mini Agents" icon="microchip" href="./mini">
    Explore lightweight, focused AI agents
  </Card>
</CardGroup>

<Note>
  For optimal results, ensure mathematical expressions and units are properly formatted for your specific use case.
</Note>

## Related

<CardGroup cols={2}>
  <Card icon="code" href="/features/codeagent">
    Use a specialised agent that writes and runs code to solve problems.
  </Card>

  <Card icon="microchip" href="/features/mini">
    Run lightweight, focused agents for single-purpose tasks.
  </Card>
</CardGroup>
