aiai agentollamapythonautomation

How to Create Your Own Free AI Agent (100% Free)

By Laraib Rabbani
Picture of the author
Published on
Free AI agent tutorial

How to Create Your Own Free AI Agent (100% Free)

Introduction

If you want to build your own AI agent without paying monthly fees, the easiest beginner path right now is to run everything on your own computer. Ollama offers a free tier at $0, runs models on your hardware, and exposes a local API. Ollama also supports tool calling, which is the feature that lets a model decide when to use functions such as math, note saving, or data lookup. (Ollama)

In this guide, you will build a simple local AI agent with Python and Ollama. Your agent will be able to chat, do calculations, save notes, and recall those notes later. This is a real agent, not just a chatbot, because it can choose tools and use their results before replying. (n8n Docs)

What Is an AI Agent?

A normal AI chat app mostly responds with text. An AI agent goes one step further. It can decide what action to take, call a tool, read the result, and then continue. Ollama’s tool calling docs describe this as function calling, where the model invokes tools and incorporates their results into its replies. (Ollama Docs)

For a beginner, that means your agent can do things like:

  1. answer normal questions
  2. calculate numbers with a tool
  3. store simple notes for later
  4. recall saved information when you ask for it

That is enough to understand the core idea before moving into more advanced agents later.

What You Are Building

You are going to build a local AI agent with three simple parts:

  1. Ollama, which runs the model on your computer and serves a local API at http://localhost:11434/api. (Ollama Docs)
  2. Python, which will hold your tool functions and the agent loop.
  3. A small notes file, so the agent can remember simple things between chats.

Step 1: Install Python

First, install Python from the official Python website if it is not already installed on your computer. Python is a general purpose language with a strong beginner ecosystem, and its official site provides the installer downloads. (Python.org)

Step 2: Install Ollama

Install Ollama using the official command for your system.

On Windows

irm https://ollama.com/install.ps1 | iex

On macOS or Linux

curl -fsSL https://ollama.com/install.sh | sh

Ollama’s official download pages provide those install commands, and the quickstart confirms that Ollama works on Windows, macOS, and Linux. (Ollama)

Step 3: Make Sure Ollama Works

Open your terminal and run:

ollama

The official quickstart says this opens the interactive menu, where you can run a model, launch tools, or access other integrations. (Ollama Docs)

Now download a model. Ollama’s CLI supports ollama pull, and Ollama’s tool calling examples use qwen3, so we will use that in this tutorial. (Ollama Docs)

ollama pull qwen3

You can also test that the model runs:

ollama run qwen3

Type a quick message, then exit with Ctrl + C when you are done. The CLI reference documents both ollama pull and ollama run. (Ollama Docs)

Step 4: Create a Project Folder

Create a new folder anywhere you like, then open a terminal inside it.

mkdir free-ai-agent
cd free-ai-agent

Now create a virtual environment.

On Windows

python -m venv .venv
.venv\Scripts\activate

On macOS or Linux

python3 -m venv .venv
source .venv/bin/activate

Install the Ollama Python package. The official tool calling docs show this exact package install step. (Ollama Docs)

pip install ollama -U

Step 5: Create Your Agent Script

Create a file named agent.py, then paste in this code:

from pathlib import Path
import json
from ollama import chat, ChatResponse

NOTES_FILE = Path("agent_notes.json")


def add(a: int, b: int) -> int:
    """Add two whole numbers."""
    return a + b


def multiply(a: int, b: int) -> int:
    """Multiply two whole numbers."""
    return a * b


def save_note(key: str, value: str) -> str:
    """Save a short note for later."""
    data = {}

    if NOTES_FILE.exists():
        try:
            data = json.loads(NOTES_FILE.read_text(encoding="utf-8"))
        except json.JSONDecodeError:
            data = {}

    data[key] = value
    NOTES_FILE.write_text(json.dumps(data, indent=2), encoding="utf-8")
    return f"Saved note for '{key}'."


def read_note(key: str) -> str:
    """Read a saved note."""
    if not NOTES_FILE.exists():
        return "No notes saved yet."

    try:
        data = json.loads(NOTES_FILE.read_text(encoding="utf-8"))
    except json.JSONDecodeError:
        return "Your notes file is broken or empty."

    return data.get(key, f"No note found for '{key}'.")


