A minimal example of an AI agent that can use a calculator tool. Perfect for beginners!
- ✅ Basic tool-calling concepts
- ✅ How LLMs decide when to use tools
- ✅ Tool definition and schemas
- ✅ The agent execution loop
- Python 3.10+
- OpenAI API key
# 1. Navigate to this directory
cd examples/python-basic
# 2. Install dependencies
pip install -r requirements.txt
# 3. Set up your API key
cp .env.example .env
# Edit .env and add your OPENAI_API_KEYpython main.pyExpected output:
User: What is 25 * 4 + 10?
Agent thinking: I need to calculate this expression
Tool called: calculator(expression="25 * 4 + 10")
Tool result: 110
Agent: The answer is 110.
def calculator(expression: str) -> str:
"""Evaluates a mathematical expression."""
try:
result = eval(expression)
return str(result)
except Exception as e:
return f"Error: {str(e)}"Key Points:
- Simple Python function
- Takes a string (math expression)
- Returns a string (result)
- Handles errors gracefully
tools = [{
"type": "function",
"function": {
"name": "calculator",
"description": "Evaluates mathematical expressions. Use for any calculation.",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "A valid Python math expression (e.g., '2 + 2')"
}
},
"required": ["expression"]
}
}
}]Why This Matters:
- LLM reads the description to understand what the tool does
- Parameters schema tells LLM what arguments to provide
- Clear descriptions = better tool selection
messages = [{"role": "user", "content": "What is 25 * 4 + 10?"}]
# First LLM call
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools
)
# Check if tool was called
if response.choices[0].message.tool_calls:
# Execute tool
tool_call = response.choices[0].message.tool_calls[0]
result = calculator(args["expression"])
# Add result to conversation
messages.append(tool_result_message)
# Second LLM call with result
final_response = client.chat.completions.create(
model="gpt-4",
messages=messages
)The Flow:
- User asks a question
- LLM decides to use calculator
- We execute the calculator
- LLM receives result
- LLM formats final answer
Try adding a weather tool:
def get_weather(location: str) -> str:
"""Mock weather API"""
return f"The weather in {location} is sunny, 22°C"Ask: "What is the square root of 144 plus 50?"
The agent should calculate: 144**0.5 + 50 = 62
Ask: "What is abc + 123?"
See how the agent handles invalid expressions.
Ask: "Calculate 10 * 5, then add 20 to that result"
Does the agent chain the calculations?
Solution: Make sure .env file exists with your key:
OPENAI_API_KEY=sk-...
Solution: You've hit OpenAI's rate limit. Wait a moment and try again.
Solution: Make sure:
- Tool description is clear
- Question requires calculation
- Model is gpt-4 or gpt-3.5-turbo-0613+
User: What is 25 * 4 + 10?
Your question
Agent thinking: I need to calculate this expression
LLM's internal reasoning (in messages)
Tool called: calculator(expression="25 * 4 + 10")
LLM decided to use the calculator tool
Tool result: 110
Our calculator function returned this
Agent: The answer is 110.
LLM formats the final response
After mastering this example:
- Add more tools - Try file reading, API calls, etc.
- Try UTCP - See how UTCP Weather Example differs
- Add complexity - Move to Multi-Tool Agent
- Build a project - Try Calculator Agent Project
✅ Tools are just Python functions with schemas ✅ LLMs use descriptions to decide when to call tools ✅ The agent loop: Ask → Decide → Execute → Respond ✅ Clear descriptions lead to better tool selection
main.py- The complete agent coderequirements.txt- Python dependencies.env.example- Template for API keysREADME.md- This file