+
    Ji                     F    R t ^ RIt^ RIHtHtHt RR R lltRR R lltR# )	aa  Anthropic prompt caching (system_and_3 strategy).

Reduces input token costs by ~75% on multi-turn conversations by caching
the conversation prefix. Uses 4 cache_control breakpoints (Anthropic max):
  1. System prompt (stable across all turns)
  2-4. Last 3 non-system messages (rolling window)

Pure functions -- no class state, no AIAgent dependency.
N)AnyDictListc                @    V ^8  d   QhR\         R\         R\        RR/# )   msgcache_markernative_anthropicreturnN)dictbool)formats   "1/home/ubuntu/hermes-agent/agent/prompt_caching.py__annotate__r      s)     1 1T 1 1 1Z^ 1    c                n   V P                  RR4      pV P                  R4      pVR8X  d   V'       d   WR&   R# Ve   VR8X  d   WR&   R# \        V\        4      '       d   RRRVRV/.V R&   R# \        V\        4      '       d2   V'       d(   VR	,          p\        V\        4      '       d   WR&   R# R# R# R# )
zFAdd cache_control to a single message, handling all format variations.role contenttoolcache_controlNtypetext)get
isinstancestrlistr   )r   r   r	   r   r   lasts   &&&   r   _apply_cache_markerr      s    7762Dggi Gv~#/ 'R-+O'3VVWo|L
I 	'4  Wr{dD!!$0! " &- r   c                    V ^8  d   QhR\         \        \        \        3,          ,          R\        R\        R\         \        \        \        3,          ,          /# )r   api_messages	cache_ttlr	   r
   )r   r   r   r   r   )r   s   "r   r   r   )   sJ      tCH~&  
$sCx.	r   c                   \         P                  ! V 4      pV'       g   V# RR/pVR8X  d   RVR&   ^ pV^ ,          P                  R4      R8X  d   \        V^ ,          WBR7       V^,          p^V,
          p\	        \        V4      4       Uu. uF#  qsV,          P                  R4      R8w  g   K!  VNK%  	  ppW) R  F  p	\        W9,          WBR7       K  	  V# u upi )	zApply system_and_3 caching strategy to messages for Anthropic models.

Places up to 4 cache_control breakpoints: system prompt + last 3 non-system messages.

Returns:
    Deep copy of messages with cache_control breakpoints injected.
r   	ephemeral1httlr   system)r	   N)copydeepcopyr   r   rangelen)
r!   r"   r	   messagesmarkerbreakpoints_used	remaininginon_sysidxs
   &&&       r   apply_anthropic_cache_controlr3   )   s     }}\*Hk"FDu{v(*HQKSA$$IH.V.Q1+//&2IX2Uqq.GVz{#HM6U $ O	 Ws   C/C)F)5mF)__doc__r(   typingr   r   r   r   r3    r   r   <module>r8      s$     " "14 r   