Building AI Agents From First Principles: A Claude Code Tutorial
shareAI-lab/learn-claude-codeBash is all you need - A nano claude code–like 「agent harness」, built from 0 to 1
AI Analysis
Educational TypeScript project that teaches the core agent loop through 12 progressive sessions, starting with bash tool execution.
Built for Engineers curious about how coding agents work, particularly those wanting to understand the fundamental patterns before diving into production systems.
From the README
Learn Claude Code -- A nano Claude Code-like agent, built from 0 to 1
THE AGENT PATTERN
=================
User --> messages[] --> LLM --> response
|
stop_reason == "tool_use"?
/ \
yes no
| |
execute tools return text
append results
loop back -----------------> messages[]
That's the minimal loop. Every AI coding agent needs this loop.
Production agents add policy, permissions, and lifecycle layers.
12 progressive sessions, from a simple loop to isolated autonomous execution. Each session adds one mechanism. Each mechanism has one motto.
s01 "One loop & Bash is all you need" — one tool + one loop = an agent
s02 "Adding a tool means adding one handler" — the loop stays the same; new tools register into the dispatch map
s03 "An agent without a plan drifts" — list the steps first, then execute; completion doubles
s04 "Break big tasks down; each subtask gets a clean context" — subagents use independent messages[], keeping the main conversation clean
s05 "Load knowledge when you need it, not upfront" — inject via tool_result, not the system prompt
s06 "Context will fill up; you need a way to make room" — three-layer compression strategy for infinite sessions
s07 "Break big goals into small tasks, order them, persist to disk" — a file-based task graph with dependencies, laying the foundation for multi-agent collaboration
s08 "Run slow operations in the background; the agent keeps thinking" — daemon threads run commands, inject notifications on completion
s09 "When the task is too big for one, delegate to teammates" — persistent teammates + async mailboxes
s10 "Teammates need shared communication rules" — one request-response pattern drives all negotiation
s11 "Teammates scan the board and claim tasks themselves" — no need for the lead to assign each one
s12 "Each works in its own directory, no interference" — tasks manage goals, worktrees manage directories, bound by ID
The Core Pattern
def agent_loop(messages):
while True:
response = client.messages.create(
model=MODEL, system=SYSTEM,
messages=messages, tools=TOOLS,
messages.append({"role": "assistant",
"content": response.content})
if response.stop_reason != "tool_use":
return
res