Build Your Own AI Agent for Free with Ollama & Python

How to Build Your Own Agentic AI for Free Using Ollama, Qwen3 and Python

Want to build your own AI assistant without paying for expensive AI APIs? You can build a powerful local AI agent using free and open-source tools such as Ollama, Qwen3 and Python.

In this detailed guide, we will build an agent step by step and gradually turn a simple local chatbot into an agentic AI assistant capable of using tools, remembering information, reading files and searching the web.

Build your own AI agent for free using Ollama, Qwen3 and Python
Build your own AI agent for free using Ollama, Qwen3 and Python.

What Is an Agentic AI?

A normal AI chatbot mainly follows a simple process: the user asks a question, the AI processes it and generates an answer.

You
 ↓
AI
 ↓
Answer

An agentic AI goes further. Instead of only generating text, an AI agent can determine what actions are required to complete a task and use external tools when necessary.

You
 ↓
AI Agent
 ↓
Understand the task
 ↓
Plan
 ↓
Choose a tool
 ↓
Execute the tool
 ↓
Read the result
 ↓
Continue reasoning
 ↓
Final answer

For example, if you ask an agent to analyze a document, it could locate the document, read its contents, process the information and then provide a summary.

What We Are Going to Build

Our project will gradually evolve into a personal AI assistant with capabilities such as:

✓ Local AI model

✓ Python agent

✓ Tool calling

✓ Calculator

✓ Persistent memory

✓ Local file access

✓ Web search

✓ Document processing

✓ Python/code execution

✓ Browser automation

How the System Works

                         YOU
                          │
                          ▼
                   ┌─────────────┐
                   │  AI AGENT   │
                   │   QWEN 3    │
                   └──────┬──────┘
                          │
          ┌───────────────┼────────────────┐
          │               │                │
          ▼               ▼                ▼
       MEMORY           FILES             WEB
       SQLite           Local           Search
          │               │                │
          └───────────────┼────────────────┘
                          │
                          ▼
                     TOOL RESULT
                          │
                          ▼
                     AI ANALYSIS
                          │
                          ▼
                       ANSWER

Requirements

Before starting, you need a computer capable of running a local language model. The exact hardware requirement depends on the model you choose.

Requirement Recommendation
Operating System Windows, Linux or macOS
RAM 8 GB minimum; 16 GB or more recommended
Storage Enough free space for the selected AI model
Python Python 3
Internet Required for downloading models and web search

Step 1 — Install Ollama

1 Download Ollama

Download and install Ollama on your computer.

After installation, open PowerShell or Command Prompt and run:

ollama --version

If Ollama is installed correctly, it will display the installed version.

Step 2 — Install Qwen3

2 Download the AI Model

For this tutorial, we can use Qwen3 8B.

ollama run qwen3:8b

The first run downloads the model. After the download is complete, Ollama will start an interactive chat.

Test it with:

Hello. Who are you?
Tip: If Qwen3 8B runs slowly on your computer, use a smaller Qwen3 model such as 4B or 1.7B.

Step 3 — Create the Python Agent

3 Create the Project Folder

mkdir MyAgent
cd MyAgent

Create a Python virtual environment:

python -m venv .venv

Activate it:

.\.venv\Scripts\Activate.ps1

Install the Ollama Python library:

pip install -U ollama

Create agent.py

Create the main Python file:

notepad agent.py

Add this basic agent:

from ollama import chat

print("My AI Agent")
print("Type 'exit' to quit.")

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

    if user_input.lower() == "exit":
        break

    response = chat(
        model="qwen3:8b",
        messages=[
            {
                "role": "user",
                "content": user_input
            }
        ]
    )

    print("Agent:", response.message.content)

Start the agent:

python agent.py

Step 4 — Add Tools

An AI agent becomes significantly more useful when it can call external functions to perform tasks.

A simple example is a calculator:

def calculator(expression):
    allowed = "0123456789+-*/().% "

    if not all(char in allowed for char in expression):
        return "Invalid calculation."

    return str(eval(
        expression,
        {"__builtins__": {}},
        {}
    ))

The agent can follow this basic workflow:

User request
     ↓
AI decides whether a tool is required
     ↓
Tool call
     ↓
Tool result
     ↓
AI processes result
     ↓
Final response
Important: For a more reliable implementation, use Ollama's structured function/tool calling rather than relying on manually parsing text such as TOOL: calculator.

Step 5 — Add Persistent Memory

A useful personal AI should be able to remember information between sessions. Python includes SQLite, making it possible to add a lightweight local database without installing a separate database server.

import sqlite3

db = sqlite3.connect("memory.db")

db.execute("""
CREATE TABLE IF NOT EXISTS memories (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    memory TEXT
)
""")

db.commit()

Memory can be stored using:

def save_memory(memory):
    db.execute(
        "INSERT INTO memories (memory) VALUES (?)",
        (memory,)
    )
    db.commit()

Step 6 — Give the Agent File Access

Create a dedicated folder:

mkdir files

Your project can look like this:

MyAgent
│
├── agent.py
├── memory.db
│
└── files
    ├── test.txt
    ├── notes.txt
    └── document.txt

The agent can list files using Python:

import os

def list_files():
    return os.listdir("files")

A basic text-file reader can be created with:

def read_file(filename):
    safe_name = os.path.basename(filename)
    path = os.path.join("files", safe_name)

    with open(path, "r", encoding="utf-8") as file:
        return file.read()
