中文 | English
These exercises are not meant to make you read every file. The goal is to enter the codebase with a question and leave with evidence.
They are not only for beginners: new readers can use them to connect examples to source, while experienced readers can use them to check whether they can explain the boundary instead of just repeating the concept.
- Run one example or read one layer note first.
- Then open the matching source map and confirm which call chain you are following.
- Do only one or two exercises for the current topic.
- When you answer, write down the symbols you searched and the files you checked.
Recommended stage: P2
Goal: follow one user input all the way to model execution and back to the final answer.
Steps:
- Read the "Main Query Path" section in the source map.
- Search
queryandqueryLoop. - Search
queryModelWithStreaming. - Write one chain in your own words:
who starts the turn -> who calls the model -> who handles tools -> who exits the loop
Done when:
- You can explain the boundary between
QueryEngine.tsandquery.ts. - You can point to the layer where tool results are appended back into history.
Recommended stage: P3
Goal: understand what QueryEngine adds beyond "just one more wrapper."
Steps:
- Search
class QueryEngine. - Search
before_getSystemPrompt,after_getSystemPrompt, andsystem_message_yielded. - Using L10, write down:
- three things it does before the main loop starts
- what would become worse if those responsibilities lived directly inside
query.ts
Done when:
- Your answer uses at least two of these ideas:
system prompt assembly,session state,slash commands.
Recommended stage: P2
Goal: stop treating "tool calls" and "safety approval" as the same layer.
Steps:
- Search
buildToolandgetTools. - Search
BASH_SECURITY_CHECK_IDS. - Explain what each of these layers does:
- tool schema
- tool availability and approval
- command safety checks
Done when:
- You can point to at least
Tool.ts,tools.ts, andtools/BashTool/bashSecurity.ts. - You can explain why Claude Code uses a 3-layer permission model instead of a single prompt.
Recommended stage: P2
Goal: understand the engineering value of async generators.
Steps:
- Search
stream_request_start. - Search
queryModelWithStreamingandfirst_chunk. - Using L8 and L11, answer:
- what raw API output becomes first
- why the upper loop benefits from
yield* - why cancellation, errors, and final text fit naturally into one event stream
Done when:
- Your answer is more specific than "streaming is convenient."
- You can name at least two concrete events or event stages.
Recommended stage: P3
Goal: understand why the terminal UI is not just one giant component.
Steps:
- Search
DO NOT ADD MORE STATE HERE. - Search
export function REPLandprocessInitialMessage. - Explain:
- which state must be shared
- which state should stay local
- how the initial message enters the interaction flow
Done when:
- You can explain why global state and REPL-local state should not all be merged together.
Recommended stage: P4
Goal: avoid treating memory as "just put more text into the system prompt."
Steps:
- Search
SYSTEM_PROMPT_DYNAMIC_BOUNDARY. - Search
ENTRYPOINT_NAME,buildMemoryLines, andextractMemories. - Explain:
- why the stable prefix and dynamic suffix are separated
- why memory behaves more like an index than a growing log
Done when:
- Your answer clearly separates context management, prompt cache, and memory extraction.
Recommended stage: P4
Goal: understand that Claude Code is not only an interactive terminal shell.
Steps:
- Search
runHeadless. - Search
bridgeMain. - Search
entrypoints/cli.tsx. - Using L15, explain:
- which modes share QueryEngine
- which modes bypass the REPL
- why dispatch must happen at the entrypoint
Done when:
- You can compare at least two of these surfaces:
REPL,print,bridge.
If you can finish the first four exercises, you have already moved from “understanding examples” to “reading source.” These documents become much more useful: