🔢 Calculator Agent Training with Multi-Turn Reinforcement Learning
TL;DR: I successfully trained Qwen 2.5 (both 0.5B and 3B variants) to use a slightly complex calculator tool through multi-turn reinforcement learning, achieving a 62% absolute increase in evaluation accuracy. Final models are available on Hugging Face.
Why train an LLM to use a calculator?
Because despite their reasoning capabilities, LLMs struggle with arithmetic precision—even basic math can lead to errors.
Developed training pipeline leveraging the verifiers framework
Iteratively improved reward signal
Executed successful RL fine-tuning runs
📈 Training Results
Below is a visualization of the reward improvement over time for two model sizes.
0.5B Model
Starts at ~0.2 reward
Ends above 0.8 reward
3B Model
Begins at 0.7 reward
Peaks near perfect reward (~1.0)
0.5B Reward Graph
3B Reward Graph
🔄 Training & Rollout Details
This project leveraged Group Relative Policy Optimization (GRPO), a novel algorithm designed for preference-aligned policy updates using group-level comparisons. GRPO encourages the model to learn from relative advantages within a group of sampled responses, making it particularly well-suited for structured reasoning tasks.
🔁 Rollout Strategy
8 samples per training prompt, each generated with a temperature of 0.9 to encourage diversity while maintaining coherence.
All rollouts were performed using a vLLM server hosted locally during training.
This output is parsed into Python code, calculated, and then the environment response is shown to the model as:
<output>
645500.9728915663
</output>
The model is then asked to formulate a final response to the user.
👀 Calculator Environment State
There was no need for a state as this was a simple tool calling environment.
🧪 Evaluation Suite
A custom benchmark suite built using the agentic_environmentslibrary. The eval dataset is here, and the inference code here.
Performance Improvements
Model Size
Pre-RL Accuracy
Post-RL Accuracy
▲ Increase
0.5B
0.6%
34%
+33.4 pts
3B
27%
89%
+62 pts
Eval performance graph inclusing Qwen 3
At the prompt of this Reddit comment and out of curiosity, I ran the evals against the newer Qwen3 models, which were released 7 months after Qwen 2.5.
Looking at the data, we can see that Qwen3 naturally achieves strong performance without RL.
We could expect that an RL-trained 1.7B Qwen3 would outperform an RL trained 3B Qwen2.5, a model almost double its size!
1question,expression,answer2"What is 4829 multiplied by 736?", 4829*736,3554144.0
Dataset Generation Methodology
Firstly, Gemini-2.5-Pro was prompted with relevant context and instructions to generate a very diverse dataset. It was instructed to diversify in the following ways:
The way a question was asked (e.g: "Find the product", "What do you get when", etc..)
The complexity of the question
It's guidelines were:
Operations must be addition, subtraction, division and multiplication.
Questions should at least be very / extremely hard for an average human to work out in their heads without the use of a calculator.
Gemini-2.5-Pro was asked to generate only the "question" & "expression" columns. This is to avoid relying on an LLM to find the correct answer. Instead the answer column was generated using a simple python script that accepted the csv as input, and ran eval() on each expression column to generate the "answer" column.
The eval dataset was made specifically to cover all the various scenarios covered in the training dataset.
📦 Models Available on Hugging Face
✅ Both trained models are now publicly accessible:
As a temp solution whilst verifiers is not on PyPI, clone verifiers at the same level as calculator_agent_rl.
-
|
-- calculator_agent_rl/
-- verfifers/
This is required in order to access the verifiers lib.
Use the devcontainer and Dockerfile for development. If using VSCode this should popup automatically.
Deployment
Rent GPU from somewhere like runpod and connect via SSH
Install uv curl -LsSf https://astral.sh/uv/install.sh | sh
Open workspace dir & Clone repo into /workspacegit clone https://{githubaccess_token}@github.com/AiTuning-Ltd/{repo}.git
Clone verifiers into /workspacegit clone https://github.com/willccbb/verifiers.git
Follow when deployed steps below
When deployed onto a training node
The below example code runs Qwen 2.5 3B on 8x GPUs (x4 for inference, x4 for training)
Run uv sync after the verifiers repo is cloned too as mentioned above.
Run uv add flash-attn --no-build-isolation
Ensure .env file is set at the root of the project
Run vLLM server (Example for a x4 GPUs):
a. cd ../verifiers
b. CUDA_VISIBLE_DEVICES=0,1,2,3 python verifiers/inference/vllm_serve.py --model "Qwen/Qwen2.5-3B-Instruct" --tensor_parallel_size 4 --max_model_len 8192 --gpu_memory_utilization 0.9 --enable_prefix_caching True
Run train.py using accelerate (Example on x4 GPUs):
CUDA_VISIBLE_DEVICES=4,5,6,7 accelerate launch --num-processes 4 --config-file ../verifiers/configs/zero3.yaml src/train.py
Deployment issue fixes
If GPUs hang at 100% utilisation for both vLLM or training script initialisation
0. Ensure you are using CUDA version 12.4+
Stop the processes
In the terminal: export NCCL_P2P_DISABLE=1. Fix found here
Re-run script
If error in verfiers package about "question"
Go to data_utils.py in verifiers and change "question" to 'question' on lines 118 & 132
If Anthropic API key is not loading onto each GPU for judge
Go to claude.py & change Anthropic() to Anthropic(api_key="{api_key}")
📚 Acknowledgements
This project makes use of the Verifiers library for reinforcement learning in verifiable environments. If you use this code in your research, please cite:
@article{brown2025verifiers,
title={Verifiers: Reinforcement Learning with LLMs in Verifiable Environments},
author={Brown, William},
year={2025}
}