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 GitHubKey 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
Memory & Context
Implement Context Compaction
Keep long agent sessions alive by compacting old conversation into structured summaries before the context window fills.
4 steps · 90-120 minutes
Memory & Context
Session Summaries That Survive Compaction
Write durable session summaries at the right moments so knowledge outlives compaction and process restarts.
4 steps · 60-90 minutes
Memory & Context
Prompt-Cache-Aware Conversation Design
Structure your agent's context for prefix caching: stable prefixes, append-only history, and cache-friendly dynamic content.
3 steps · 45-75 minutes
MCP & Tooling
Tool Result Size Management
Stop tool outputs from flooding context: budgets per tool, smart truncation, pagination, and reference-based results.
4 steps · 60-90 minutes
From the archive
Verbatim excerpts mined from our local archive of this repository — the prompts, schemas, and patterns worth stealing.
'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
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