CAMEL-24560: Use Long for GenAiUsage token counts - #26105
Conversation
OpenAI token usage returns long values; storing them as Integer required Math.toIntExact casts that could overflow or fail for large counts. - Change GenAiUsage inputTokens/outputTokens to Long - Keep Integer factory overload for LangChain4j/Spring AI call sites - Record large token values in Micrometer counters via increment(double) - Pass OpenAI prompt/completion token counts without int conversion - Add GenAiUsageTest and large-token observability coverage - Document Long token fields in the 4.23 upgrade guide Co-authored-by: Cursor Agent <noreply@cursor.com>
- Stop narrowing OpenAI embeddings usage headers to int - Add tests for null/zero Micrometer token counters and Integer factory nulls Co-authored-by: Cursor Agent <noreply@cursor.com>
Automated review follow-up (Bugbot + Grok)Ran Bugbot and Grok review on this changeset. Findings addressed in commit
Grok noted optional future cleanup (single AI-generated comment on behalf of atiaomar1978-hub |
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
|
🧪 CI tested the following changed modules:
🔬 Scalpel shadow comparison — Scalpel: 72 tested, 26 compile-only — current: 71 all testedMaveniverse Scalpel detected 98 affected modules (current approach: 71).
|
gnodet
left a comment
There was a problem hiding this comment.
Solid PR. The Integer → Long widening for GenAiUsage token fields is clean and well-executed:
- Backward compatibility preserved — the
Integerconvenience overload keeps LangChain4j and Spring AI callers working without changes. - OpenAI
(int)cast truncation fixed —OpenAIEmbeddingsProducerno longer narrowslong→int. - Micrometer precision is fine —
doublehas 53 bits of mantissa, exact up to 2^53 (~9 quadrillion tokens). The.increment(tokens.doubleValue())change is correct. - Test coverage is thorough — large values beyond
Integer.MAX_VALUE, null/zero guards, both factory overloads, span attributes, and Micrometer counters are all covered.
Metadata note: This PR has no milestone set — should be 4.23.0 since it targets main.
This review was generated by an AI agent, Hermès, on behalf of @gnodet.
Summary
Changes
GenAiUsagetoken fields fromIntegertoLongso OpenAI and other providers that reportlongtoken counts are recorded without lossy(int)casts orMath.toIntExactfailures.Follow-up to CAMEL-23861 / review on PR #25337.
Changes
GenAiUsage:inputTokensandoutputTokensare nowLong; primary factory acceptsLong; convenience overload keepsIntegerfor LangChain4j/Spring AI call sitesOpenAIProducer: passesusage.promptTokens()/usage.completionTokens()directly (removedtoTokenCounthelper)OpenAIEmbeddingsProducer: sets prompt/total token headers aslong(no(int)cast)GenAiMicrometerSupport: records token counters withincrement(double)for large valuesGenAiUsageTestplus large-token, null/zero, and span coverageGenAiUsageusesLongtoken fieldsTesting
./mvnw -pl components/camel-ai/camel-ai-observability-api,components/camel-ai/camel-ai-observability -am test \ -Dtest=GenAiUsageTest,GenAiObservabilityTest,GenAiObservabilitySpanTestReview notes (Bugbot + Grok)
AI-generated PR description on behalf of atiaomar1978-hub