Skip to main content

Building a Minimal Agent from Scratch using Granite 3.3

2025-04-26

Agents is the new hype. Dozens of frameworks are popping up every day.

For me, in order to understand something, I love building it from scratch to get a feel of how it works. So I decided to build a minimal agent from scratch using Granite 3.3.

To be an agent, a model needs to have choice to perform certain actions in order to achieve a goal. In this case, I will give the agent the ability to call a magic function in order to let the user know what it returns for the number the user provides.

The full code is available in this Colab Notebook.


I've created a modified version using Groq, Tavily Search, and Riza in this Colab Notebook.


1. Building the Toolbox

Let's start by building our toolbox:

2. Defining the Response Schema

The response from the model can be either

  • a function call
  • a final response message to indicate that the agent is done
  • a tool call error
  • a regular message

Our execution loop should parse the response and interpret it accordingly.

3. Utility Methods for Parsing and Logging

Let's define some utility methods for parsing and logging

4. Loading the Model and Calling It

Let's use the snippet provided in the Granite 3.3 Model Card

Abstract the logic of calling the model into a function

Now let's define the system message that will be used to instruct the model on how to behave.

5. Defining the System Message

5.1 (Optional) Adding an Example for In-Context Learning

Let's also add an example for in-context learning

6. Defining the Agent Loop

Now let's define the agent loop that will execute the tool calls and print the results.

This is the core of any agent. You need to:

  • Give the model the choice to perform certain actions
  • Define the response schema, how to parse it.
  • Define the execution loop that will execute the tool calls and print the results.