From 1fc72c647fd52e3e73d4309c3b568d4d5fe012b5 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Thu, 15 Jan 2026 19:48:11 -0800 Subject: [PATCH] Fix token estimate during compaction (#9337) This addresses #9287 --- codex-rs/core/src/context_manager/history.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs index 4ee2e252c..2279beef2 100644 --- a/codex-rs/core/src/context_manager/history.rs +++ b/codex-rs/core/src/context_manager/history.rs @@ -97,7 +97,11 @@ impl ContextManager { } | ResponseItem::Compaction { encrypted_content: content, - } => estimate_reasoning_length(content.len()) as i64, + } => { + let reasoning_bytes = estimate_reasoning_length(content.len()); + i64::try_from(approx_tokens_from_byte_count(reasoning_bytes)) + .unwrap_or(i64::MAX) + } item => { let serialized = serde_json::to_string(item).unwrap_or_default(); i64::try_from(approx_token_count(&serialized)).unwrap_or(i64::MAX)