Learn Claude Code

Learn Claude Code

Xây một nano agent kiểu Claude Code từ 0 đến 1, mỗi lần chỉ thêm một cơ chế

Mẫu cốt lõi

Mọi AI coding agent đều dùng cùng một vòng lặp: gọi mô hình, thực thi tool, đưa kết quả trở lại. Harness bổ sung chính sách, quyền, bộ nhớ, phối hợp và kiểm soát vòng đời quanh vòng lặp đó.

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)

Mảng tin nhắn lớn dần

Theo dõi mảng messages tăng lên khi agent loop chạy

messages[]len=0
[]

Lộ trình học

17 buổi học tuần tự, từ một vòng lặp đơn giản đến điều phối xác định và đóng mục tiêu

s01106 dòng

The Agent Loop

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

s02149 dòng

Tool Use

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

s03185 dòng

Permission

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

s04207 dòng

Hooks

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

s05284 dòng

TodoWrite

Explicit plans keep long-running work visible and correctable.

s06291 dòng

Subagent

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

s07306 dòng

Skill Loading

Inject specialized knowledge only when the task actually needs it.

s08503 dòng

Context Compact

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

s09679 dòng

Memory

Some facts should survive summarization and future sessions.

s10466 dòng

Task System

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

s11404 dòng

Background Tasks

The agent can keep reasoning while slow work completes elsewhere.

s12642 dòng

Cron Scheduler

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

s131592 dòng

Agent Team Runtime

Persistent teammates can reliably discover and execute parallel work when the runtime owns messaging, atomic claims, and task-bound working directories.

s14444 dòng

MCP Tools

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

s152770 dòng

Integrated Harness

The integrated harness is still one loop, surrounded by the systems introduced across the course.

s16725 dòng

Workflow Runtime

When orchestration has a fixed shape, code can make it parallel, deterministic, and resumable.

s17794 dòng

Goal Loop

A durable goal keeps the loop working until an independent evaluator finds the completion condition satisfied in the conversation.

Các tầng kiến trúc

Năm mối quan tâm độc lập ghép thành một agent hoàn chỉnh