Package com.tnsai.llm
Class ChatChunk
java.lang.Object
com.tnsai.llm.ChatChunk
Represents a single chunk in a streaming LLM response.
ChatChunk is emitted during streaming chat operations, providing both the content token and rich metadata about the streaming progress. This enables real-time monitoring, token counting, and latency tracking.
Chunk Types
| Type | Description | Content |
|---|---|---|
ChatChunk.ChunkType.START | Stream started | Empty |
ChatChunk.ChunkType.CONTENT | Token content | Text token |
ChatChunk.ChunkType.TOOL_CALL | Tool invocation | Tool JSON |
ChatChunk.ChunkType.DONE | Stream completed | Empty |
ChatChunk.ChunkType.ERROR | Error occurred | Error message |
Usage Example
LLMClient client = new OpenAIClient("gpt-4o");
// Stream with metadata
client.streamChatWithSpec("Tell me a story", Optional.empty(), Optional.empty(), Optional.empty())
.forEach(chunk -> {
if (chunk.isContent()) {
System.out.print(chunk.getContent());
}
// Track progress
chunk.getTokenCount().ifPresent(count -> {
System.err.println("Tokens so far: " + count);
});
// Check finish reason
if (chunk.isDone()) {
System.out.println("\nFinished: " + chunk.getFinishReason().orElse("unknown"));
}
});
Token Tracking
AtomicInteger totalTokens = new AtomicInteger();
long startTime = System.currentTimeMillis();
client.streamChatWithSpec(message, system, history, tools)
.forEach(chunk -> {
chunk.getTokenCount().ifPresent(totalTokens::set);
});
long latency = System.currentTimeMillis() - startTime;
System.out.printf("Generated %d tokens in %dms%n", totalTokens.get(), latency);
Error Handling
client.streamChatWithSpec(message, system, history, tools)
.forEach(chunk -> {
if (chunk.isError()) {
System.err.println("Stream error: " + chunk.getContent());
return;
}
System.out.print(chunk.getContent());
});
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe type of streaming chunk.static enumThe reason why generation stopped. -
Constructor Summary
ConstructorsConstructorDescriptionChatChunk(ChatChunk.ChunkType type, String content, Instant timestamp, Integer tokenCount, Integer chunkIndex, ChatChunk.FinishReason finishReason, String model, String requestId) ChatChunk(ChatChunk.ChunkType type, String content, Instant timestamp, Integer tokenCount, Integer chunkIndex, ChatChunk.FinishReason finishReason, String model, String requestId, ChatResponse.ToolCall toolCall) -
Method Summary
Modifier and TypeMethodDescriptionstatic ChatChunkCreates a CONTENT chunk with text token.static ChatChunkCreates a CONTENT chunk with metadata.static ChatChunkCreates a CONTENT chunk with full metadata.static ChatChunkdone(ChatChunk.FinishReason reason, int totalTokens) Creates a DONE chunk indicating successful completion.static ChatChunkdone(ChatChunk.FinishReason reason, int totalTokens, String model, String requestId) Creates a DONE chunk with full metadata.static ChatChunkCreates an ERROR chunk.Gets the chunk index if available.Gets the finish reason if available.getModel()Gets the model if available.Gets the request ID if available.Gets the token count if available.Gets the structured ToolCall data if this is a TOOL_CALL chunk.booleanChecks if this is a content chunk.booleanisDone()Checks if this is a done chunk.booleanisError()Checks if this is an error chunk.booleanisStart()Checks if this is a start chunk.booleanChecks if this is a tool call chunk.static ChatChunkCreates a START chunk indicating stream beginning.static ChatChunktoolCall(ChatResponse.ToolCall toolCall) Creates a TOOL_CALL chunk with structured ToolCall data.static ChatChunkCreates a TOOL_CALL chunk with JSON content.
-
Constructor Details
-
ChatChunk
public ChatChunk(ChatChunk.ChunkType type, String content, Instant timestamp, Integer tokenCount, Integer chunkIndex, ChatChunk.FinishReason finishReason, String model, String requestId) -
ChatChunk
public ChatChunk(ChatChunk.ChunkType type, String content, Instant timestamp, Integer tokenCount, Integer chunkIndex, ChatChunk.FinishReason finishReason, String model, String requestId, ChatResponse.ToolCall toolCall)
-
-
Method Details
-
start
Creates a START chunk indicating stream beginning. -
content
Creates a CONTENT chunk with text token. -
content
Creates a CONTENT chunk with metadata. -
content
public static ChatChunk content(String text, int tokenCount, int chunkIndex, String model, String requestId) Creates a CONTENT chunk with full metadata. -
toolCall
Creates a TOOL_CALL chunk with JSON content. -
toolCall
Creates a TOOL_CALL chunk with structured ToolCall data. -
done
Creates a DONE chunk indicating successful completion. -
done
public static ChatChunk done(ChatChunk.FinishReason reason, int totalTokens, String model, String requestId) Creates a DONE chunk with full metadata. -
error
Creates an ERROR chunk. -
isContent
public boolean isContent()Checks if this is a content chunk. -
isStart
public boolean isStart()Checks if this is a start chunk. -
isDone
public boolean isDone()Checks if this is a done chunk. -
isError
public boolean isError()Checks if this is an error chunk. -
isToolCall
public boolean isToolCall()Checks if this is a tool call chunk. -
getTokenCount
Gets the token count if available. -
getChunkIndex
Gets the chunk index if available. -
getFinishReason
Gets the finish reason if available. -
getModel
Gets the model if available. -
getRequestId
Gets the request ID if available. -
getToolCall
Gets the structured ToolCall data if this is a TOOL_CALL chunk.
-