Learn Claude Code

Learn Claude Code

從 0 到 1 構建 nano Claude Code-like agent,每次只加一個機制

核心模式

所有 AI 程式設計 Agent 共享同一個迴圈:呼叫模型、執行工具、回傳結果。生產級系統會在其上疊加策略、許可權和生命週期層。

agent_loop.py
while True:
    response = client.messages.create(messages=messages, tools=tools)
    if response.stop_reason != "tool_use":
        break
    for tool_call in response.content:
        result = execute_tool(tool_call.name, tool_call.input)
        messages.append(result)

訊息增長

觀察 Agent 迴圈執行時訊息陣列的增長

messages[]len=0
[]

學習路徑

20 個漸進式課程,從簡單迴圈到完整多 Agent Harness

s01102

The Agent Loop

The smallest useful agent is a loop that calls the model, runs tools, and feeds results back.

s02135

Tool Use

The loop stays stable while capabilities register into a dispatch table.

s03175

Permission

Dangerous actions need a harness decision point before the shell runs.

s04228

Hooks

Cross-cutting behavior belongs around the loop, not tangled inside it.

s05235

TodoWrite

Explicit plans keep long-running work visible and correctable.

s06303

Subagent

Subagents give each subtask a clean message history while preserving the main thread.

s07334

Skill Loading

Inject specialized knowledge only when the task actually needs it.

s08414

Context Compact

Compression keeps the conversation usable when the context window gets crowded.

s09528

Memory

Some facts should survive summarization and future sessions.

s10165

System Prompt

The system prompt is a generated product of policy, tools, skills, and context.

s11287

Error Recovery

A robust harness classifies failures and decides what kind of retry is worthwhile.

s12299

Task System

A task graph turns vague goals into ordered, observable work.

s13381

Background Tasks

The agent can keep reasoning while slow work completes elsewhere.

s14645

Cron Scheduler

Recurring work should be created by the harness, not remembered by the model.

s15786

Agent Teams

Persistent teammates let work continue in parallel without stuffing every thought into one context.

s16711

Team Protocols

Multi-agent systems need explicit message contracts, not vibes.

s17649

Autonomous Agents

Teammates become useful when they can discover and claim work themselves.

s18810

Worktree Isolation

Parallel agents need isolated filesystems as much as isolated conversations.

s19837

MCP Tools

External services can become agent tools through a standard discovery and call protocol.

s201708

Comprehensive Agent

The final harness is still one loop, now surrounded by the systems that make it production-shaped.

h010

CLAUDE.md

AI doesn't disobey — it guesses when you leave blanks. A written manual removes the guessing.

h020

Verification

Fluent text is not the same as correct output. Define 'done' before the task starts.

h030

Memory

Memory lives in your files, not in AI's head. A three-part handoff note bridges any conversation gap.

h040

Hooks

CLAUDE.md is a guide. A hook is a guard. If 5% failure is unacceptable, enforce it.

h050

Layered Governance

AI ignores the second half of a 200-line rule file. Split into global / task / personal layers.

h060

Integration

Start with a minimum viable harness — manual, checklist, handoff note — and add pieces only when needed.

架構層次

五個正交關注點組合成完整的 Agent