SSD Nodes Learn 8GB RAM — $66/yr
How to do am Matt ConnorBy Matt Connor · Updated 2026-08-02

How to Learn AI Agents from Scratch, Step by Step

Learn AI agents from scratch with six build stages: concepts, your own loop, tools, memory, loop design, and safety. Build one working thing at every stage.

Di stage wey get six steps

To learn AI agents from scratch, follow six stages one after another: di concepts, your first loop, tools, memory, loop design, and safety. For each stage, you go build one thing with your own hands. If you jump ahead, na common reason people stop, because framework go hide exactly di part wey you need see.

An AI agent na loop around a language model wey fit call tools. Na only this sentence describe di whole subject. Everything after am na detail about wetin dey enter di loop, wetin di tools fit touch, and how you go stop di loop if e start fail. If you fit explain di loop to another person, you don learn di thing. If you only fit name frameworks, you never learn am.

Di plan below assume say you learn by building. Read one stage, build di small thing, break am on purpose, then continue. If you only read one stage, you never do am.

Wetin you really need before stage 1

The honest prerequisite list short, and e short pass wetin most course pages dey suggest.

  • You fit read and write Python or TypeScript for the level of a fifty line script.
  • You dey comfortable for Linux shell: install a package, edit a file, read a log.
  • You get an API key for a hosted model, or a machine wey fit run local one.

Na the whole list be that. You no need machine learning theory, and you no need don train any model before. Nothing for agent work involve gradients or training data. Graphics card only matter if you decide to run the model by yourself. That one na separate skill wey you fit learn later from hosting Ollama for VPS to self host an LLM.

Wetin people dey underestimate na the shell side. Agents dey fail because of permissions, paths, environment variables, and processes wey dey die quietly. If stack trace about PATH or file mode make you close the terminal, spend one weekend learn Linux basics first. E go save you one month later.

Stage 1: agent wetin be, and wetin e no be

Start with one API call and no loop. Send one prompt, print the reply, then check the token counts for the response. Now you understand the unit of cost and the unit of latency.

Then learn tool use, wey be the only truly new idea for the whole field. You describe one function to the model as a name, description, and JSON (JavaScript object notation) schema for its inputs. The model no dey run anything. E replies with one structured request: call run_command with these arguments. Your code runs the function, sends the output back as a message, and asks the model again. The model na planner wey dey read text and write text. Your code na the thing wey get hands.

Chatbot dey end after one reply. Agent dey repeat that exchange until the model stop asking for tools. That repetition na the whole difference, and na why the failure modes dey differ too. Chatbot fit give wrong answer once. Agent fit act on wrong answer several times before anybody notice.

Stage 2: write the loop yourself, once

No start with framework. Write about thirty lines of Python so the shape of the thing na your own.

sudo apt update && sudo apt install -y python3-venv
python3 -m venv ~/agent
source ~/agent/bin/activate
pip install anthropic
export ANTHROPIC_API_KEY=your-key-here
import subprocess
import anthropic

client = anthropic.Anthropic()

tools = [{
    "name": "run_command",
    "description": "Run a read only shell command and return its output.",
    "input_schema": {
        "type": "object",
        "properties": {"command": {"type": "string"}},
        "required": ["command"],
    },
}]

messages = [{"role": "user", "content": "How much disk space is free here?"}]

while True:
    response = client.messages.create(
        model="claude-opus-5",
        max_tokens=4096,
        tools=tools,
        messages=messages,
    )
    if response.stop_reason != "tool_use":
        break
    messages.append({"role": "assistant", "content": response.content})
    results = []
    for block in response.content:
        if block.type == "tool_use":
            done = subprocess.run(
                block.input["command"], shell=True,
                capture_output=True, text=True, timeout=10,
            )
            results.append({
                "type": "tool_result",
                "tool_use_id": block.id,
                "content": done.stdout or done.stderr,
            })
    messages.append({"role": "user", "content": results})

print(next(b.text for b in response.content if b.type == "text"))

Run am with python3 agent.py. If everything dey work well, e go print one paragraph wey name your filesystems and their free space, because the model ask for df -h, your code run am, and the second pass turn that table into one sentence. If e no print anything, the loop end before text block show. Add print(response.stop_reason) inside the loop and watch how the values dey change.

Now break am on purpose. Delete the tool_use_id line and read the error, because API dey reject tool result wey no get matching id, and na the commonest beginner bug. Ask question wey need two commands and watch the loop run two times. Ask something impossible and watch whether e give up or spin forever.

One warning about this example. E pass model output straight into shell with shell=True. This one fit work for scratch machine wey you fit rebuild, but e wrong everywhere else. Stage 6 go fix am. The concepts under the loop dey explain with more detail for how to build your own AI agent on a VPS.

Stage 3: tools wey agent never get before

Your run_command tool dey work, but real agent need tools wey fit reach outside the box: ticket system, database, repository. To write special wrapper for every service and every agent no dey scale.

Model Context Protocol (MCP) na the answer wey industry don settle on. MCP server dey expose set of tools through standard transport, and any MCP aware agent fit use am without custom glue. Reference filesystem server na one command:

npx -y @modelcontextprotocol/server-filesystem /home/you/projects

Node must dey installed for this to work, and directory argument na the only path wey server go touch. Na this be the small version of the security model: server dey decide the boundary, no be model. Point client to am, and your agent gain file reading and writing wey you never write yourself. How to run these tools properly under service account, with the transport choices explained, dey covered for how to run MCP servers on VPS for AI coding agents.

The lesson for this stage be say tool design na the real work. Vague description go make model guess. Tool wey dey return forty thousand characters go poison context window. Tool wey fit delete things go eventually delete things.

