diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs index 035d0d286..7277f0b0c 100644 --- a/codex-rs/core/src/context_manager/history.rs +++ b/codex-rs/core/src/context_manager/history.rs @@ -84,9 +84,19 @@ impl ContextManager { .unwrap_or(i64::MAX); let items_tokens = self.items.iter().fold(0i64, |acc, item| { - let serialized = serde_json::to_string(item).unwrap_or_default(); - let item_tokens = i64::try_from(approx_token_count(&serialized)).unwrap_or(i64::MAX); - acc.saturating_add(item_tokens) + acc + match item { + ResponseItem::Reasoning { + encrypted_content: Some(content), + .. + } + | ResponseItem::CompactionSummary { + encrypted_content: content, + } => estimate_reasoning_length(content.len()) as i64, + item => { + let serialized = serde_json::to_string(item).unwrap_or_default(); + i64::try_from(approx_token_count(&serialized)).unwrap_or(i64::MAX) + } + } }); Some(base_tokens.saturating_add(items_tokens)) @@ -145,21 +155,13 @@ impl ContextManager { None } }) - .map(Self::estimate_reasoning_length) + .map(estimate_reasoning_length) .fold(0usize, usize::saturating_add); let token_estimate = approx_tokens_from_byte_count(total_reasoning_bytes); token_estimate as usize } - fn estimate_reasoning_length(encoded_len: usize) -> usize { - encoded_len - .saturating_mul(3) - .checked_div(4) - .unwrap_or(0) - .saturating_sub(650) - } - pub(crate) fn get_total_token_usage(&self) -> i64 { self.token_info .as_ref() @@ -247,6 +249,14 @@ fn is_api_message(message: &ResponseItem) -> bool { } } +fn estimate_reasoning_length(encoded_len: usize) -> usize { + encoded_len + .saturating_mul(3) + .checked_div(4) + .unwrap_or(0) + .saturating_sub(650) +} + #[cfg(test)] #[path = "history_tests.rs"] mod tests;