Security warning: Do not give your agent unrestricted access to your entire computer. Start with a dedicated directory and carefully control what operations the agent can perform.

Step 7 — Add Web Search

A local language model cannot automatically know information that has appeared on the Internet after its training data. Adding a web-search tool allows the agent to retrieve current information.

Install the DDGS package:

pip install ddgs

Create the search function:

from ddgs import DDGS

def web_search(query):
    results = DDGS().text(
        query,
        max_results=5
    )

    output = []

    for result in results:
        output.append(
            f"Title: {result.get('title')}\n"
            f"URL: {result.get('href')}\n"
            f"Description: {result.get('body')}\n"
        )

    return "\n".join(output)

Now the agent can potentially search for current information such as news, software releases, tutorials and other web content.

Example:

Search the web for the latest developments in local AI models and summarize the important points.

The Complete Agent Architecture

                         ┌───────────────┐
                         │     USER      │
                         └───────┬───────┘
                                 │
                                 ▼
                      ┌──────────────────┐
                      │    AI AGENT      │
                      │      QWEN3       │
                      └────────┬─────────┘
                               │
                ┌──────────────┼──────────────┐
                │              │              │
                ▼              ▼              ▼
            MEMORY           FILES           WEB
            SQLite           Local          Search
                │              │              │
                └──────────────┼──────────────┘
                               │
                               ▼
                         TOOL RESULTS
                               │
                               ▼
                         AI REASONING
                               │
                               ▼
                           RESPONSE

Why Run the AI Locally?

Advantage Explanation
No AI API bill The local model can run without paying a commercial AI API for every request.
Privacy Your conversations and local files can remain on your own computer.
Customization You control the agent's Python code, tools and memory.
Local automation Python allows the agent to interact with local files and other software.

Is It Really Completely Free?

The core software stack can be used without paying for a commercial AI API, but there can still be indirect costs.

  • You need a computer.
  • Running AI models consumes electricity.
  • Internet access may cost money.
  • Some external services can have limits or paid plans.
Best approach: Keep the AI model and core processing local, and only use external services when they are actually required.

What We Can Add Next

The current agent is only the foundation. It can be expanded with many more capabilities.

🚀 PDF reading

🚀 DOCX support

🚀 Excel/XLSX processing

🚀 Image understanding

🚀 Python code execution

🚀 Browser automation

🚀 Voice input and output

🚀 Better long-term memory

🚀 RAG and vector databases

🚀 Scheduled tasks

🚀 Multiple specialized agents

🚀 Web dashboard

Single Agent vs Multi-Agent System

As the project becomes more advanced, you can divide responsibilities among multiple specialized agents.

                         MASTER AGENT
                              │
              ┌───────────────┼───────────────┐
              │               │               │
              ▼               ▼               ▼
          RESEARCHER        CODER           WRITER
              │               │               │
              ▼               ▼               ▼
          Web Search       Python          HTML/SEO
              │               │               │
              └───────────────┼───────────────┘
                              ▼
                         FINAL RESULT

For example, the researcher could collect information, the coding agent could process data and the writing agent could turn the results into an article.

Example of a Future Personal AI

Once the different capabilities are combined, you could give your personal AI a complex request such as:

"Research how to turn an old Android phone into a server, compare the available methods, create an SEO-optimized article, generate the HTML and save the finished article in my project folder."

A more advanced agent could break that request into multiple tasks:

Understand request
       ↓
Create plan
       ↓
Search web
       ↓
Collect information
       ↓
Analyze information
       ↓
Write article
       ↓
Generate HTML
       ↓
Save file
       ↓
Report completion

Important Security Considerations

Agentic AI is more powerful than a simple chatbot because it can perform actions. Therefore, security should be considered from the beginning.

  • Do not give the agent unrestricted administrator access.
  • Restrict file access to specific directories.
  • Be extremely careful when allowing shell commands.
  • Require confirmation for destructive operations.
  • Keep passwords and API keys away from directories accessible to the agent.
  • Review browser automation permissions.
  • Back up important files before allowing automated modification.

Frequently Asked Questions

Can I build an AI agent for free?

Yes. A local setup using Ollama, an open model and Python can be created without paying for a commercial AI API. External services may have their own limits or costs.

Can Qwen3 run on a normal laptop?

Yes. Smaller Qwen3 variants can run on many modern computers. The best model size depends on your RAM, processor and GPU.

Does a local AI need Internet access?

The local language model itself can run without Internet access. However, features such as web search require an Internet connection.

Can I make the agent read PDFs?

Yes. PDF processing can be added as another tool, allowing the agent to extract and analyze text from PDF documents.

Can an AI agent control my computer?

It is possible to provide controlled browser and computer automation tools. However, unrestricted computer control is not recommended. Sensitive actions should require explicit confirmation.

Conclusion

Building your own agentic AI does not necessarily require an expensive cloud AI subscription. With Ollama, Qwen3 and Python, you can create a local foundation for a powerful personal AI assistant.

The key difference between a normal chatbot and an AI agent is its ability to work with tools, memory, data and actions.

Starting with a simple local chatbot, you can gradually add calculators, persistent memory, file access, web search, document processing, browser automation and many other capabilities.

Next step: Build proper structured tool calling and add Python execution, PDF processing, better memory and browser automation to turn this basic prototype into a much more capable personal AI assistant.

Note: Software packages, model versions and APIs can change over time. Always check the relevant official documentation before using an AI agent for important or sensitive tasks.

Post a Comment

Previous Post Next Post