Skip to content

Commit 997491c

Browse files
committed
fix(engine): use saturating casts for token counts in streaming
1 parent f3df454 commit 997491c

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

src/cortex-engine/src/streaming.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,25 @@ pub struct StreamTokenUsage {
2626
pub total_tokens: u32,
2727
}
2828

29+
/// Safely convert an i64 token count to u32 with saturation.
30+
/// Negative values clamp to 0, values > u32::MAX clamp to u32::MAX.
31+
#[inline]
32+
fn saturating_i64_to_u32(value: i64) -> u32 {
33+
if value <= 0 {
34+
0
35+
} else if value > u32::MAX as i64 {
36+
u32::MAX
37+
} else {
38+
value as u32
39+
}
40+
}
41+
2942
impl From<crate::client::TokenUsage> for StreamTokenUsage {
3043
fn from(usage: crate::client::TokenUsage) -> Self {
3144
Self {
32-
prompt_tokens: usage.input_tokens as u32,
33-
completion_tokens: usage.output_tokens as u32,
34-
total_tokens: usage.total_tokens as u32,
45+
prompt_tokens: saturating_i64_to_u32(usage.input_tokens),
46+
completion_tokens: saturating_i64_to_u32(usage.output_tokens),
47+
total_tokens: saturating_i64_to_u32(usage.total_tokens),
3548
}
3649
}
3750
}

0 commit comments

Comments
 (0)