Claus Ibsen created CAMEL-23860:
-----------------------------------

             Summary: camel-langchain4j-chat/agent/tools - Add token usage and 
finish reason as exchange headers
                 Key: CAMEL-23860
                 URL: https://issues.apache.org/jira/browse/CAMEL-23860
             Project: Camel
          Issue Type: Improvement
          Components: camel-langchain4j-agent, camel-langchain4j-chat, 
camel-langchain4j-tools
            Reporter: Claus Ibsen


The camel-langchain4j-embeddings component already exposes token usage data 
(input/output/total token count) and finish reason as exchange headers from the 
LangChain4j Response object. However, the chat, agent, and tools producers 
discard this metadata.

Specifically:
- camel-langchain4j-chat: extracts only response.text() and discards the 
Response wrapper containing tokenUsage() and finishReason()
- camel-langchain4j-agent: same, no token data exposed
- camel-langchain4j-tools: reads finishReason() internally for control flow but 
does not expose it as a header

The LangChain4j API provides this data in the Response object - the embeddings 
producer already demonstrates the pattern:

{code:java}
if (result.tokenUsage() != null) {
    message.setHeader(...INPUT_TOKEN_COUNT, 
result.tokenUsage().inputTokenCount());
    message.setHeader(...OUTPUT_TOKEN_COUNT, 
result.tokenUsage().outputTokenCount());
    message.setHeader(...TOTAL_TOKEN_COUNT, 
result.tokenUsage().totalTokenCount());
}
{code}

The same pattern should be applied to the chat, agent, and tools producers so 
users can track LLM token consumption, costs, and completion status.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to