Class CostAwareLLMClient

java.lang.Object
com.tnsai.llm.cost.CostAwareLLMClient
All Implemented Interfaces:
com.tnsai.llm.LLMClient

public class CostAwareLLMClient extends Object implements 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());