Agents
First decide whether you need an agent at all, then write one from scratch: tool design, planning and self-checking, memory, multiple agents, MCP and security, and finally teach RepoBot to read the source on its own.
Lessons
- 01Agents and workflows: decide whether you need an agent first
An agent lets the model decide what to do next. For the same three questions, a fixed workflow got them right in 1 call; the agent needed 3 to 4. The difference between the two, what agents cost, and a checklist for deciding whether to use one.
30 minutes · Intermediate - 02Writing an agent loop by hand
With no framework, write an agent in a little over a hundred lines: tool registration, the call loop, stop conditions, error handling. Test it offline first with a fake model that follows a script, then switch to a real model and watch it decide for itself what to look up.
50 minutes · Intermediate - 03How to design tools
With the same three tools, vague names and descriptions let the model choose correctly only 14 times out of 30; written clearly, it got all 30 right. How to write a tool's name, description, parameters, return value and error messages.
35 minutes · Intermediate - 04Planning and self-checking
One multi-step task, three approaches compared: just do it, plan first then do it, and check the result afterwards. Planning first doubled the cost for about the same result; the self-check really did find two citation errors, but cost four times as much.
40 minutes · Intermediate - 05Memory
An agent's short-term memory is its message list; long-term memory has to be stored by you and retrieved when needed. Give the agent "remember" and "recall" tools, and watch it use a fact it remembered last time in a brand-new conversation.
35 minutes · Intermediate - 06Several agents working together
A supervisor agent splits a task, hands it to three worker agents with independent contexts running in parallel, then combines the results. Compared with a single agent, it used a third fewer tokens, and the supervisor's context was only 472 tokens. What multi-agent systems are really worth, and what they cost.
40 minutes · Intermediate - 07MCP: a standard socket for connecting tools to agents
Write a minimal MCP server with the official Python SDK containing two tools for searching the httpx docs; then write a client that connects to it, and have DeepSeek call those tools through MCP to answer a question.
45 minutes · Intermediate - 08Agent security
A real, reproducible indirect prompt injection experiment, with "instructions" for the AI hidden in a web page. One model never fell for it; the other still did 4 times even with the prompt explicitly forbidding it. Only a confirmation step enforced in the program stopped every attempt.
45 minutes · Intermediate - 09Project: a Q&A assistant that reads the source
Turn RepoBot into an agent: search the docs first, and if the answer isn't there, dig through the httpx source. Defaults and exception logic that v2 couldn't answer, v3 gets right, citing the source file and line number.
60 minutes · Intermediate
An agent is a loop: the model decides what to do next, the program carries it out, the result goes back to the model, and this repeats until the model thinks it can answer. This module uses no framework. It starts from that loop and adds, piece by piece, what a real system needs.
Every lesson has a comparison experiment, and the results aren't always "the more advanced method wins": for the same questions, a fixed workflow got them right in 1 call while the agent spent 4 times as much; planning first doubled the cost without changing the conclusion; and writing clear tool descriptions raised the rate of choosing the right tool from 47% to 100%.
Why this order
Lesson 1 first answers "should you use one at all", the most important decision. Lesson 2 writes the loop by hand, and every later lesson builds on it. Lessons 3 to 5 each cover one of the three things that most affect how well an agent works: its tools, how it goes about the work (planning and checking), and memory and context. Lesson 6 extends from one agent to several. Lesson 7 covers MCP, so the tools you write can be used by all kinds of agents. Lesson 8 covers security: once an agent can act, security has to be discussed. Lesson 9 puts all of this into RepoBot.
You're done when
- Given a requirement, you can decide whether it needs one call, a workflow or an agent, and explain why.
- Without a framework, you can write an agent loop with tool registration, error handling and a step limit, and test it offline with a scripted model.
- You can write clear descriptions for a set of tools, and verify with an experiment how often the model picks the right one.
- You can write a minimal MCP server and plug its tools into your own agent.
- You can explain how indirect prompt injection happens, and why "warning the model in the prompt" isn't enough and limits have to be enforced in the program.
- RepoBot v3 can answer questions whose answers exist only in the source code, citing the source file and line number.
Code for this module
Code and program output are shown exactly as they ran, so comments and printed output are in Chinese.
- code/05-agents/agent_loop.py
- code/05-agents/agent_security.py
- code/05-agents/mcp_client.py
- code/05-agents/mcp_server.py
- code/05-agents/memory.json
- code/05-agents/memory.py
- code/05-agents/multi_agent.py
- code/05-agents/planning_reflection.py
- code/05-agents/tool_design.py
- code/05-agents/workflow_vs_agent.py
- projects/repobot/v3/agent.py
- projects/repobot/v3/eval_agent.py
- projects/repobot/v3/eval_qa.jsonl
- projects/repobot/v3/llm.py
- projects/repobot/v3/README.md
- projects/repobot/v3/repobot.py
- projects/repobot/v3/requirements.txt
- projects/repobot/v3/retrieval.py
- projects/repobot/v3/tools.py