available_functions = {
    "add": add,
    "multiply": multiply,
    "save_note": save_note,
    "read_note": read_note,
}

messages = [
    {
        "role": "system",
        "content": (
            "You are a beginner friendly local AI agent. "
            "Use tools whenever they help. "
            "Use save_note when the user asks you to remember something. "
            "Use read_note when the user asks what you remember."
        ),
    }
]

print("Free Local AI Agent")
print("Type 'exit' to quit.\n")

while True:
    user_input = input("You: ").strip()

    if user_input.lower() in {"exit", "quit"}:
        print("Goodbye.")
        break

    if not user_input:
        continue

    messages.append({"role": "user", "content": user_input})

    while True:
        response: ChatResponse = chat(
            model="qwen3",
            messages=messages,
            tools=[add, multiply, save_note, read_note],
        )

        messages.append(response.message)

        if response.message.tool_calls:
            for tool_call in response.message.tool_calls:
                function_name = tool_call.function.name
                function_args = tool_call.function.arguments

                if function_name in available_functions:
                    result = available_functions[function_name](**function_args)
                    messages.append(
                        {
                            "role": "tool",
                            "tool_name": function_name,
                            "content": str(result),
                        }
                    )
        else:
            print(f"Agent: {response.message.content}\n")
            break

This follows the same core pattern shown in Ollama’s official tool calling examples: send messages to the model, let the model request a tool, execute the matching Python function, return the tool result to the model, and keep looping until there are no more tool calls. (Ollama Docs)

Step 6: Run Your AI Agent

Start the script:

python agent.py

Now test it with simple prompts like these:

remember my name is Brown
what is my name
what is 27 times 48
remember my favourite colour is green
what do you remember about me

If the model decides it needs a tool, it will call one. If not, it will answer directly.

How This Works

Here is the simple flow:

  1. You type a message.
  2. Python sends your message to Ollama.
  3. Ollama decides whether to answer directly or call a tool.
  4. If a tool is needed, Python runs the matching function.
  5. Python sends the tool result back to Ollama.
  6. Ollama writes the final answer.

That loop is the heart of an AI agent. Ollama’s docs call this an agent loop, where the model can make multiple tool calls before producing its final reply. (Ollama Docs)

Why This Is Actually 100% Free

This setup is free in the software sense:

  1. Ollama has a free tier at $0. (Ollama)
  2. The model runs on your own hardware. (Ollama)
  3. The API is local, so you do not need a paid external AI key just to get started. (Ollama Docs)
  4. The tool calling capability is built into Ollama. (Ollama Docs)

The only real cost is your own computer’s resources, such as RAM, storage, and processing power.

Beginner Tips

Keep your first agent small

Do not try to build an everything agent on day one. Start with three or four tools and make sure each one works well.

Use clear tool names

Names like save_note and read_note make it obvious what the agent should use.

Test one feature at a time

First test math. Then test memory. Then test mixed prompts.

Keep your notes simple

This tutorial stores notes in a small JSON file. It is enough for learning, and much easier to understand than databases or vector stores.

Troubleshooting

The script says it cannot connect to Ollama

Make sure Ollama is installed and running. Ollama serves its local API at http://localhost:11434/api. (Ollama Docs)

You can also test the local API directly:

curl http://localhost:11434/api/chat -d "{\"model\":\"qwen3\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello\"}]}"

The quickstart and API docs both show local chat requests through that API. (Ollama Docs)

The model feels slow

That usually means your computer is struggling with the model size. Try a smaller model from the Ollama library, or close other heavy apps while testing. Ollama also notes that larger settings and models require more memory. (Ollama)

What To Build Next

Once this works, you can improve it by adding:

  1. a get_time tool
  2. a save_task tool
  3. a list_tasks tool
  4. a weather or search tool
  5. a small web interface with Flask or FastAPI

That is how simple agents turn into real assistants.

Final Thoughts

The best part of this approach is that you are not renting somebody else’s assistant. You are building your own. It runs locally, uses your tools, and can be expanded step by step as your skills grow.

For a beginner, that is the ideal place to start.

Laraib Rabbani Newsletter

Want to Stay Connected With Brown?
The best articles, links and news related to web development delivered once a week to your inbox.