Skip to content

Write Your Own Agent from Scratch ​

The word "Agent" is everywhere now, but at its core it's just one loop: the model wants to call a tool → the program executes it → the result is fed back to the model → the model decides the next step, until it no longer needs any tools.

Below is a real run of the Agent built in this course on an ordinary computer, using a free local model. Click "Next" and watch the conversation grow line by line. Note step 3: it went down the wrong path, got an error, and corrected itself.

Step by stepThe agent loop: a real runModel:qwen3:8b (local Ollama)

messages = [ ]

  1. Call the model
  2. Execute the tool
  3. Put the result back into the conversation
  4. No tool calls → end

第 0 轮Wait for user input

What You'll Have After the Course ​

A command-line Agent you wrote yourself (agent.py, about 160 lines, with no dependency on any Agent framework):

  • Can list directories, read and write files, run commands, and fetch web pages; it asks you first before writing files or running commands
  • Remembers what was discussed before, and automatically compresses the conversation into a summary when it gets too long
  • Can use "skills": follows the steps written in SKILL.md, for example organizing work logs into a weekly report
  • Can connect to any MCP server and directly use tools written by others
  • Change one line of config to switch between local Ollama, DeepSeek, Tongyi Qianwen, Zhipu, or any other OpenAI-compatible API

After finishing this course, when you look at any Agent framework or product, you'll be able to see what it adds on top of this loop.

What You Need to Prepare ​

  • A little Python (being able to read functions and for loops is enough)
  • A computer. No API Key needed: running Qwen3 8B locally with Ollama is enough — every piece of code in this course was first run on a local model

Course Outline ​

StageContent
Getting StartedWhat exactly is an Agent; setting up the environment and model
Building from ScratchFunction calling → Agent loop → real tools → memory → skills
Connecting to the EcosystemWrite an MCP server and connect it; differences between MCP, Function Calling, A2A, and Skills
Frameworks and ShippingHow to choose among mainstream frameworks; safety; debugging and evaluation
Dissecting Real ProjectsTake apart OpenClaw, Grok Bot, Codex, OpenCode, and goose with the same building blocks, and see how top projects do it
Quick ReferenceFrequently asked questions

The complete code is available for direct download: myagent.zip.

Start from What exactly is an Agent.

gaodong.aiAboutPrivacyContact

Independent tutorials, not affiliated with any product mentioned. Product names and trademarks belong to their owners. Check official pages for prices and policies.