Module 2 Wrap-up: Your First Minimal Agent
·Agentic AI

Module 2 Wrap-up: Your First Minimal Agent

Hands-on: Build a manual, single-step agent loop from scratch in Python.

Module 2 Wrap-up: Building the Loop

We have learned about the four pillars (Brain, Tools, Memory, Loop). Now, we are going to build a Manual Agent. We won't use LangChain or LangGraph yet; we want to see the "ugly" internal code that makes an agent works.


Hands-on Exercise: The Calculator Bot

We will build an agent that can solve math problems using a calculator tool. Why? Because LLMs are famous for being Bad at Math. By giving the LLM a tool, we "Fix" its brain.

1. The Setup

Ensure you have the ollama library installed: pip install ollama

2. The Code

Create a file named simple_agent.py:

import ollama
import json

# 1. THE TOOL (The "Hands")
def calculator(a, b, operation):
    if operation == "add": return a + b
    if operation == "multiply": return a * b
    return "Unknown operation"

# 2. THE CHAT HISTORY (The "Memory")
messages = [
    {
        "role": "system", 
        "content": """You are a math agent. If you need to solve a math problem, 
        you MUST output raw JSON in this format: {"action": "calculator", "a": 1, "b": 2, "op": "add"}.
        If you have the answer, respond with 'FINAL: {result}'."""
    }
]

def run_agent(user_input):
    messages.append({"role": "user", "content": user_input})
    
    # 3. THE CONTROL LOOP
    for i in range(3): # Limit to 3 turns
        response = ollama.chat(model='llama3', messages=messages)
        content = response['message']['content']
        print(f"--- Agent Thought: {content} ---")
        
        if "FINAL:" in content:
            print(f"Goal Reached: {content}")
            return
            
        # 4. TOOL INVOCATION (The "Glue")
        try:
            call = json.loads(content)
            if call["action"] == "calculator":
                result = calculator(call["a"], call["b"], call["op"])
                # Add the result to memory
                messages.append({"role": "assistant", "content": content})
                messages.append({"role": "user", "content": f"Observation: The result is {result}"})
                print(f"--- Executed Tool: {result} ---")
        except:
            print("Model failed to output JSON, retrying...")

# 5. TEST IT
run_agent("What is 1255 multiplied by 42?")

3. Run and Observe

Watch how the agent:

  1. Reads your request.
  2. Decides to use the calculator.
  3. Parses the math.
  4. Reads the tool result.
  5. Provides the final natural language answer.

Module 2 Summary

  • An agent is more than a prompt; it is a Pillar-based Architecture.
  • State management is the hardest part of building production agents.
  • Descriptions are the only way the LLM understands your code.
  • The Control Loop prevents the LLM from wandering and keeps it on task.

Coming Up Next...

In Module 3, we look at Design Patterns. We move beyond simple "Calculator" loops and learn how to build "Planner-Executor" systems and "Human-in-the-Loop" guardrails.


Module 2 Checklist

  • I can name the four pillars of an agent.
  • I understand why a tool needs a good description.
  • I have successfully run the simple_agent.py script.
  • I understand the difference between Stateless and Stateful agents.
  • I can identify where the "Control Loop" is in my code.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn