Learn Claude Code
Back To Learning Path
Deep Dive

Code Reading Order

When This Page Helps

Shows which local `agents/*.ts` files to open first and what state or functions to inspect before the code turns into noise.

Deep Dive -- Read this when you're about to open the Python agent files and want a strategy for reading them.

This page is not about reading more code. It answers a narrower question: once the chapter order is stable, what is the cleanest order for reading this repository's code without scrambling your mental model again?

Conclusion First

Do not read the code like this:

  • do not start with the longest file
  • do not jump straight into the most "advanced" chapter
  • do not open web/ first and then guess the mainline
  • do not treat all agents/*.py files like one flat source pool

The stable rule is simple:

read the code in the same order as the curriculum.

Inside each chapter file, keep the same reading order:

  1. state structures
  2. tool definitions or registries
  3. the function that advances one turn
  4. the CLI entry last

Why This Page Exists

You will probably not get lost in the prose first. You will get lost when you finally open the code and immediately start scanning the wrong things.

Typical mistakes:

  • staring at the bottom half of a long file first
  • reading a pile of run_* helpers before knowing where they connect
  • jumping into late platform chapters and treating early chapters as "too simple"
  • collapsing task, runtime task, teammate, and worktree back into one vague idea

Use The Same Reading Template For Every Agent File

For any agents/sXX_*.py, read in this order:

1. File header

Answer two questions before anything else:

  • what is this chapter teaching
  • what is it intentionally not teaching yet

2. State structures or manager classes

Look for things like:

  • LoopState
  • PlanningState
  • CompactState
  • TaskManager
  • BackgroundManager
  • TeammateManager
  • WorktreeManager

3. Tool list or registry

Look for:

  • TOOLS
  • TOOL_HANDLERS
  • build_tool_pool()
  • the important run_* entrypoints

4. The turn-advancing function

Usually this is one of:

  • run_one_turn(...)
  • agent_loop(...)
  • a chapter-specific handle_*

5. CLI entry last

if __name__ == "__main__" matters, but it should not be the first thing you study.

Stage 1: s01-s06

This stage is the single-agent backbone taking shape.

ChapterFileRead FirstThen ReadConfirm Before Moving On
s01agents/s01_agent_loop.pyLoopStateTOOLS -> execute_tool_calls() -> run_one_turn() -> agent_loop()You can trace messages -> model -> tool_result -> next turn
s02agents/s02_tool_use.pysafe_path()tool handlers -> TOOL_HANDLERS -> agent_loop()You understand how tools grow without rewriting the loop
s03agents/s03_todo_write.pyplanning state typestodo handler path -> reminder injection -> agent_loop()You understand visible session planning state
s04agents/s04_subagent.pyAgentTemplaterun_subagent() -> parent agent_loop()You understand that subagents are mainly context isolation
s05agents/s05_skill_loading.pyskill registry typesregistry methods -> agent_loop()You understand discover light, load deep
s06agents/s06_context_compact.pyCompactStatepersist / micro compact / history compact -> agent_loop()You understand that compaction relocates detail instead of deleting continuity

Stage 2: s07-s11

This stage hardens the control plane around a working single agent.

ChapterFileRead FirstThen ReadConfirm Before Moving On
s07agents/s07_permission_system.pyvalidator / managerpermission path -> run_bash() -> agent_loop()You understand gate before execute
s08agents/s08_hook_system.pyHookManagerhook registration and dispatch -> agent_loop()You understand fixed extension points
s09agents/s09_memory_system.pymemory managerssave path -> prompt build -> agent_loop()You understand memory as a long-term information layer
s10agents/s10_system_prompt.pySystemPromptBuilderreminder builder -> agent_loop()You understand input assembly as a pipeline
s11agents/s11_error_recovery.pycompact / backoff helpersrecovery branches -> agent_loop()You understand continuation after failure

Stage 3: s12-s14

This stage turns the harness into a work runtime.

ChapterFileRead FirstThen ReadConfirm Before Moving On
s12agents/s12_task_system.pyTaskManagertask create / dependency / unlock -> agent_loop()You understand durable work goals
s13agents/s13_background_tasks.pyNotificationQueue / BackgroundManagerbackground registration -> notification drain -> agent_loop()You understand runtime slots
s14agents/s14_cron_scheduler.pyCronLock / CronSchedulercron match -> trigger -> agent_loop()You understand future start conditions

Stage 4: s15-s19

This stage is about platform boundaries.

ChapterFileRead FirstThen ReadConfirm Before Moving On
s15agents/s15_agent_teams.pyMessageBus / TeammateManagerroster / inbox / loop -> agent_loop()You understand persistent teammates
s16agents/s16_team_protocols.pyRequestStore / TeammateManagerrequest handlers -> agent_loop()You understand request-response plus request_id
s17agents/s17_autonomous_agents.pyclaim and identity helpersclaim path -> resume path -> agent_loop()You understand idle check -> safe claim -> resume work
s18agents/s18_worktree_task_isolation.pyTaskManager / WorktreeManager / EventBusworktree lifecycle -> agent_loop()You understand goals versus execution lanes
s19agents/s19_mcp_plugin.pycapability gate / MCP client / plugin loader / routertool pool build -> route -> normalize -> agent_loop()You understand how external capability enters the same control plane

Best Doc + Code Loop

For each chapter:

  1. read the chapter prose
  2. read the bridge note for that chapter
  3. open the matching agents/sXX_*.py
  4. follow the order: state -> tools -> turn driver -> CLI entry
  5. run the demo once
  6. rewrite the smallest version from scratch

Key Takeaway

Code reading order must obey teaching order: read boundaries first, then state, then the path that advances the loop.