Package com.tnsai.llm.observability
Interface LLMObserver
- All Known Implementing Classes:
LLMMetrics,ObservableLLMClient.CompositeObserver,PromptLogger
public interface LLMObserver
Observer interface for LLM request/response monitoring.
LLMObserver enables non-invasive monitoring of all LLM interactions. Implement this interface to add custom logging, metrics collection, or audit trails.
Usage Example
// Create a logging observer
LLMObserver loggingObserver = new LLMObserver() {
@Override
public void onRequest(LLMClient client, String message,
Optional<String> systemPrompt,
Optional<List<Map<String, Object>>> history,
Optional<List<Map<String, Object>>> tools) {
logger.info("LLM Request to {}: {}", client.getModel(), message);
}
@Override
public void onResponse(LLMClient client, ChatResponse response, long latencyMs) {
logger.info("LLM Response from {} ({}ms): {}",
client.getModel(), latencyMs, response.getContent().substring(0, 100));
}
@Override
public void onError(LLMClient client, Exception error, long latencyMs) {
logger.error("LLM Error from {} ({}ms): {}",
client.getModel(), latencyMs, error.getMessage());
}
};
// Wrap client with observer
LLMClient observedClient = new ObservableLLMClient(client, loggingObserver);
Multiple Observers
// Combine multiple observers
LLMObserver combined = CompositeObserver.of(
loggingObserver,
metricsObserver,
auditObserver
);
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondefault voidCalled when an error occurs.default 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.default voidonResponse(com.tnsai.llm.LLMClient client, com.tnsai.llm.ChatResponse response, long latencyMs) Called after receiving a successful response.default voidonStreamChunk(com.tnsai.llm.LLMClient client, String chunk, int chunkIndex) Called for each streaming chunk received.default voidonStreamComplete(com.tnsai.llm.LLMClient client, int totalChunks, long latencyMs) Called when streaming completes.default voidonStreamError(com.tnsai.llm.LLMClient client, Exception error, int chunksReceived, long latencyMs) Called when streaming encounters an error.
-
Field Details
-
NOOP
No-op observer that does nothing.
-
-
Method Details
-
onRequest
default void onRequest(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.- Parameters:
client- The LLM client being usedmessage- The user messagesystemPrompt- Optional system prompthistory- Optional conversation historytools- Optional tools
-
onResponse
default void onResponse(com.tnsai.llm.LLMClient client, com.tnsai.llm.ChatResponse response, long latencyMs) Called after receiving a successful response.- Parameters:
client- The LLM client that was usedresponse- The response receivedlatencyMs- Time taken in milliseconds
-
onError
Called when an error occurs.- Parameters:
client- The LLM client that was usederror- The exception that occurredlatencyMs- Time taken before error in milliseconds
-
onStreamChunk
Called for each streaming chunk received.- Parameters:
client- The LLM client being usedchunk- The content chunkchunkIndex- The index of this chunk (0-based)
-
onStreamComplete
default void onStreamComplete(com.tnsai.llm.LLMClient client, int totalChunks, long latencyMs) Called when streaming completes.- Parameters:
client- The LLM client that was usedtotalChunks- Total number of chunks receivedlatencyMs- Total time taken in milliseconds
-
onStreamError
default void onStreamError(com.tnsai.llm.LLMClient client, Exception error, int chunksReceived, long latencyMs) Called when streaming encounters an error.- Parameters:
client- The LLM client that was usederror- The exception that occurredchunksReceived- Number of chunks received before errorlatencyMs- Time taken before error in milliseconds
-