Calculator Agent A model using a tool in a loop

First AI agent, built from scratch

Ask a maths question in plain words.

The agent breaks the problem into steps, calls a calculator tool for each one, and shows you every turn of the loop until it has the answer.

    How the agent works

    "Agents are models using tools in a loop." This one is built from four parts, in plain Python with no agent framework.

    The user's question goes to the model. If the model asks for the calculator, the tool runs and its result goes back to the model. This repeats until the model writes the final answer. Question Model Calculator Answer tool_use result end_turn
    1. LLM and instructions. Claude, with a system prompt that tells it to solve problems step by step and use the calculator for every sum.
    2. Memory. Every message, tool call and tool result is kept in a list and sent back each turn, so follow-up questions work.
    3. Tool. A calculator the model can call. Expressions are parsed safely rather than run with eval().
    4. The loop. Call the model. If it asks for a tool, run it, send the result back and repeat, up to 10 times, until it answers.

    Based on Leonie Monigatti's guide AI Agent from Scratch in Python. The trace under each answer shows this loop as it runs.