Build a Simple AI Agent in Python: Beginner Project with Tools & Memory - Solitaire Infosystems
Request a Quote !

Request a Quote !

Request a Quote !

Blogs

Build a Simple AI Agent in Python: Beginner Project with Tools & Memory

  • By: admin
  • date: Nov 17, 2026
Blog Image

To build a simple AI agent, you need three parts: a brain that decides what to do, tools it can call, and memory that keeps facts between sessions. This beginner project builds all three in about 60 lines of plain Python, with no API key, so you understand how agents work before you plug in a large language model.

Most “build an agent” tutorials start with a framework and five API keys. You end up with something that works but you can’t explain. Recruiters notice that in the first interview question. Here you write every line yourself.

 

What is an AI agent, in plain words?

 

An AI agent is a program that reads a request, chooses an action, carries it out with a tool or its memory, and replies with the result. A chatbot only talks; an agent acts.

                          Chatbot                               AI agent
 

What it does

 

                       Replies with text           Chooses and performs an action
 

Tools

 

                                   None           Calculator, search, APIs, files
 

Memory

 

                Forgets when the chat ends               Saves facts and reuses them
 

Example

 

              “What is 12 × 7?” → guesses                   Calls the calculator → 84

 

What do you need before you start?

 

  • Python 3.8 or newer and any code editor (VS Code works well)
  • Comfort with functions, dictionaries and while loops
  • About an hour for the first working version

How do you build a simple AI agent step by step?

 

Step 1: Give the agent memory

 

Memory here is a JSON file. The agent loads it when it starts and saves to it whenever it learns something, so it still knows your name after you close the program. Python’s built-in json module does all the work.

 

Step 2: Give it tools

 

A tool is just a function. This agent gets two: a calculator and a date lookup. They sit in a dictionary called TOOLS, so adding a third tool later is one line.

 

Step 3: Write the brain

 

The decide() function reads the message and returns an action: remember, recall, use a tool, or simply reply. For now it uses simple pattern rules. Later, this is the only function you replace with an LLM.

 

Step 4: Run the agent loop

 

The loop reads input, asks the brain what to do, runs that action and prints the result, until you type quit. That loop is the core of every agent, from this one to production systems.

 

The full code (agent.py)

 

import json
import re
from datetime import date
MEMORY_FILE = “memory.json”
# ———- Memory ———-
def load_memory():
    try:
        with open(MEMORY_FILE) as f:
            return json.load(f)
    except FileNotFoundError:
        return {}
def save_memory(memory):
    with open(MEMORY_FILE, “w”) as f:
        json.dump(memory, f, indent=2)
# ———- Tools ———-
def calculator(expression):
    if not re.fullmatch(r”[\d\s+\-*/().]+”, expression):
        return “I can only do basic arithmetic.”
    return str(eval(expression))  # safe here: only digits and operators pass
def today(_):
    return date.today().strftime(“%d %B %Y”)
TOOLS = {“calculator”: calculator, “today”: today}
# ———- Brain (rules now, an LLM later) ———-
def decide(text):
    t = text.strip()
    m = re.match(r”remember my (\w+) is (.+)”, t, re.I)
    if m:
        return (“remember”, m.group(1).lower(), m.group(2))
    m = re.match(r”what(?: is|’s) my (\w+)”, t, re.I)
    if m:
        return (“recall”, m.group(1).lower(), None)
    if t.lower().startswith(“calc “):
        return (“tool”, “calculator”, t[5:])
    if “date” in t.lower():
        return (“tool”, “today”, “”)
    return (“reply”, None, None)
# ———- Agent loop ———-
def run():
    memory = load_memory()
    print(“Agent ready. Type ‘quit’ to exit.”)
    while True:
        user = input(“You: “)
        if user.lower() == “quit”:
            break
        action, name, value = decide(user)
        if action == “remember”:
            memory[name] = value
            save_memory(memory)
            print(f”Agent: Got it, your {name} is {value}.”)
        elif action == “recall”:
            print(“Agent:”, memory.get(name, f”You haven’t told me your {name} yet.”))
        elif action == “tool”:
            print(“Agent:”, TOOLS[name](value))
        else:
            print(“Agent: I can remember facts, do maths (calc 12*7) or tell the date.”)
if __name__ == “__main__”:
    run()
Run it with python agent.py. A real session looks like this:
You: Remember my name is Aman
Agent: Got it, your name is Aman.
You: What is my name?
Agent: Aman
You: calc 12*7
Agent: 84
You: quit

 

Run it again and ask “What’s my name?”. It answers Aman, because memory.json survived the restart. That is the moment the project clicks for most students.

 

How do you turn this into an LLM-powered agent?

 

Replace the rules inside decide() with a call to a language model and describe your tools to it. This is called function calling or tool calling. As the OpenAI function calling guide explains, the model never runs your function itself: it returns which tool to call and with what arguments, your code runs it, and you send the result back. Your TOOLS dictionary and memory file stay exactly as they are.

 

What mistakes do beginners make with their first AI agent?

 

  • Running eval() on raw input. It executes any code a user types. The calculator above only allows digits and maths operators through.
  • Treating memory as an endless chat log. Store facts as key-value pairs instead, or the file grows and the agent slows down.
  • Adding ten tools on day one. Two tools that work reliably teach you more than ten that half work.
  • No fallback reply. If the brain doesn’t understand, the agent should say what it can do, not crash.
  • Pasting API keys into the code. Use environment variables before you push anything to GitHub.

 

Is a simple AI agent worth adding to your resume?

 

On its own, no. Extended, yes. Interviewers want to see that you made decisions, so take it further: connect an LLM, add a notes or weather tool, write a README that explains the agent loop, add two or three tests and record a 60-second demo. Now it is a project you can talk through for ten minutes, which is what a fresher interview really tests.

 

 

Frequently asked questions

 

Do I need to know machine learning to build an AI agent?

No. A simple AI agent is mostly regular programming: functions, a loop and a way to store data. You need machine learning knowledge only if you want to train your own models, which most agent projects never do.

 

Can I build an AI agent without an API key?

Yes. The rule-based version in this guide runs on plain Python with no account or API key. You add a language model later, when you want the agent to understand free-form questions.

 

What is the difference between an AI agent and a chatbot?

A chatbot replies with text. An AI agent decides on an action, such as calling a tool or saving a fact, carries it out, and uses the result. Memory and tools are what make it an agent.

 

Which programming language should beginners use for AI agents?

Python is the practical choice. It is easy to read, and the major model providers publish official Python SDKs, so moving from this project to an LLM-powered agent needs no new language.

 

How long does it take to build a simple AI agent?

The base version takes about one evening. Adding a language model, two or three useful tools, a README and tests usually takes another week of part-time work.

 

 

Where can you build AI projects with mentors in Mohali?

 

Solitaire Infosystems is a software development and IT training company at C-110, Phase 7, Industrial Area, Mohali. It runs job-oriented training in ML/AI, Python and other technologies in 4-week, 6-week and 6-month formats, where students work on projects with experienced developers reviewing their code. Final-year students who want industry experience can also apply for the stipend-based internship programme.

 

Want to build this with a mentor checking your code? Explore the AI and Python industrial training in Mohali, or talk to our training team on 98766 56700 / info@solitaireinfosystems.com.

About the Author

Comments