Claude Code’s Source Code Reveals Anthropic’s Move from Tool to Self‑Evolving AI Agent
A deep dive into Claude Code’s 500 k‑line TypeScript repository shows how Anthropic is turning a programming assistant into a memory‑rich, autonomous AI agent platform with multi‑agent collaboration, cloud‑native scheduling, speculative execution, and even a pet‑style companion.
Memory lifecycle management
Claude Code stores four distinct memory types in separate files: user (preferences), feedback (corrections), project (project information), and reference (external references). The feedback type records both negative and positive feedback. The prompt explicitly requires recording success as well as failure:
Record from failure AND success: if you only save corrections, you avoid past mistakes but drift away from approaches the user has already validated, and may grow overly cautious.
Three‑layer memory architecture
Session Memory – triggered when a conversation reaches 10 000 tokens; thereafter every additional 5 000 tokens **and** either ≥3 tool calls **or** a natural pause cause a summarisation of the current session.
Auto‑Dream – runs when at least 24 h have passed since the last consolidation **and** ≥5 new sessions have accumulated. It reviews, merges and prunes the memory files.
KAIROS Daily Log – an append‑only log that runs continuously in assistant mode and is distilled nightly.
Context compression – nine‑section structured memory
1. Primary Request and Intent
2. Key Technical Concepts
3. Files and Code Sections (must include code!)
4. Errors and fixes
5. Problem Solving
6. All user messages (no omissions)
7. Pending Tasks
8. Current Work
9. Optional Next StepThe system enforces two hard rules: exact code snippets must be retained, and every user utterance must be kept.
DreamTask – background “sleep” and dreaming
When its trigger conditions are met, a DreamTask spawns a sub‑agent that processes memory in four phases:
Phase 1 — Orient: scan existing memories to avoid duplicates
Phase 2 — Gather: grep log directories for relevant details
Phase 3 — Consolidate: merge duplicates, resolve contradictions, convert relative dates to absolute dates, delete superseded facts
Phase 4 — Prune: keep the index under 200 lines / 25 KB and resolve conflictsTrigger: at least 24 h since the previous run **and** at least five new sessions accumulated. Concurrency is controlled by a file‑lock whose modification time records the last run; if the lock is stale (>1 h) it is reclaimed, and on failure the timestamp rolls back so the task can retry.
Autonomy via tick heartbeat and SleepTool
In assistant mode the model receives a periodic <tick> heartbeat. After each tick the model evaluates whether valuable work exists; if not, it calls SleepTool to pause. The prompt notes that the SleepTool cache expires after five minutes, forcing the model to balance wake‑up frequency against API cost.
Enforced tool‑only output
All user‑visible output must be sent through the SendUserMessage (aka BriefTool) tool. Direct text output is hidden, separating internal reasoning from what the user sees.
External message wake‑up
Through the MCP Channel protocol, external platforms (Discord, Slack, SMS, etc.) push messages wrapped in XML tags into the assistant session. The SleepTool wakes the assistant within one second of detecting a new message.
Scheduling – local and cloud engines
CronScheduler – a built‑in scheduler that supports standard cron expressions for local tasks.
/schedule skill – creates remote Anthropic‑hosted agents that run on a cron schedule. Each remote session is isolated, with configurable Git repositories, tool whitelists and MCP connectors. Minimum interval is one hour.
GitHub PR subscription
The SubscribePRTool streams GitHub pull‑request events (reviews, CI results, new comments) into the current session in real time.
Multi‑agent collaboration
Swarm/Teammate – file‑mailbox team system
Each teammate has a JSON mailbox file; agents communicate by reading/writing these files with file‑lock safety. Two execution modes exist:
In‑process mode – shared API client via AsyncLocalStorage.
Screen‑split mode – separate ClaudeCLI processes in tmux / iTerm2.
Permission requests use a structured permission_request/permission protocol, ensuring explicit approvals.
Coordinator – strict project manager
When Coordinator mode is active, Claude acts as a project manager with only three tools: Agent (assign work), SendMessage (continue), and TaskStop (halt). The system prompt (≈369 lines) defines a four‑stage workflow:
Research → Synthesis → Implementation → Verification
A quoted rule stresses that the coordinator must understand the problem itself, not delegate comprehension:
Never write "based on your findings" or "based on the research." These phrases delegate understanding to the worker instead of doing it yourself.
Forked sub‑agent – byte‑level cache sharing
When a forked sub‑agent is created, the parent’s assistant message is duplicated and all tool_use results are replaced with identical placeholder text; only the final instruction differs. This guarantees byte‑perfect prompt cache hits across parallel agents.
UDS_INBOX – cross‑instance communication
Multiple Claude Code instances discover each other via Unix Domain Sockets. Messages are sent with addresses like uds:/path/to.sock (local) or bridge:session_id (remote).
Bridge – four remote‑control modes
Headless : UI‑less background, ideal for CI/CD.
Remote MCP : exposes an MCP server for external clients.
Teleport : transfers sessions between browser and terminal.
Full Remote : runs a completely remote Claude Code instance.
The protocol supports session restoration, state synchronization and device switching.
Ultraplan – using the strongest model as a planner
For complex tasks the system launches a remote server running Anthropic’s Opus 4.6 in “Plan Mode”. Workflow:
User describes a complex task → local CLI creates a remote session.
Opus 4.6 thinks for up to 30 minutes.
User reviews or edits the plan in the browser.
Approved plan is sent back for local execution.
A sentinel constant ULTRAPLAN_TELEPORT_SENTINEL prevents recursive activation of the /ultraplan command.
Settings sync – cross‑device memory synchronization
The Anthropic API provides a settings‑sync endpoint. The client merges changes using a last‑write‑wins timestamp strategy, with local caching and incremental synchronization.
Interaction enhancements
Buddy – desktop pet system
The hidden “BUDDY” system defines 18 species (duck, goose, blob, cat, dragon, octopus, owl, penguin, turtle, snail, ghost, axolotl, capybara, cactus, robot, rabbit, mushroom, chonk), five rarity levels, six eye styles, eight hats and five attribute values (DEBUGGING, PATIENCE, CHAOS, WISDOM, SNARK). A deterministic pet is generated from userId+salt; a 1 % chance yields a shiny variant. Pets are rendered as three‑frame ASCII animations and can be summoned with /buddy pet, which displays a heart animation. Appearance data is regenerated each login; personality data (name, traits) is stored separately to prevent tampering.
Voice mode – push‑to‑talk
A complete voice‑input pipeline streams audio via WebSocket to Anthropic’s private STT endpoint. Native capture uses macOS CoreAudio or Linux ALSA; fallback to SoX is not supported. The audio module is lazily loaded because dlopen is blocking and can take up to eight seconds on cold start. A feature flag can switch to Deepgram Nova 3 STT.
Prompt suggestion + speculative execution
After each turn a forked sub‑agent predicts the user’s next input, discarding overly short/long or AI‑style suggestions. The predicted action can be executed in an isolated sandbox agent; if the user confirms, the result is kept, otherwise it is discarded. This mirrors CPU branch prediction but for human intent.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Machine Learning Algorithms & Natural Language Processing
Focused on frontier AI technologies, empowering AI researchers' progress.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
