Skip to main content

LLM-controlled robot via tool calls

Time: 11:25 AM to 12:15 PM
This is the moment everything comes together. Your robot now understands speech, thinks with a large language model, speaks back to you, and executes physical actions based on the conversation.

The full loop


Building the program

This is the most complex program of the week. Here is how each piece works.

Step 1 — Define the tool menu

You give GPT a list of tools as JSON dictionaries. Each tool has a name, description, and parameters. Here is one tool from the list:
The model reads these descriptions to decide which tool to call and what arguments to pass. It never sees your Python code — only this JSON menu.

Step 2 — Write the action functions

Each tool maps to a Python function that controls the physical robot:
Notice two safety patterns:
  • Clampingmax(10, min(speed, 60)) prevents the LLM from requesting dangerous speeds
  • Return value — each function returns a string describing what happened, which gets sent back to GPT

Step 3 — Connect tools to functions

A dictionary maps tool names to Python functions. When GPT says "call move_forward", your code looks it up here:

Step 4 — Process a command with GPT

This is the core function. It sends the user’s command to GPT along with the tool menu, then executes any tool calls GPT requests:
There are two API calls per command:
  1. First call — GPT reads the command and the tool menu, then returns structured JSON saying which functions to call with which arguments
  2. Second call — after your code executes the tools and sends results back, GPT generates the natural language response that gets spoken aloud

Step 5 — The main loop

Tie it all together with wake word detection and a command loop:
In type mode (--type flag), the wake word and voice input are replaced with input() — same tool-calling logic, just typed instead of spoken.

Run the program

Voice mode (with USB microphone):
Keyboard mode (type commands instead of speaking):
This program needs sudo because it controls the motors and servos, which require root access to the I2C bus.

How to interact

Voice mode: Say the wake word “Hey buddy” to activate the robot. It will respond with “Yes?” and listen for your command. Speak your request clearly, and the robot will execute it. Type mode: Just type your command and press Enter. No wake word needed.

Available actions

The LLM has access to these 10 tools. It decides which ones to call based on your natural language command:
The model can call multiple tools in one response. Try compound commands like “make a square” — it will call move_forward and turn_right four times in sequence.

Try these commands

Start with simple commands, then try more complex ones: Simple:
  • “Move forward”
  • “Turn left”
  • “Celebrate”
  • “Look around”
With parameters:
  • “Drive forward for 5 seconds”
  • “Back up slowly for 3 seconds”
  • “Turn right 90 degrees”
Multi-step:
  • “Make a square”
  • “Drive forward, then turn around and come back”
  • “Nod yes, then say ‘I agree!’”
Creative:
  • “Do a victory dance”
  • “Explore the room”
  • “Act excited about something”
Questions (no tools needed):
  • “What can you do?”
  • “How do your motors work?”
  • “Tell me a joke about robots”

How tool calls flow in the code

Here is what happens inside process_command() when you say “move forward for 3 seconds”:

Inspect the code

Read through llm_robot.py and find the answers to these questions:
1

Find the system prompt

Look for the SYSTEM_PROMPT variable. This is the hidden instruction that tells GPT it is controlling a physical robot. How does it instruct the model to behave?
2

Find the tool definitions

Look for the TOOLS list. Each tool is a JSON dictionary with a name, description, and parameters. Count how many tools are defined. Do the descriptions match the table above?
3

Find where tools get executed

Look for the ACTIONS dictionary and the process_command function. When GPT returns a tool call, how does the code know which Python function to run?
4

Find where the result goes back

In process_command, after executing a tool, the result is appended to messages with role: "tool". Then a second API call is made so GPT can generate its final spoken response. Why does the model need to see the result?

Make it yours

1

Change the wake word

Open llm_robot.py and find the WAKE_WORD variable near the top. Change "hey buddy" to whatever you want — your name, a code word, or a silly phrase.
2

Change the personality

Find the SYSTEM_PROMPT variable and rewrite it. Make the robot a pirate, a drill sergeant, a surfer dude, or a Shakespearean actor. The personality affects how it responds and which tools it chooses to call.
3

Add a new tool

This is the advanced challenge. To add a new tool:
  1. Add a new entry to the TOOLS list with a name, description, and parameters
  2. Write a Python function that performs the physical action
  3. Add it to the ACTIONS dictionary
Ideas for new tools:
  • spin_in_circle — turn 360 degrees
  • patrol — drive forward, look around, drive back
  • shy — slowly back away while looking down
  • read_distance — read the ultrasonic sensor and report what it sees
When testing with motors, always have the robot on a clear surface with space to move. Keep your hands away from the wheels.

Day 3 wrap-up

What you built today: The full loop is now complete. You started the day understanding what an LLM is, learned how tool calls connect language to action, built a voice chatbot, and then gave the LLM control of a physical robot. Your robot can now hear, think, speak, and act. But it is still blind. It can hear you and respond, but it cannot see what is in front of it. Preview for tomorrow: On Day 4, the robot’s camera feed goes directly into a vision language model. It will finally be able to see and understand its surroundings — reading signs, identifying objects, and describing what it sees. The brain gets eyes.