Project: AI Math Tutor
Dec 30, 2025 • 30 min read
Standard LLMs are terrible at math. They "hallucinate" numbers because they predict the next token instead of calculating the result. Asking GPT-4 to multiply 6-digit numbers is like asking a poet to do accounting.
In this guide, we will build a Math Tutor Agent that uses OpenAI's Code Interpreter to write and execute Python code for 100% accurate calculations.
1. The Architecture: "Don't Compute, Orchestrate"
Instead, it should:
- Write a Python script using
scipyorsympy. - Execute it in a sandboxed environment.
- Read the
stdoutresult. - Explain the steps to the student.
2. Setting up the Assistant with Code Interpreter
We only need to do this once. The Assistant lives on OpenAI's servers.
from openai import OpenAI
client = OpenAI(api_key="sk-...")
assistant = client.beta.assistants.create(
name="Professor Pi",
instructions="You are a math tutor. Always write Python code to solve problems. Use SymPy for algebra and Matplotlib for graphs.",
tools=[{"type": "code_interpreter"}],
model="gpt-4-turbo"
)
print(f"Assistant ID: {assistant.id}")3. Solving Symbolic Math (Algebra)
Most tutorials just show `1 + 1`. Let's solve Algebra.
The Assistant will automatically generate and run this:
import sympy
x = sympy.symbols('x')
expr = sympy.sin(x**2)
derivative = sympy.diff(expr, x)
print(derivative)Output: 2*x*cos(x**2)
4. Generating Graphs (Visual Learning)
A good tutor draws diagrams. Code Interpreter can generate image files.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y = x**3
plt.plot(x, y)
plt.title("y = x^3")
plt.grid(True)
plt.savefig('/mnt/data/plot.png') # <--- Saves to sandbox5. Handling Image Files in the Frontend
When the run completes, you need to check `messages` for image files.
// Node.js: Retrieve the file content
const messages = await openai.beta.threads.messages.list(thread.id);
const lastMsg = messages.data[0];
if (lastMsg.content[0].type === 'image_file') {
const fileId = lastMsg.content[0].image_file.file_id;
// Download the raw bytes
const response = await openai.files.content(fileId);
const imageBuffer = await response.arrayBuffer();
// Send to frontend as Base64 or Blob
}6. Integrating LaTeX Rendering
For the text explanations, tell the model to output LaTeX (e.g., $2x^2$). Then use a library like react-katex to render it beautifully.
System Instruction Tweak
"Output all mathematical expressions in LaTeX format wrapped in single dollar signs."
import 'katex/dist/katex.min.css';
import Latex from 'react-latex-next';
// In your chat component
<div className="message-bubble">
<Latex>{message.content}</Latex>
</div>7. Next Steps
This is just the beginning. The Assistants API also supports Function Calling (connecting to external APIs like Wolfram Alpha) and File Search (uploading textbooks as knowledge).
"Don't calculate. Orquestrate."
Learn more about Assistants API v2 →