mirror of
https://github.com/openai/codex.git
synced 2026-04-29 08:56:38 +00:00
Update context window after model switch (#11520)
- Update token usage aggregation to refresh model context window after a model change. - Add protocol/core tests, including an e2e model-switch test that validates switching to a smaller model updates telemetry.
This commit is contained in:
@@ -1206,6 +1206,9 @@ impl TokenUsageInfo {
|
||||
if let Some(last) = last {
|
||||
info.append_last_usage(last);
|
||||
}
|
||||
if let Some(model_context_window) = model_context_window {
|
||||
info.model_context_window = Some(model_context_window);
|
||||
}
|
||||
Some(info)
|
||||
}
|
||||
|
||||
@@ -2820,4 +2823,46 @@ mod tests {
|
||||
assert_eq!(value["msg"]["cancelled"][0], "c");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_usage_info_new_or_append_updates_context_window_when_provided() {
|
||||
let initial = Some(TokenUsageInfo {
|
||||
total_token_usage: TokenUsage::default(),
|
||||
last_token_usage: TokenUsage::default(),
|
||||
model_context_window: Some(258_400),
|
||||
});
|
||||
let last = Some(TokenUsage {
|
||||
input_tokens: 10,
|
||||
cached_input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
reasoning_output_tokens: 0,
|
||||
total_tokens: 10,
|
||||
});
|
||||
|
||||
let info = TokenUsageInfo::new_or_append(&initial, &last, Some(128_000))
|
||||
.expect("new_or_append should return info");
|
||||
|
||||
assert_eq!(info.model_context_window, Some(128_000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_usage_info_new_or_append_preserves_context_window_when_not_provided() {
|
||||
let initial = Some(TokenUsageInfo {
|
||||
total_token_usage: TokenUsage::default(),
|
||||
last_token_usage: TokenUsage::default(),
|
||||
model_context_window: Some(258_400),
|
||||
});
|
||||
let last = Some(TokenUsage {
|
||||
input_tokens: 10,
|
||||
cached_input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
reasoning_output_tokens: 0,
|
||||
total_tokens: 10,
|
||||
});
|
||||
|
||||
let info = TokenUsageInfo::new_or_append(&initial, &last, None)
|
||||
.expect("new_or_append should return info");
|
||||
|
||||
assert_eq!(info.model_context_window, Some(258_400));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user