🧮 Math Assistant

pre-built calculator tool via Strands + Bedrock

Concept

Uses the pre-built calculator tool from strands_tools plus a system prompt. Strands ships ready-made community tools so you don't have to write everything from scratch. The calculator tool evaluates math expressions precisely (no floating-point guessing by the LLM). A system prompt shapes the agent's persona on every request.

Theory & Concepts

The Math Assistant demo illustrates how we solve the problem of LLMs struggling with exact math. When you ask a math question in the browser, it reaches math_agent.py where the agent is set up with a pre-built calculator tool from the strands_tools library.

We also give the agent a system prompt ("You are a helpful math assistant.") to guide its behavior. When Amazon Nova Pro sees a mathematical expression, instead of guessing the answer (which LLMs are prone to do), it emits a tool call to the calculator. Strands executes this calculation securely in Python and hands the exact result back to the model.

The model then formulates a natural language response containing the accurate result and sends it back to your browser. This demonstrates how off-the-shelf tools can instantly upgrade an agent's capabilities with deterministic precision.

Request flow

Browser question POST /math
app.py math_route math_agent.py solve()
Agent(calculator tool) Bedrock Nova Pro
calculator called result back to Browser

Code flow

flowchart TD A["Browser<br/>question"] -->|"POST /math"| B["app.py<br/>math_route"] B -->|"question"| C["math_agent.py<br/>solve"] C -->|"question + tools"| D["Bedrock<br/>Nova Pro"] D -->|"tool call: calculator"| E["calculator<br/>tool"] E -->|"result"| D D -->|"answer text"| C C -->|"answer text"| B B -->|"JSON result"| A

Backend

math_agent.py
"""Module 2 demo — a pre-built community tool: the calculator.

You don't have to write everything: `strands_tools` ships ready-made tools.
This also uses a SYSTEM PROMPT — standing instructions that shape the agent's
behaviour on every request.
"""

from strands import Agent
from strands.models.bedrock import BedrockModel
from strands_tools import calculator

from config import MODEL_ID, agent_text


def solve(question: str) -> str:
    """Answer a math question using the pre-built calculator tool."""
    agent = Agent(
        model=BedrockModel(model_id=MODEL_ID),
        tools=[calculator],  # pre-built tool from strands_tools community package
        system_prompt="You are a helpful math assistant.",
        callback_handler=None,
    )
    return agent_text(agent(question))