Simple Decision-Making Agent in Python - Building Agents Part 1

Learn how to build a simple rule-based decision-making agent in Python. Perfect for beginners, this is Part 1 of the Building Agents series and includes code, logic flow, and real-world analogies.

Simple Decision-Making Agent in Python - Building Agents Part 1
Photo by Curtis Potvin / Unsplash

What Is an Agent?

An agent is something that can:

  1. Observe its environment
  2. Think based on rules or logic
  3. Act to solve a problem or achieve a goal

This pattern is found everywhere, from humans to AI systems to even your washing machine!

Real-World Analogy:

Imagine you're deciding what to eat. You look into the fridge (observe), see what ingredients you have (think), and choose to make an omelette (act).

That is how a simple agent works.


What Is a Simple Decision-Making Agent?

This type of agent uses rules or conditions to make decisions.

  • It does not learn or adapt
  • It’s fast and predictable
  • Perfect for situations where the rules are clear

Examples:

  • A thermostat: If temperature < 20°C, turn on the heater
  • A traffic light: If 60 seconds pass, switch the lights
  • A meal picker: If you're very hungry and have eggs, make an omelette

How the Agent Works

Process Overview on How the Agent works

Let’s Build One in Python!

Problem:

We want an agent to suggest a meal based on:

  • What ingredients do we have
  • How hungry we are (1 to 5)
  • How much time do we have

Rules:

  • If you have eggs + rice and time > 10 min → make egg fried rice
  • If you have bread + cheese + eggs and time ≤ 10 min → make a cheese omelette sandwich
  • If you have bread + jam and time ≤ 5 min → make bread and jam

Python Code:

class MealAgent:
    def __init__(self):
        self.recipes = [
            {
                "name": "Egg Fried Rice",
                "ingredients": ["rice", "eggs"],
                "time": 15,
                "filling": 4
            },
            {
                "name": "Cheese Omelette Sandwich",
                "ingredients": ["bread", "cheese", "eggs"],
                "time": 10,
                "filling": 5
            },
            {
                "name": "Bread and Jam",
                "ingredients": ["bread", "jam"],
                "time": 5,
                "filling": 2
            }
        ]

    def perceive(self, ingredients, hunger, time):
        self.ingredients = ingredients
        self.hunger = hunger
        self.time = time

    def reason(self):
        matches = []
        for recipe in self.recipes:
            if recipe["time"] <= self.time and all(i in self.ingredients for i in recipe["ingredients"]):
                matches.append(recipe)

        if not matches:
            return "Sorry, no suitable meal."

        matches.sort(key=lambda x: abs(x["filling"] - self.hunger))
        return matches[0]["name"]

    def act(self):
        suggestion = self.reason()
        print(f"👩‍🍳 Suggested Meal: {suggestion}")

How to Use It

agent = MealAgent()
agent.perceive(["bread", "cheese", "eggs"], hunger=5, time=10)
agent.act()

Output:

👩‍🍳 Suggested Meal: Cheese Omelette Sandwich

Comparison with Smarter Agents

Simple Agent

Uses fixed rules. It's fast, predictable, and easy to understand.

  • ❌ Cannot handle natural language input.
  • ✅ Very cheap to run and easy to debug.
  • ❌ Does not adapt or learn

GPT Agent

Uses a language model to understand messy, natural language input.

  • ✅ Understands vague inputs like “I’m kinda hungry”.
  • ❌ Doesn’t always explain why it made a choice.
  • 🔶 Slightly expensive depending on usage.
  • ❌ Doesn’t use rules; not always predictable

Clarifying Agent (Hybrid)

Combines GPT and rule-based logic.

  • ✅ Understands natural input via GPT.
  • ✅ Asks follow-up questions if data is unclear.
  • ✅ Uses rule-based reasoning once inputs are clear.
  • 🔶 More accurate and human-like, but higher cost and complexity

Recap

  • A simple agent is rule-driven and easy to build
  • Great for teaching or small tasks
  • In this article, we built a meal agent that suggests meals based on rules

Next up: We’ll build the same agent but with GPT understanding plain text like

"I'm kinda hungry and I have rice and eggs."

Read: LLM-Powered Agent in Python - Building Agents Part 2

Stay tuned!