Skip to content
Flows
Tier 1 · Claude Code internals Claude Code internals · fetched 2026-07-02

claude-code-source-analysis (xiaonancs)

A systems-engineering reconstruction of the complete Claude Code agent harness from a reverse-engineered v2.1.88 snapshot (includes the extracted source zip plus appendix analyses). Part of a series that also covers OpenAI Codex CLI internals, giving a comparative harness view.

View source on GitHub

Key takeaways

  • 01

    Reconstructs the full harness from an actual extracted v2.1.88 snapshot

  • 02

    Sister studies (Codex CLI) enable cross-harness comparison

  • 03

    Strong on context management and compaction mechanics

Flows built on this research

From the archive

Verbatim excerpts mined from our local archive of this repository — the prompts, schemas, and patterns worth stealing.

Insight

'Bash is all you need' — and a 200x cache cost lever

The core agent loop, as the community summarizes it:

while (true) {
  response = call_model(messages)
  tool_results = execute_tools_if_any(response)
  messages.push(response, tool_results)
  if (!needs_follow_up(response, tool_results)) break
}

The complexity is not in the loop but in the harness around it: context management, permission control, security defense, memory persistence, multi-agent orchestration. (1,884 files, 513,216 lines; 40 tools, 86 slash commands, 90 compile-time feature flags.)

On caching: under Anthropic's prompt cache, a cache hit costs roughly 1/200th of a miss at 200K tokens - System Prompt cache design directly moves the cost of millions of daily API calls.

[translated] The 30-article study's two sharpest numbers: the loop is 6 lines, and getting the cache boundary right is a 200x cost lever.

总纲B-Claude-Code-Harness技术深度分析.md

Memory

Five memory layers, ~4,200 lines of code

| 1 | CLAUDE.md instruction files | permanent | project/user/enterprise dirs | static instructions |
| 2 | Auto Memory (memdir) | cross-session | ~/.claude/projects/<slug>/memory/ | AI-extracted persistent knowledge |
| 3 | Session Memory | single session | .../session-memory/summary.md | structured notes for the current session |
| 4 | Agent Memory | cross-session | three scope directories | per-agent-type experience |
| 5 | Relevant Memories | injected per turn | in-memory attachment | Sonnet side-query recall |

Implementation: src/memdir/ (8 files), claudemd.ts (1,480 lines), extractMemories/ (771 lines), SessionMemory/ (821 lines), autoDream/ (465 lines), agentMemory.ts (178 lines).

[translated] Each layer answers a different timescale - permanent rules, cross-session knowledge, session notes, per-agent experience, and per-turn recall - and together they cost ~4,200 lines.

Part I Foundations/07-Memory.md