Package com.tnsai.llm.observability
Class LLMMetrics
java.lang.Object
com.tnsai.llm.observability.LLMMetrics
- All Implemented Interfaces:
LLMObserver
Collects and reports metrics for LLM operations.
LLMMetrics tracks:
- Request/response counts per provider
- Token usage (estimated) per provider
- Latency statistics (avg, p50, p95, p99)
- Error rates per provider
- Cost estimates based on token usage
Usage Example
// Create metrics collector
LLMMetrics metrics = new LLMMetrics();
// Wrap your client
LLMClient observedClient = new ObservableLLMClient(client, metrics);
// Use the client normally
observedClient.chat("Hello!");
observedClient.chat("Another query");
// Get metrics report
LLMMetrics.Report report = metrics.getReport();
System.out.println("Total requests: " + report.totalRequests());
System.out.println("Total estimated cost: $" + report.totalEstimatedCost());
System.out.println("Avg latency: " + report.avgLatencyMs() + "ms");
Per-Provider Metrics
Map<String, LLMMetrics.ProviderMetrics> byProvider = metrics.getMetricsByProvider();
for (var entry : byProvider.entrySet()) {
System.out.println(entry.getKey() + ":");
System.out.println(" Requests: " + entry.getValue().requests());
System.out.println(" Errors: " + entry.getValue().errors());
System.out.println(" Avg latency: " + entry.getValue().avgLatencyMs() + "ms");
}
Integration with Micrometer
// Export to Micrometer (for Prometheus, etc.)
MeterRegistry registry = new SimpleMeterRegistry();
metrics.exportToMicrometer(registry);
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordPer-provider metrics.static final recordComprehensive metrics report. -
Field Summary
Fields inherited from interface com.tnsai.llm.observability.LLMObserver
NOOP -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGets metrics per provider.Gets a comprehensive metrics report.voidCalled when an error occurs.voidonRequest(com.tnsai.llm.LLMClient client, String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Called before sending a request to the LLM.voidonResponse(com.tnsai.llm.LLMClient client, com.tnsai.llm.ChatResponse response, long latencyMs) Called after receiving a successful response.voidonStreamChunk(com.tnsai.llm.LLMClient client, String chunk, int chunkIndex) Called for each streaming chunk received.voidonStreamComplete(com.tnsai.llm.LLMClient client, int totalChunks, long latencyMs) Called when streaming completes.voidonStreamError(com.tnsai.llm.LLMClient client, Exception error, int chunksReceived, long latencyMs) Called when streaming encounters an error.voidreset()Resets all metrics.
-
Constructor Details
-
LLMMetrics
public LLMMetrics()
-
-
Method Details
-
onRequest
public void onRequest(com.tnsai.llm.LLMClient client, String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Description copied from interface:LLMObserverCalled before sending a request to the LLM.- Specified by:
onRequestin interfaceLLMObserver- Parameters:
client- The LLM client being usedmessage- The user messagesystemPrompt- Optional system prompthistory- Optional conversation historytools- Optional tools
-
onResponse
public void onResponse(com.tnsai.llm.LLMClient client, com.tnsai.llm.ChatResponse response, long latencyMs) Description copied from interface:LLMObserverCalled after receiving a successful response.- Specified by:
onResponsein interfaceLLMObserver- Parameters:
client- The LLM client that was usedresponse- The response receivedlatencyMs- Time taken in milliseconds
-
onError
Description copied from interface:LLMObserverCalled when an error occurs.- Specified by:
onErrorin interfaceLLMObserver- Parameters:
client- The LLM client that was usederror- The exception that occurredlatencyMs- Time taken before error in milliseconds
-
onStreamChunk
Description copied from interface:LLMObserverCalled for each streaming chunk received.- Specified by:
onStreamChunkin interfaceLLMObserver- Parameters:
client- The LLM client being usedchunk- The content chunkchunkIndex- The index of this chunk (0-based)
-
onStreamComplete
public void onStreamComplete(com.tnsai.llm.LLMClient client, int totalChunks, long latencyMs) Description copied from interface:LLMObserverCalled when streaming completes.- Specified by:
onStreamCompletein interfaceLLMObserver- Parameters:
client- The LLM client that was usedtotalChunks- Total number of chunks receivedlatencyMs- Total time taken in milliseconds
-
onStreamError
public void onStreamError(com.tnsai.llm.LLMClient client, Exception error, int chunksReceived, long latencyMs) Description copied from interface:LLMObserverCalled when streaming encounters an error.- Specified by:
onStreamErrorin interfaceLLMObserver- Parameters:
client- The LLM client that was usederror- The exception that occurredchunksReceived- Number of chunks received before errorlatencyMs- Time taken before error in milliseconds
-
getReport
Gets a comprehensive metrics report. -
getMetricsByProvider
Gets metrics per provider. -
reset
public void reset()Resets all metrics.
-