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.
messages = [ ]
- Call the model
- Execute the tool
- Put the result back into the conversation
- 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
| Stage | Content |
|---|---|
| Getting Started | What exactly is an Agent; setting up the environment and model |
| Building from Scratch | Function calling → Agent loop → real tools → memory → skills |
| Connecting to the Ecosystem | Write an MCP server and connect it; differences between MCP, Function Calling, A2A, and Skills |
| Frameworks and Shipping | How to choose among mainstream frameworks; safety; debugging and evaluation |
| Dissecting Real Projects | Take apart OpenClaw, Grok Bot, Codex, OpenCode, and goose with the same building blocks, and see how top projects do it |
| Quick Reference | Frequently asked questions |
The complete code is available for direct download: myagent.zip.
Start from What exactly is an Agent.