Package com.tnsai.llm.cache
Class PromptCachingClient
java.lang.Object
com.tnsai.llm.cache.PromptCachingClient
- All Implemented Interfaces:
com.tnsai.llm.LLMClient
LLMClient wrapper that adds prompt caching support.
PromptCachingClient wraps any LLMClient and adds cache control markers to system prompts, tools, and conversation history.
Features
- Automatic system prompt caching
- Tool definition caching
- Conversation history caching (configurable breakpoints)
- Cache usage tracking and statistics
Usage Example
// Wrap an Anthropic client
LLMClient baseClient = new AnthropicClient("claude-sonnet-4-20250514");
PromptCachingClient client = PromptCachingClient.builder()
.client(baseClient)
.cacheSystemPrompt(true)
.cacheTools(true)
.cacheHistoryBreakpoints(2) // Cache last 2 history points
.build();
// Use normally - caching is automatic
ChatResponse response = client.chat("Hello", systemPrompt, history, tools);
// Check cache usage
if (response.hasCacheUsage()) {
System.out.println("Cache read tokens: " + response.getCacheReadInputTokens().get());
}
Cost Savings
- Cache reads: 90% cheaper than regular input tokens
- Cache writes: 25% more expensive (one-time cost)
- TTL: 5 minutes (refreshed on each use)
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic PromptCachingClient.Builderbuilder()com.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) com.tnsai.llm.ChatResponsechat(List<com.tnsai.models.chat.ContentPart> messageParts, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) intGets the number of history breakpoints for caching.doubleGets cache hit rate (0.0 to 1.0).com.tnsai.llm.LLMCapabilitiescom.tnsai.llm.LLMClientGets the underlying delegate client.doubleGets estimated cost savings from caching.getModel()longGets total request count.floatlongGets total cache creation tokens across all requests.longGets total cache read tokens across all requests.booleanChecks if system prompt caching is enabled.booleanChecks if tool caching is enabled.voidResets statistics.streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Stream<com.tnsai.llm.ChatChunk> streamChatWithSpec(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, getProviderName, getTopP, listModels, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec
-
Method Details
-
builder
-
getModel
- Specified by:
getModelin interfacecom.tnsai.llm.LLMClient
-
getTemperature
public float getTemperature()- Specified by:
getTemperaturein interfacecom.tnsai.llm.LLMClient
-
getMaxTokens
- Specified by:
getMaxTokensin interfacecom.tnsai.llm.LLMClient
-
getCapabilities
public com.tnsai.llm.LLMCapabilities getCapabilities()- Specified by:
getCapabilitiesin interfacecom.tnsai.llm.LLMClient
-
chat
public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
chatin interfacecom.tnsai.llm.LLMClient
-
chat
public com.tnsai.llm.ChatResponse chat(List<com.tnsai.models.chat.ContentPart> messageParts, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
chatin interfacecom.tnsai.llm.LLMClient
-
streamChat
public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatin interfacecom.tnsai.llm.LLMClient
-
streamChatWithSpec
public Stream<com.tnsai.llm.ChatChunk> streamChatWithSpec(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatWithSpecin interfacecom.tnsai.llm.LLMClient
-
getTotalCacheReadTokens
public long getTotalCacheReadTokens()Gets total cache read tokens across all requests. -
getTotalCacheCreationTokens
public long getTotalCacheCreationTokens()Gets total cache creation tokens across all requests. -
getRequestCount
public long getRequestCount()Gets total request count. -
getCacheHitRate
public double getCacheHitRate()Gets cache hit rate (0.0 to 1.0). -
getEstimatedSavings
public double getEstimatedSavings()Gets estimated cost savings from caching.- Returns:
- Savings as a fraction (e.g., 0.85 = 85% savings)
-
resetStats
public void resetStats()Resets statistics. -
getDelegate
public com.tnsai.llm.LLMClient getDelegate()Gets the underlying delegate client. -
isCacheSystemPromptEnabled
public boolean isCacheSystemPromptEnabled()Checks if system prompt caching is enabled. -
isCacheToolsEnabled
public boolean isCacheToolsEnabled()Checks if tool caching is enabled. -
getCacheHistoryBreakpoints
public int getCacheHistoryBreakpoints()Gets the number of history breakpoints for caching.
-