Skip to main content
The Calculator tool enables agents to perform mathematical calculations and evaluate mathematical expressions. It’s a simple but essential tool for agents that need to work with numbers.

Overview

The Calculator tool uses a mathematical expression evaluator to compute results from string-based mathematical expressions. It’s implemented using LangChain’s Calculator tool and can handle basic arithmetic as well as more complex mathematical operations.
This tool is particularly useful when you need precise numerical calculations rather than having the LLM attempt to do math (which can be unreliable).

Configuration

The Calculator tool has no configuration parameters. Simply add it to your agent’s tool list to enable mathematical calculations.
// No inputs required
// The tool is ready to use immediately

Usage

Adding to an Agent

1

Add Calculator Tool

Drag the Calculator tool from the Tools category onto your canvas.
2

Connect to Agent

Connect the Calculator tool to your agent’s tools input.Compatible agents:
  • Tool Agent
  • ReAct Agent
  • Conversational Agent
  • OpenAI Assistant
3

Test

Ask the agent a question requiring calculation, such as:
  • “What is 15 multiplied by 27?”
  • “Calculate the area of a circle with radius 5”
  • “What’s 1234 + 5678?”

Example Queries

The agent can handle various types of mathematical questions:
User: What is 456 + 789?
Agent: Let me calculate that for you.
[Uses Calculator: 456 + 789]
Result: 1245

Supported Operations

The Calculator tool supports standard mathematical operations:
  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: ** or ^
  • Modulo: %
5 + 3        // 8
10 - 4       // 6
6 * 7        // 42
20 / 4       // 5
2 ** 8       // 256
17 % 5       // 2

How It Works

1

Agent Receives Query

User asks a question that requires mathematical calculation.
2

Agent Recognizes Need

The LLM determines that a calculation is needed to answer the question.
3

Tool Selection

Agent selects the Calculator tool and formats the mathematical expression.
4

Expression Evaluation

The calculator evaluates the expression and returns the numerical result.
5

Response Generation

The agent incorporates the calculation result into its response to the user.

Common Use Cases

Financial Calculations

Calculate prices, discounts, taxes, and totals for e-commerce or financial applications

Unit Conversions

Perform numerical conversions when combined with conversion formulas

Data Analysis

Calculate statistics, percentages, and aggregations from data

Engineering Problems

Solve mathematical problems in technical support or engineering chatbots

Example Workflows

E-Commerce Price Calculator

User: I have a cart with items costing $25, $40, and $15. 
      With a 10% discount, what's my total?

Agent Reasoning:
1. Calculate subtotal: 25 + 40 + 15
2. Calculate discount: subtotal * 0.10
3. Calculate final total: subtotal - discount

[Calculator: 25 + 40 + 15] → 80
[Calculator: 80 * 0.10] → 8
[Calculator: 80 - 8] → 72

Response: Your subtotal is $80. With a 10% discount ($8), 
          your final total is $72.

Area and Perimeter Calculator

User: What's the area of a rectangle that's 12 meters by 8 meters?

Agent Reasoning:
- Area of rectangle = length × width
- Need to calculate: 12 * 8

[Calculator: 12 * 8] → 96

Response: The area of a 12m × 8m rectangle is 96 square meters.

Best Practices

When to Use the Calculator
  • Any arithmetic or mathematical operation
  • Precise numerical calculations
  • Complex expressions with multiple operations
  • Financial calculations where accuracy is critical
  • When you need reproducible, exact results
Limitations
  • Basic math only: No advanced functions like trigonometry or logarithms
  • String expressions: Input must be a valid mathematical expression string
  • No variables: Cannot store or reference variables
  • No symbolic math: Evaluates to numerical results only

Troubleshooting

Possible causes:
  • Question doesn’t clearly require calculation
  • Agent attempts to do math mentally (unreliable)
  • Calculator tool not properly connected
Solutions:
  • Rephrase question to explicitly request calculation
  • Improve system message to encourage tool usage
  • Verify tool is connected in the workflow
  • Use more explicit language: “Calculate…”, “What is the exact result of…”
Possible causes:
  • Agent formatted the expression incorrectly
  • Invalid mathematical syntax
Solutions:
  • Review agent’s verbose logs to see the expression sent
  • Update system message with examples of valid expressions
  • Test with simpler calculations first
Possible causes:
  • Order of operations issue
  • Floating point precision
  • Agent misunderstood the problem
Solutions:
  • Check the actual expression used in verbose logs
  • Verify parentheses are used correctly for grouping
  • Ask the agent to show its work step-by-step

Tips for Better Results

Improve Agent Math Performance
  1. Be explicit: Use phrases like “calculate exactly” or “use the calculator”
  2. System message: Include instructions like “Always use the calculator tool for mathematical operations”
  3. Step-by-step: For complex problems, ask the agent to break down the calculation
  4. Verify results: For critical calculations, ask the agent to double-check

Example System Message

You are a helpful assistant with access to a calculator tool.
When users ask mathematical questions, ALWAYS use the calculator 
tool to ensure accurate results. Never attempt to do arithmetic 
mentally. Show your calculations step-by-step for complex problems.

Combining with Other Tools

The Calculator tool works well in combination with:

Web Browser

Search for data, then calculate results

Retriever

Retrieve numerical data, then perform calculations

API Tools

Fetch data from APIs and calculate aggregations

Custom Tools

Create custom tools that use calculator for internal computations

Example: Web Search + Calculator

User: What's the current price of Bitcoin multiplied by 100?

Agent:
1. [Web Search: "current Bitcoin price"]
   Result: Bitcoin is currently trading at $43,250

2. [Calculator: 43250 * 100]
   Result: 4,325,000

Response: The current Bitcoin price ($43,250) multiplied by 100 
          equals $4,325,000.

Alternative Tools

For more advanced mathematical operations, consider:
  • WolframAlpha Tool: Access to Wolfram’s computational engine for advanced math
  • Python REPL Tool: Execute Python code for complex calculations and data manipulation
  • Custom Tool: Create a custom tool wrapping specialized mathematical libraries

Agent Overview

Learn about building agents with tools

Custom Tools

Create tools for specialized calculations

Tool Agent

Best agent for using calculator and other tools

WolframAlpha

Advanced mathematical and computational tool

Build docs developers (and LLMs) love