Stage 4: memory, wey mostly na just files

Beginners dey rush go use vector database for here. No do am, at least not yet.

Agent no get memory between calls. You dey resend the whole conversation every time, na why long session dey cost more per turn pass short one. So memory split into two problems. The first one na wetin fit enter context window now. You manage am by summarising, trimming old tool output, and caching the stable prefix of your prompt, so you go pay only small part of the price for am. The second one na wetin go survive restart, and na storage be that.

For the second problem, ordinary markdown file wey agent fit read and write better pass vector database for almost every first project. Give am one file, tell am the format, tell am make e read the file before e start and update am when e learn something. You go get most of the benefit, and you fit open the file see wetin your agent believe. Use embeddings and retrieval when the notes no longer fit inside the context window, and no do am before then.

Stage 5: loop na di product

By now, you fit make agent wey dey work while you dey watch am. Stage 5 na to make am work when you no dey watch am.

Four questions dey decide whether e safe to leave agent unattended. Wetin go trigger am, so e no go run without reason. Wetin be the boundary wey e go operate inside, so mistake no go spread. How you go verify the result, because agent wey dey grade im own homework go always pass. Wetin be the budget wey go stop am, for tokens or wall clock time. To design these four things deliberately na the discipline wey loop engineering, and wetin dat definition cover describe.

The exercise: take your stage 2 agent, give am task wey need four or five steps, and add hard iteration cap. Then remove the cap and watch wetin unbounded loop go do to your token bill. Do am once with small budget, so you no go do am by mistake with big one.

Stage 6: safety, secrets, and cost

This stage no be optional, and e be last only because you no fit feel the risk until you don build something wey dey work.

Run the agent as im own unprivileged user. Never run am as root or as your own account. This one make the blast radius be one directory instead of the whole machine. Keep credentials away from the model, because anything wey dey inside the context window fit come out through a tool call. The fix na scoped short lived tokens behind a helper, as dem describe for how to keep secrets away from your AI agents. Set a hard limit on spending, because unattended loop go charge for every iteration when nobody dey monitor am. The caps and batching wey keep am under control dey for AI agent cost control on an always on VPS.

Cost need one clear number. As of July 2026, Claude Opus 5 dey charge $5 for every million input tokens and $25 for every million output tokens. A chatty agent wey dey resend a conversation as e dey grow fit push a few hundred thousand tokens through one task. Prompt caching and using a smaller model for routine steps go change the calculation much more than any prompt adjustment.

Prompt injection belong here too. If your agent reads a web page, an issue tracker, or an inbox, the person wey write that text dey also write instructions for your agent. The defence no be a more clever system prompt. Na the boundary, because an agent wey no fit delete a repository no fit be talked into deleting one.

Which map you suppose follow?

Choose one curriculum and finish am instead of sampling six. The Microsoft ai-agents-for-beginners repository na the most complete free one, an eighteen lesson course wey don pass 70,000 stars as of July 2026, and e match the stages above well. Roundups of trending agent repositories dey useful to see wetin dey available, but dem no too useful as syllabus, because list wey stars sort am dey sort by popularity, no be by teaching order.

When you want real project to practise with, coding agent na the best first target: feedback dey immediate, the tools clear, and mistakes cheap to undo. Run coding AI agent for VPS dey explain one end to end. If you prefer study working systems instead of building from zero, the comparison for the best self hosted AI agents dey show how several projects solve the same loop in different ways.

How long this one go take?

For person wey already dey program, stages 1 and 2 fit take one evening. Stage 3 fit take one weekend; most of the time go tool descriptions, no be protocol. Stages 4 and 5 fit take some weeks of real use, because na only when you watch your agent forget things you go learn wetin e dey forget. Stage 6 no really get final end, because every new capability wey you give am go open am again.

Two months of steady evening work fit help most people build agent wey dey work, get clear limits, and useful. People wey take one year usually na the ones wey continue to read instead of building.

FAQ

I need know machine learning before I fit build AI agent?

No. To build agent mean say you call model through API and connect the tool requests to real functions. Na normal application programming be that. You no dey touch training, gradients, or datasets. The skills wey decide whether your agent go work na tool schema design, error handling, and Linux permissions. Machine learning theory go matter only if you later fine tune model. That one na different work with different requirements.

Make I start with framework like LangChain or CrewAI?

Write one raw loop first, then adopt framework. Framework dey replace the thirty lines for stage 2 with configuration object. This convenient after you don understand wetin e replace, but e fit confuse you before then. When your agent misbehave, you need reason about the message list and tool results directly. This hard well well if you never see dem before. After you write one loop by yourself, framework go save you time instead of hiding the mechanism.

How much e go cost to learn AI agents?

E go cost less than most people expect, if you put limit. Hosted API key and small VPS dey cover everything for these six stages. The real risk no be the hourly rate. Na loop wey no get limit fit charge you for every iteration while you dey sleep. Set hard spend limit for your API account on day one. Add iteration cap to every loop wey you write. Use cheaper model for routine steps. If you run the model locally, you no go pay token bill, but you go need the required hardware.

Wetin be the difference between AI agent and chatbot?

Chatbot dey answer once. Agent dey repeat cycle: model request tool, your code run am, result go back, then model decide wetin to do next. Na this repetition dey make agent complete task with several steps. Na also why agents need boundaries wey chatbots no need. Wrong answer from chatbot na bad paragraph. Wrong answer from agent na bad paragraph plus anything wey e do about am.

#ai-agents#learning#curriculum#mcp#self-hosting