Class CacheableContent

java.lang.Object
com.tnsai.llm.cache.CacheableContent

public class CacheableContent extends Object
Wrapper for content that can be cached.

CacheableContent wraps text content or structured content with optional cache control settings.

Usage Examples

Cacheable System Prompt


 CacheableContent systemPrompt = CacheableContent.of(
     "You are an expert Java developer...",
     CacheControl.ephemeral()
 );
 

Cacheable with Multiple Text Blocks


 CacheableContent content = CacheableContent.builder()
     .addText("Context information...")
     .addText("More context...", CacheControl.ephemeral())  // Only this part cached
     .build();
 
  • Method Details

    • of

      public static CacheableContent of(String text)
      Creates cacheable content from text.
      Parameters:
      text - The text content
      Returns:
      Cacheable content without cache control
    • of

      public static CacheableContent of(String text, CacheControl cacheControl)
      Creates cacheable content from text with cache control.
      Parameters:
      text - The text content
      cacheControl - The cache control settings
      Returns:
      Cacheable content with cache control
    • builder

      public static CacheableContent.Builder builder()
      Creates a builder for complex content with multiple blocks.
    • getText

      public Optional<String> getText()
      Gets the text content (for simple content).
    • getBlocks

      Gets content blocks (for complex content).
    • getCacheControl

      public Optional<CacheControl> getCacheControl()
      Gets the cache control (for simple content).
    • isCacheable

      public boolean isCacheable()
      Checks if this content has cache control enabled.
    • isSimple

      public boolean isSimple()
      Checks if this is simple text content.
    • toApiFormat

      public Object toApiFormat()
      Converts to Anthropic API format.

      For simple text without cache control:

      {"type": "text", "text": "..."}

      For text with cache control:

      {"type": "text", "text": "...", "cache_control": {"type": "ephemeral"}}
      Returns:
      Content in Anthropic API format