Learn Claude Code
s03

TodoWrite

Planning & Coordination

Plan Before You Act

177 LOC5 toolsTodoManager + nag reminder
Overview

跑长任务时 Claude 自己写一份结构化清单——每条待办、当前状态、做完打勾。这份清单是写到内存里的一份工作板,模型每隔几轮回头看一眼,避免任务长到一半忘了前面还有什么没做。

Running example
Research project
耐心资本对企业 ESG 表现的实证项目
What happens in this section

phase 1 要按依赖顺序跑完 do0_setup → do1_esg_import → do2_csmar_vars → do3_myopia_merge → do4_panel_assemble → do5_desc_corr → do6_baseline 七个 Stata 脚本,每个脚本跑完打勾、报告关键产物(PC 七合一 76305 obs → 合并 → 缩尾 → 剔金融 J → 剔 ST → 关键变量非缺失 → 控制变量 1%/99% winsor → 最终 26441 obs),脚本之间有严格的数据依赖顺序。

s01 > s02 > [ s03 ] s04 > s05 > s06 | s07 > s08 > s09 > s10 > s11 > s12

"An agent without a plan drifts" -- list the steps first, then execute.

Harness layer: Planning -- keeping the model on course without scripting the route.

Problem

On multi-step tasks, the model loses track. It repeats work, skips steps, or wanders off. Long conversations make this worse -- the system prompt fades as tool results fill the context. A 10-step refactoring might complete steps 1-3, then the model starts improvising because it forgot steps 4-10.

Solution

+--------+      +-------+      +---------+
|  User  | ---> |  LLM  | ---> | Tools   |
| prompt |      |       |      | + todo  |
+--------+      +---+---+      +----+----+
                    ^                |
                    |   tool_result  |
                    +----------------+
                          |
              +-----------+-----------+
              | TodoManager state     |
              | [ ] task A            |
              | [>] task B  <- doing  |
              | [x] task C            |
              +-----------------------+
                          |
              if rounds_since_todo >= 3:
                inject <reminder> into tool_result

How It Works

  1. TodoManager stores items with statuses. Only one item can be in_progress at a time.
class TodoManager:
    def update(self, items: list) -> str:
        validated, in_progress_count = [], 0
        for item in items:
            status = item.get("status", "pending")
            if status == "in_progress":
                in_progress_count += 1
            validated.append({"id": item["id"], "text": item["text"],
                              "status": status})
        if in_progress_count > 1:
            raise ValueError("Only one task can be in_progress")
        self.items = validated
        return self.render()
  1. The todo tool goes into the dispatch map like any other tool.
TOOL_HANDLERS = {
    # ...base tools...
    "todo": lambda **kw: TODO.update(kw["items"]),
}
  1. A nag reminder injects a nudge if the model goes 3+ rounds without calling todo.
if rounds_since_todo >= 3 and messages:
    last = messages[-1]
    if last["role"] == "user" and isinstance(last.get("content"), list):
        last["content"].insert(0, {
            "type": "text",
            "text": "<reminder>Update your todos.</reminder>",
        })

The "one in_progress at a time" constraint forces sequential focus. The nag reminder creates accountability.

What Changed From s02

ComponentBefore (s02)After (s03)
Tools45 (+todo)
PlanningNoneTodoManager with statuses
Nag injectionNone<reminder> after 3 rounds
Agent loopSimple dispatch+ rounds_since_todo counter

Try It

cd claude-code-for-researchers
python agents/s03_todo_write.py
  1. Make a todo list of do0_setup → do6_baseline (7 scripts) and run them in order
  2. For phase 2, add tasks: do7 IV/PSM → do8 mechanism → do9 heterogeneity → do10 robustness with dependencies
  3. Audit "耐心资本" usage in 07_论文写作/ against the protected-term list, write the report to audit-protected-terms.md