Skip to content

[Feature]: Prompt Caching Optimization - Move Dynamic Info Out of System Prompt #26750

Description

@tggpt01

Summary

Optimize prompt caching by moving dynamic information (time, runtime) from system prompt to conversation messages, following Claude Code team's best practices.

Problem to solve

Current system prompt contains dynamic info that changes every request:

  • ## Current Date & Time - changes every minute
  • ## Runtime - contains model, channel that may vary

This breaks Anthropic's prefix caching, causing:

  • Lower cache hit rate (~50-60% vs potential >85%)
  • Higher token costs
  • Slower response times

Proposed solution

  1. Move time info to user messages via <system-reminder> tag:
const userMessage = `<system-reminder>Time: ${now}</system-reminder>\n\n${originalMessage}`;
  1. Separate Runtime info:
  • Keep stable info (agent, host, repo, os) in system prompt
  • Move dynamic info (model, channel) to messages
  1. Fix tool definition order - ensure consistent ordering across sessions

  2. Implement Cache-Safe Compaction - keep same prefix during compaction

Alternatives considered

  • Remove time completely: Not viable as agents need current time awareness
  • Cache per-minute: Inefficient, still breaks on minute boundaries
  • User-level caching only: Misses opportunity for cross-session optimization

Impact

Affected: All Clawdbot users using Anthropic models
Severity: Medium - impacts cost and latency
Frequency: Every API call
Consequence:

  • Estimated 30-50% higher token costs than necessary
  • 20-30% slower first-token latency
  • Reduced efficiency for long conversations

Evidence/examples

References:

Additional information

Claude Code team treats cache hit rate as a critical metric - drops trigger SEV incidents. This optimization aligns with their production-proven patterns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions