agent: Fix compaction being billed as un-cached input tokens (#64938)

## Summary

Compaction currently bills the entire context window as input tokens
instead of cached input tokens, because tool definitions change during
compaction requests. Compaction here is separate from native provider
compaction.

Closes #59528

The fix is only able to be applied to providers which have a way of
disabling tools without changing the tool definitions. This notably does
not include Anthropic.

This PR adds a compaction strategy enum, which describes whether a
provider supports disabling tools without causing a cache miss. The new
variant checks whether the model supports `tool_choice: none` and
includes it in the request, while keeping the tool definitions the same.

A provider may support `tool_choice: none`, but still bust the cache, or
have models which don't support it. So we need both the enum and
`tool_choice: none` check.

## Testing

I've added tests for checking request prefix equality during compaction,
where this new fix is applied.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Fixed compaction being billed as un-cached input tokens
This commit is contained in:
tidely
2026-11-04 13:36:23 +00:00
committed by GitHub
parent ad54befbc6
commit ead5672e16
9 changed files with 362 additions and 61 deletions
@@ -23,8 +23,9 @@ use language_model::{
LanguageModelCompletionEvent, LanguageModelCompletionStream, LanguageModelEffortLevel,
LanguageModelId, LanguageModelName, LanguageModelProviderId, LanguageModelProviderName,
LanguageModelRequest, LanguageModelToolChoiceSupport, ModelRateLimiters, OPEN_AI_PROVIDER_ID,
OPEN_AI_PROVIDER_NAME, ProviderErrorCategory, RateLimiter, X_AI_PROVIDER_ID,
X_AI_PROVIDER_NAME, ZED_CLOUD_PROVIDER_ID, ZED_CLOUD_PROVIDER_NAME, unavailable_error,
OPEN_AI_PROVIDER_NAME, PromptCompactionStrategy, ProviderErrorCategory, RateLimiter,
X_AI_PROVIDER_ID, X_AI_PROVIDER_NAME, ZED_CLOUD_PROVIDER_ID, ZED_CLOUD_PROVIDER_NAME,
unavailable_error,
};
use schemars::JsonSchema;
@@ -546,6 +547,11 @@ pub fn language_model(model: &cloud_llm_client::LanguageModel) -> LanguageModel
none: true,
any: model.provider != Anthropic || anthropic::supports_forced_tool_use(&model.id.0),
},
// Anthropic invalidates cached messages when `tool_choice` changes.
prompt_compaction_strategy: match model.provider {
OpenAi | XAi => PromptCompactionStrategy::PreserveRequestPrefix,
Anthropic | Google => PromptCompactionStrategy::RebuildPrompt,
},
supports_split_token_display: matches!(model.provider, OpenAi | XAi),
max_output_tokens: Some(model.max_output_tokens as u64),
..LanguageModel::new(
@@ -2140,6 +2146,18 @@ mod tests {
);
}
#[test]
fn prompt_compaction_preserves_prefix_only_for_supported_upstreams() {
assert_eq!(
language_model(&cloud_test_config()).prompt_compaction_strategy,
PromptCompactionStrategy::PreserveRequestPrefix
);
assert_eq!(
language_model(&cloud_anthropic_test_config()).prompt_compaction_strategy,
PromptCompactionStrategy::RebuildPrompt
);
}
#[test]
fn config_resolves_current_model_list_and_rejects_unlisted_models() {
let listed = language_model(&cloud_test_config());