Learn · Guide 3 of 3

Build my own AI agent

A chatbot talks. An agent does things: checks the time, reads a file, fills a form. Here is the simplest honest way to build one, with or without code, and how to keep it safe.

Free guide  Checked November 17, 2026. About 15 minutes to read. Code follows the official Ollama Python examples.

What an agent isThe three partsPath A: no codePath B: 25 lines of PythonLine by lineAdd a second toolSafety rulesTroubleshootingQuestionsSources

What is an agent, really?

An agent is an AI model plus tools plus a loop. You give it a goal; it decides which tool to use, looks at the result, and keeps going until it is done. The tools are ordinary programs: “get the time”, “search my notes”, “send a draft to me for approval”.

Think of a helpful assistant at a desk. The model is the assistant’s brain. The tools are the phone, the calculator and the filing cabinet. The loop is the habit of checking: “Did that answer the question? If not, what should I do next?”

The three parts, simply

Path A: no code

  1. Download our free OPC Desktop for Windows from Downloads. Pick a model, press Start, chat. It can use your browser and desktop when you allow it.
  2. Or use Open WebUI’s tools on your home AI brain to add web search and other helpers, switching each one on only when you need it.

Path B: your first agent in about 25 lines of Python

  1. Decide the one job. Pick a single small task, like “tell me what is on my calendar file today” or “summarise new notes in this folder”. Small jobs succeed; vague jobs wander.
  2. Get a model that can use tools. Install Ollama and pull a tool-capable model, for example ollama pull llama3.1. The Ollama library marks which models support tools.
  3. Install Python and the Ollama library. Python 3.10 or newer from python.org, then pip install ollama.
pip install ollama
ollama pull llama3.1

Save this as agent.py and run python agent.py:

import datetime, ollama

def get_time() -> str:
    """Get the current local date and time."""
    return datetime.datetime.now().strftime("%A %B %d, %Y %I:%M %p")

TOOLS = {"get_time": get_time}
messages = [{"role": "user", "content": "What day is it, and what time?"}]

for _ in range(8):                             # never loop forever
    reply = ollama.chat(model="llama3.1", messages=messages, tools=[get_time])
    messages.append(reply.message)
    if not reply.message.tool_calls:          # no tool needed: final answer
        print(reply.message.content)
        break
    for call in reply.message.tool_calls:     # run each tool the model asked for
        fn = TOOLS.get(call.function.name)
        result = fn(**(call.function.arguments or {})) if fn else "Tool not found"
        print("tool:", call.function.name, "->", result)   # simple log
        messages.append({"role": "tool", "content": str(result), "tool_name": call.function.name})

What each part does, line by line

  1. def get_time() is the tool. The text in triple quotes is its description; the model reads it to decide when to use it.
  2. TOOLS is a list the program uses to find the right function when the model asks for one by name.
  3. messages is the conversation so far. It starts with your question.
  4. for _ in range(8) is the loop, with a safety limit of 8 rounds so it can never run forever.
  5. ollama.chat(..., tools=[get_time]) sends the conversation and the tools to the model. The Ollama Python library turns your function and its description into a tool the model understands.
  6. If there are no tool calls, the model has its answer, so we print it and stop.
  7. Otherwise we run each requested tool, print a log line, and add the result to the conversation as a tool message, the same way Ollama’s own example does. Then the loop asks the model again.

Add a second tool

Give your agent a way to read a notes file. Add this function and put it in both TOOLS and the tools=[...] list:

def read_notes() -> str:
    """Read the text of my notes file, notes.txt, in this folder."""
    with open("notes.txt", encoding="utf-8") as f:
        return f.read()[:4000]   # keep it short for small models

Now ask: “What is on my list today, and what time is it?” The model should call both tools, then answer. That is a real agent: it chose two tools, used their results and finished the job. Notice the tool only reads. That is on purpose.

Useful first agents to build

Once the loop works, the only thing that changes from one agent to the next is the tools. Here are a few safe, genuinely useful ideas that only need read-only tools:

Notice the pattern: the model does the thinking and the talking, and the tools do anything that must be exact, like dates, sums or file contents. That split is what makes agents reliable.

Five safety rules we follow

  1. Start read-only. Tools that look things up come before tools that change things.
  2. Ask before acting. Anything that sends, buys, deletes or posts waits for your “yes”.
  3. Keep secrets out of prompts. Store passwords and keys in your system’s keychain, never in the chat.
  4. Log every action so you can see what it did and undo it.
  5. Prove it worked. “Done” means you can see the result, not that the AI said so.

Where to go from here

  1. Test with easy questions first. Ask things you already know the answer to. Watch which tools it calls and with what inputs.
  2. Add safety before adding power. Read-only tools first. Anything that sends, buys, deletes or posts must wait for a human “yes”.
  3. Log everything. Print or save every tool call and result so you can see what happened and undo it.
  4. Grow slowly. Add one tool at a time, test it alone, then together with the others.

Troubleshooting

What you seeMost likely causeWhat to do
The model never calls the toolThe model does not support tools, or the description is vagueUse a tool-capable model (for example llama3.1). Write a one-sentence docstring that says exactly when to use the tool.
KeyError for a tool nameThe model asked for a tool that does not existKeep the TOOLS dictionary names identical to the function names. Reply “tool not found” instead of crashing.
Wrong types, like “3012” instead of 42Tool arguments arrive as textConvert inputs inside the tool, as Ollama’s own example does with int(a) + int(b).
The loop runs foreverThe model keeps asking for toolsAdd a step limit, for example stop after 8 rounds, and print what happened.
Connection refusedOllama is not runningStart Ollama (open the app, or run ollama serve) and try again.
Answers are slowModel too big for your graphics cardRun ollama ps; aim for 100% GPU, or use a smaller model.

Questions people ask

What is the difference between a chatbot and an agent?

A chatbot only writes text. An agent can also use tools you give it, such as reading a file or checking the time, and it keeps going step by step until the job is done.

Do I need to code?

No for Path A: our free OPC Desktop and Open WebUI’s tools work without code. Yes, a little Python, for Path B, where you build one yourself and understand exactly what it does.

Which model should I use?

One that supports tool calling and fits your graphics card. The Ollama library labels tool-capable models. Bigger models choose tools more reliably; small ones are fine for simple tools.

Is it safe to let an agent use my computer?

Only with rules. Start with tools that only read. Make anything that changes, sends, buys or deletes ask you first. Never put passwords in prompts.

Why did my agent call the wrong tool or make up an answer?

Usually the tool description is unclear, or the model is too small. Make each description one plain sentence about exactly what the tool does, and check results instead of trusting the words “done”.

Can my agent use the internet?

Only if you give it a tool that does. Every internet tool is a door out of your home, so add it on purpose and log what it sends.

Can I run this on my home AI brain?

Yes. Point the script at your server by setting the Ollama host, or run the script on the server itself.

How is this different from big AI agent products?

The idea is the same: a model, tools and a loop. Products add many tools, memory and polish. Building a small one yourself is the best way to understand, and judge, the big ones.

Sources

Want one home for all your AI helpers? ABUZ8 HQ keeps your agents, files and conversations in one place. Sign-up is optional; every guide here stays free.

Get startedAll guides