Learn Claude Code
Build an AI coding agent from scratch, one concept at a time
The Core Pattern
Every AI coding agent shares the same loop: call the model, execute tools, feed results back. Everything else is details.
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)Message Growth
Watch the messages array grow as the agent loop executes
Learning Path
12 progressive versions, from 13 lines to 1321 lines
The Agent Loop
The entire agent is a while loop + one tool
Tools
Adding tools means adding handlers, the loop stays the same
TodoWrite
Visible plans improve task completion and accountability
Subagents
Process isolation = context isolation
Skills
Skills inject via tool_result, not system prompt
Compact
Forgetting old context enables infinite-length sessions
Tasks
File-based state survives context compression
Background Tasks
Non-blocking daemon threads + notification queue
Agent Teams
Persistent teammates with async mailbox inboxes
Team Protocols
Same request-response pattern, two applications
Autonomous Agents
Polling + timeout makes teammates self-organizing
Architectural Layers
Five orthogonal concerns that compose into a complete agent