Package com.tnsai.llm.cost
Class CostAwareLLMClient
java.lang.Object
com.tnsai.llm.cost.CostAwareLLMClient
- All Implemented Interfaces:
com.tnsai.llm.LLMClient
LLMClient wrapper that tracks usage costs and enforces budgets.
CostAwareLLMClient wraps any LLMClient and automatically:
- Records token usage and costs
- Tracks cumulative spend
- Enforces budget limits (optional)
- Triggers alerts on threshold breaches
Usage Example
// Create base client
LLMClient baseClient = new OpenAIClient("gpt-4o", apiKey);
// Wrap with cost tracking
CostAwareLLMClient client = CostAwareLLMClient.builder()
.client(baseClient)
.costTracker(new InMemoryCostTracker())
.budgetManager(BudgetManager.builder()
.limit(10.0) // $10 daily budget
.daily()
.alertThreshold(0.8)
.build())
.build();
// Use normally - costs are tracked automatically
ChatResponse response = client.chat("Hello!");
// Check costs
System.out.println("Total cost: $" + client.getTotalCost());
System.out.println("Requests: " + client.getRequestCount());
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic CostAwareLLMClient.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(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools, float temperature) 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) Gets the budget manager.Gets budget status if budget manager is configured.com.tnsai.llm.LLMCapabilitiesGets the cost tracker.com.tnsai.llm.LLMClientGets the underlying delegate client.getModel()longGets total request count.Gets cost statistics.floatGets total cost across all requests.longGets total input tokens used.longGets total output tokens used.streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) streamChat(List<com.tnsai.models.chat.ContentPart> messageParts, 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) static CostAwareLLMClientwrap(com.tnsai.llm.LLMClient client) Wraps a client with default cost tracking.static CostAwareLLMClientwrap(com.tnsai.llm.LLMClient client, BudgetManager budget) Wraps a client with cost tracking and budget.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, getProviderName, getTopP, listModels, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec
-
Method Details
-
builder
-
wrap
Wraps a client with default cost tracking. -
wrap
Wraps a client with cost tracking and budget. -
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
-
chat
public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools, float temperature) - 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
-
streamChat
public Stream<String> streamChat(List<com.tnsai.models.chat.ContentPart> messageParts, 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
-
getCostTracker
Gets the cost tracker. -
getBudgetManager
Gets the budget manager. -
getTotalCost
Gets total cost across all requests. -
getRequestCount
public long getRequestCount()Gets total request count. -
getTotalInputTokens
public long getTotalInputTokens()Gets total input tokens used. -
getTotalOutputTokens
public long getTotalOutputTokens()Gets total output tokens used. -
getDelegate
public com.tnsai.llm.LLMClient getDelegate()Gets the underlying delegate client. -
getBudgetStatus
Gets budget status if budget manager is configured. -
getStatistics
Gets cost statistics.
-