3.11 Design Insights
- Memoize guarantees idempotency: Both
getSystemContextandgetUserContextare memoized, computed only once per session. WhensetSystemPromptInjection()changes, both functions' caches are manually cleared - Progressiveness of the compression pipeline: From zero-cost trimming to full summarization, upgrading level by level as needed. Most conversations never trigger Autocompact
- Reversibility of projection-based folding: Context Collapse doesn't modify original messages and can be safely rolled back — this is where it's superior to Autocompact
- Cache-aware context assembly: The injection order of context (system prompt first, user context before messages) considers prompt cache hit rates
- Anchor strategy for token estimation: Using the precise usage reported by the server as an anchor, only estimating increments, balancing precision and latency
- system-reminder as a unified injection channel: Wrapped in XML tags, injecting system information at any position in the message flow without confusing role boundaries
- Pragmatic trade-off of session-level latching: Both TTL eligibility and beta headers are locked once determined, lasting until session end — sacrificing flexibility for cache stability, since the cost of invalidating 50-100K tokens of cache far outweighs the inability to toggle a feature mid-session
Hands-on Practice: In claude-code-from-scratch,
src/prompt.tsandsrc/system-prompt.mddemonstrate the minimal implementation of context construction. Compare with the multi-layer context assembly in this chapter and think: what context does a minimal Agent need to be sufficient? See the tutorial Chapter 3: System Prompt Engineering.
Previous chapter: Agent Loop | Next chapter: Tool System