Tier 1 · Claude Code internals Claude Code internals · fetched 2026-07-02
claude-code-source-analysis (Elyyta)
Smaller independent analysis of the Claude Code source; skimmed for unique observations to cross-check the major bundles.
View source on GitHubKey takeaways
- 01
Use as a cross-check witness against the book-length analyses
From the archive
Verbatim excerpts mined from our local archive of this repository — the prompts, schemas, and patterns worth stealing.
Agent pattern
queryLoop and its four exit conditions
async function* queryLoop(params, consumedCommandUuids) {
let state = { messages, toolUseContext, turnCount: 1, ... }
while (true) {
yield { type: 'stream_request_start' }
// call API (streaming)
if (response.stop_reason !== 'tool_use') {
return { type: 'stop', reason: response.stop_reason }
}
const toolResults = await runTools(toolCalls, toolUseContext)
state = { ...state, messages: [...messages, assistantMsg, ...toolResults] }
}
}
Exit conditions:
1. stop_reason !== 'tool_use'
2. state.turnCount >= maxTurns
3. budgetTracker.isExhausted()
4. maxOutputTokensRecoveryCount >= MAX_OUTPUT_TOKENS_RECOVERY_LIMIT The production agent loop reduced to its skeleton - and the reminder that a real loop needs four ways out, not one.
docs/en/01-agent-loop-tools.md
Insight
The model is the agent; the code is the harness
agent_loop() core pattern (30 lines of pseudocode)
===
production queryLoop() (TypeScript, with streaming/compaction/permissions/UI)
The core pattern is identical:
while(true) { call LLM -> run tools -> append results -> check stop_reason }
All of the production version's complexity lives in the harness layer, not in the loop itself.
12 mechanisms mapped to source: Agent Loop -> query.ts queryLoop() · Tool Dispatch -> tools.ts assembleToolPool() · Subagents -> runAgent.ts · Context Compact -> services/compact/ · Agent Teams -> teammateMailbox.ts · Worktree Isolation -> utils/worktree.ts [translated] The thesis of the whole research vault in one line: capability comes from the model, reliability comes from the harness around it.
README.md