跪拜 Guibai
← All articles
Frontend · Backend · Artificial Intelligence

89 Feature Flags, a YOLO Classifier, and the Cache Tricks Inside Claude Code's Leaked Source

By 道友可好 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

These are production engineering patterns from a leading AI coding tool, not speculative best practices. The caching rules, classifier design, and token-budget discipline are directly applicable to anyone building on top of LLM APIs or shipping an AI agent.

Summary

A source-map leak made Claude Code's TypeScript source restorable, and the resulting analysis surfaces 89 build-time feature flags that map out a product roadmap spanning autonomous background agents, multi-agent orchestration, and distributed execution. The permission system relies on a YOLO classifier that uses a fast/slow AI pipeline to audit AI actions, defaulting to deny when uncertain and falling back to manual confirmation after repeated rejections.

Prompt caching is treated as core infrastructure, governed by five principles: keep static content first, update via appended messages rather than edits, never switch models mid-session, manage tool definitions carefully to avoid prefix invalidation, and move cache breakpoints to the latest message. One optimization—moving the agent list from the system prompt to a system-reminder—alone reduced global cache_creation by 10.2%.

Context management operates under hard token budgets. The system prompt consumes 15-20K tokens, auto-compaction triggers at 167K, and every tool result is capped at 50K characters. When content is truncated, the model is told where to find the full version on disk rather than being left to reason on incomplete data.

Takeaways
89 build-time feature flags in Claude Code's source map a product roadmap that includes autonomous background agents (KAIROS), multi-agent orchestration with shared memory, and distributed execution.
The YOLO classifier uses a two-stage AI pipeline—a fast 64-token pass followed by a 4096-token deep reasoning pass—to decide whether an AI action is safe, defaulting to deny on uncertainty.
After three consecutive or twenty total rejections, the classifier falls back to manual user confirmation rather than looping indefinitely.
Prompt caching follows five rules: static content first, update via appended system-reminder messages instead of editing prompts, never switch models mid-session, manage tools carefully to avoid prefix breaks, and move cache breakpoints to the latest message.
Moving the agent list from the system prompt to a system-reminder cut global cache_creation by 10.2%.
Beta headers are latched—once sent, they are always sent—because removing one changes the request signature and invalidates the cache.
Date strings are memoized across midnight to avoid breaking the cache prefix.
System prompts consume 15-20K tokens, auto-compaction triggers at 167K (83.5% of the 200K window), and single tool results are capped at 50K characters.
Truncated content is not silently dropped; the model is told the content was cut and given a disk path to fetch the full version.
Six harness principles emerge from the codebase: treat prompts as the control plane, design for cache awareness, fail closed, A/B test everything, observe before fixing, and latch state for stability.
Conclusions

The 89 feature flags are not just toggles—they are a de facto public product roadmap, showing that Anthropic is actively building toward autonomous background agents, multi-agent systems, and a distributed execution platform.

Using AI to classify the safety of another AI's actions is a recursive trust problem. The two-stage design with a hard fallback to human judgment after repeated rejections is a pragmatic admission that the classifier itself is fallible.

The cache optimization that saved 10.2% globally came from moving a single piece of data between message roles—an almost trivial change with outsized cost impact, suggesting many LLM applications are leaving similar savings on the table.

Hard token budgets on every input—system prompts, tool results, context windows—contradict the 'just throw it all in the context' approach common in early LLM integrations. The cheapest token is the one never sent.

Telling the model that content was truncated and where to find it, rather than silently dropping it, treats the model as an agent capable of information retrieval rather than a passive inference engine. This is a subtle but important design choice for agent architectures.

Concepts & terms
YOLO Classifier
A two-stage AI safety component in Claude Code that judges whether an AI's proposed action is safe. Stage 1 uses a fast 64-token model for sub-second decisions; Stage 2 uses a 4096-token model with chain-of-thought reasoning for ambiguous cases. Defaults to deny when uncertain.
Feature Flag (Build-time)
A compile-time toggle implemented via Bun's `feature()` function that enables dead-code elimination. Entire module trees can be excluded from the build, making experimental features physically absent from production binaries until the flag is enabled.
Cache Prefix
The leading portion of a prompt that remains unchanged across requests, allowing the LLM provider to cache and reuse its computed state. Breaking the prefix—by editing it, switching models, or changing tool definitions—invalidates the cache and forces recomputation.
Auto-Compaction
A context-window management strategy that triggers when token usage reaches a threshold (167K in Claude Code, 83.5% of the 200K window). It compresses conversation history to reclaim space, reserving 20K tokens for the compressed output.
System Reminder
A message appended to the conversation rather than inserted into the system prompt. Because it sits after the cached prefix, it can update the model's behavior without invalidating the cache—a key technique in cache-aware prompt design.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