Use Anthropic capability metadata and update model compatibility (#67203)

- Read compaction support and accepted thinking modes from the Anthropic
Models API.
- Infer system-message updates, preserved-thinking prefix binding,
adaptive-only thinking, and temperature restrictions from Claude family
and version. Keep fast mode and forced tool choice on explicit model
lists.
- Use an advertised thinking-off mode when available; otherwise retain
model-specific fallbacks for missing metadata and let models without an
off mode use their default.
- Cover family cutoffs, future model IDs, and request serialization in
regression tests.

Validation: 49 Anthropic unit tests, `cargo check -p language_models`,
formatting, and `git diff --check` passed.

Release Notes:

- Improved compatibility with new Claude models and kept Inline Assist
working on models that cannot turn thinking off.
This commit is contained in:
Bennet Bo Fenner
2026-11-20 12:48:18 +00:00
committed by GitHub
parent 87e6a02e1e
commit ad1058d78d
7 changed files with 502 additions and 182 deletions
+245 -102
View File
@@ -22,7 +22,18 @@ pub mod completion;
pub const ANTHROPIC_API_URL: &str = "https://api.anthropic.com";
pub const FAST_MODE_BETA_HEADER: &str = "fast-mode-2026-02-01";
pub const FABLE_MODEL_ID_PREFIX: &str = "claude-fable-5";
pub const FABLE_FALLBACK_MODEL_ID: &str = "claude-opus-4-8";
pub const THINKING_BINDING_CONTROLS_BETA_HEADER: &str = "thinking-binding-controls-2026-08-01";
/// <https://platform.claude.com/docs/en/build-with-claude/compaction>
pub const COMPACTION_BETA_HEADER: &str = "compact-2026-01-12";
/// The smallest input-token trigger Anthropic accepts for compaction.
pub const MIN_COMPACTION_TRIGGER_TOKENS: u64 = 50_000;
/// Fast mode availability does not follow model version order.
/// <https://platform.claude.com/docs/en/build-with-claude/fast-mode#feature-availability>
pub fn supports_fast_mode(model_id: &str) -> bool {
matches!(
model_id,
@@ -34,37 +45,54 @@ pub fn supports_fast_mode(model_id: &str) -> bool {
)
}
/// Model IDs where adaptive thinking runs by default when a request omits the
/// `thinking` field, and where thinking must instead be turned off with an
/// explicit `thinking: {"type": "disabled"}`.
/// Explicit opt-out for models that think by default, used when the Models API
/// doesn't advertise an off mode. Keep this model-specific: Opus 5 accepts
/// `disabled`, but Opus 5.5 rejects it.
///
/// On earlier Opus models omitting `thinking` means thinking is off; Claude
/// Opus 5 flipped that default, as did Claude Haiku 5.5. Claude Fable 5 and
/// Claude Mythos 5 also think by default, but they reject `{"type":
/// "disabled"}` with a 400 error, so they are deliberately excluded here
/// (thinking cannot be turned off for them at all).
///
/// <https://platform.claude.com/docs/en/about-claude/models/migration-guide#migrating-to-claude-opus-5>
pub fn requires_explicit_thinking_opt_out(model_id: &str) -> bool {
matches!(model_id, "claude-opus-5" | "claude-haiku-5-5")
/// <https://platform.claude.com/docs/en/build-with-claude/thinking#turning-thinking-off>
fn thinking_opt_out_fallback(model_id: &str) -> Option<Thinking> {
match model_id {
"claude-sonnet-5-5" => Some(Thinking::BetweenTools),
"claude-opus-5" | "claude-sonnet-5" | "claude-haiku-5-5" => Some(Thinking::Disabled),
_ => None,
}
}
pub const FABLE_MODEL_ID_PREFIX: &str = "claude-fable-5";
pub const FABLE_FALLBACK_MODEL_ID: &str = "claude-opus-4-8";
pub const THINKING_BINDING_CONTROLS_BETA_HEADER: &str = "thinking-binding-controls-2026-08-01";
fn requires_adaptive_thinking(model_id: &str) -> bool {
model_version_at_least(model_id, |family| match family {
"opus" => Some((4, 7)),
"sonnet" | "fable" | "mythos" => Some((5, 0)),
"haiku" => Some((5, 5)),
_ => None,
})
}
// Mythos Preview accepts manual thinking but still rejects non-default temperature.
// <https://platform.claude.com/docs/en/build-with-claude/thinking#sampling-parameters>
fn rejects_custom_temperature(model_id: &str) -> bool {
if model_id == "claude-mythos-preview" {
return true;
}
model_version_at_least(model_id, |family| match family {
"opus" => Some((4, 7)),
"sonnet" | "fable" | "mythos" => Some((5, 0)),
"haiku" => Some((5, 5)),
_ => None,
})
}
/// <https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks#outcomes>
pub fn binds_thinking_blocks_to_prefix(model_id: &str) -> bool {
matches!(
model_id,
"claude-fable-5-2"
| "claude-mythos-5-2"
| "claude-opus-5-5"
| "claude-fable-5-1"
| "claude-sonnet-5-5"
| "claude-haiku-5-5"
)
model_version_at_least(model_id, |family| match family {
"opus" | "sonnet" | "haiku" => Some((5, 5)),
"fable" | "mythos" => Some((5, 1)),
_ => None,
})
}
/// Checks for models that reject forced tool choice on every request, not
/// request-specific restrictions such as manual extended thinking.
/// <https://platform.claude.com/docs/en/build-with-claude/thinking#response-prefill-and-forced-tool-use>
pub fn supports_forced_tool_use(model_id: &str) -> bool {
!matches!(
model_id,
@@ -79,29 +107,37 @@ pub fn supports_forced_tool_use(model_id: &str) -> bool {
/// Whether this model accepts system messages inside conversation history.
///
/// Older models retain the flattened system-prompt representation.
/// Infer support for newer versions of known model families. The cutoff is
/// family-specific: Sonnet 5 lacks support even though Opus 4.8 has it.
/// <https://platform.claude.com/docs/en/build-with-claude/mid-conversation-system-messages>
pub fn supports_mid_conversation_system_messages(model_id: &str) -> bool {
matches!(
model_id,
"claude-fable-5-1"
| "claude-mythos-5-1"
| "claude-fable-5"
| "claude-mythos-5"
| "claude-mythos-preview"
| "claude-opus-5-5"
| "claude-opus-5"
| "claude-opus-4-8"
| "claude-sonnet-5-5"
| "claude-haiku-5-5"
)
if model_id == "claude-mythos-preview" {
return true;
}
model_version_at_least(model_id, |family| match family {
"opus" => Some((4, 8)),
"sonnet" | "haiku" => Some((5, 5)),
"fable" | "mythos" => Some((5, 0)),
_ => None,
})
}
/// <https://platform.claude.com/docs/en/build-with-claude/compaction>
pub const COMPACTION_BETA_HEADER: &str = "compact-2026-01-12";
fn model_version_at_least(
model_id: &str,
minimum_version: impl FnOnce(&str) -> Option<(u8, u8)>,
) -> bool {
let Some((family, major, minor)) = parse_claude_model_version(model_id) else {
return false;
};
minimum_version(family).is_some_and(|minimum| (major, minor) >= minimum)
}
/// The smallest input-token trigger Anthropic accepts for compaction.
pub const MIN_COMPACTION_TRIGGER_TOKENS: u64 = 50_000;
fn parse_claude_model_version(model_id: &str) -> Option<(&str, u8, u8)> {
let (family, version) = model_id.strip_prefix("claude-")?.split_once('-')?;
let (major, minor) = version.split_once('-').unwrap_or((version, "0"));
// Only infer from plain versions, not dated snapshots or custom aliases.
Some((family, major.parse().ok()?, minor.parse().ok()?))
}
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
@@ -117,6 +153,8 @@ pub enum AnthropicModelMode {
/// Capabilities reported by the Anthropic models endpoint for a given model.
#[derive(Clone, Debug, Default, Deserialize)]
pub struct ModelCapabilities {
#[serde(default)]
pub context_management: Option<ContextManagementCapability>,
#[serde(default)]
pub thinking: Option<ThinkingCapability>,
#[serde(default)]
@@ -125,12 +163,20 @@ pub struct ModelCapabilities {
pub effort: Option<EffortCapability>,
}
#[derive(Clone, Debug, Default, Deserialize)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct SupportedCapability {
#[serde(default)]
pub supported: bool,
}
#[derive(Clone, Debug, Default, Deserialize)]
pub struct ContextManagementCapability {
#[serde(default)]
pub supported: bool,
#[serde(default)]
pub compact_20260112: Option<SupportedCapability>,
}
#[derive(Clone, Debug, Default, Deserialize)]
pub struct ThinkingCapability {
#[serde(default)]
@@ -139,12 +185,16 @@ pub struct ThinkingCapability {
pub types: Option<ThinkingTypes>,
}
#[derive(Clone, Debug, Default, Deserialize)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct ThinkingTypes {
#[serde(default)]
pub adaptive: SupportedCapability,
#[serde(default)]
pub enabled: SupportedCapability,
#[serde(default)]
pub disabled: Option<SupportedCapability>,
#[serde(default)]
pub between_tools: Option<SupportedCapability>,
}
#[derive(Clone, Debug, Default, Deserialize)]
@@ -171,6 +221,7 @@ pub struct Model {
pub max_output_tokens: u64,
pub default_temperature: f32,
pub mode: AnthropicModelMode,
pub thinking_types: Option<ThinkingTypes>,
pub supports_thinking: bool,
pub supports_adaptive_thinking: bool,
pub supports_images: bool,
@@ -187,19 +238,15 @@ pub struct Model {
impl Model {
/// Construct a `Model` from an entry returned by the `/v1/models` listing endpoint.
pub fn from_listed(entry: ListModelEntry) -> Self {
let supports_thinking = entry
let thinking = entry
.capabilities
.as_ref()
.and_then(|t| t.thinking.as_ref())
.map(|t| t.supported)
.unwrap_or(false);
let supports_adaptive_thinking = entry
.capabilities
.and_then(|capabilities| capabilities.thinking.as_ref());
let supports_thinking = thinking.is_some_and(|thinking| thinking.supported);
let thinking_types = thinking.and_then(|thinking| thinking.types.clone());
let supports_adaptive_thinking = thinking_types
.as_ref()
.and_then(|t| t.thinking.as_ref())
.and_then(|t| t.types.as_ref())
.map(|types| types.adaptive.supported)
.unwrap_or(false);
.is_some_and(|types| types.adaptive.supported);
let supports_images = entry
.capabilities
.as_ref()
@@ -224,7 +271,11 @@ impl Model {
let mode = if supports_adaptive_thinking {
AnthropicModelMode::AdaptiveThinking
} else if supports_thinking {
} else if supports_thinking
&& thinking_types
.as_ref()
.is_none_or(|types| types.enabled.supported)
{
AnthropicModelMode::Thinking {
budget_tokens: Some(4_096),
}
@@ -234,26 +285,17 @@ impl Model {
let supports_speed = supports_fast_mode(&entry.id);
// <https://platform.claude.com/docs/en/build-with-claude/compaction#supported-models>
let supports_compaction = matches!(
entry.id.as_str(),
"claude-fable-5-2"
| "claude-mythos-5-2"
| "claude-opus-5-5"
| "claude-fable-5-1"
| "claude-fable-5"
| "claude-mythos-5-1"
| "claude-mythos-5"
| "claude-mythos-preview"
| "claude-opus-5"
| "claude-opus-4-8"
| "claude-opus-4-7"
| "claude-opus-4-6"
| "claude-sonnet-5-5"
| "claude-sonnet-5"
| "claude-sonnet-4-6"
| "claude-haiku-5-5"
);
let supports_compaction = entry
.capabilities
.as_ref()
.and_then(|capabilities| capabilities.context_management.as_ref())
.is_some_and(|management| {
management.supported
&& management
.compact_20260112
.as_ref()
.is_some_and(|compaction| compaction.supported)
});
let mut extra_beta_headers = Vec::new();
if supports_speed {
@@ -273,6 +315,7 @@ impl Model {
max_output_tokens: entry.max_tokens,
default_temperature: 1.0,
mode,
thinking_types,
supports_thinking,
supports_adaptive_thinking,
supports_images,
@@ -940,9 +983,8 @@ pub enum Thinking {
#[serde(default, skip_serializing_if = "Option::is_none")]
block_binding: Option<ThinkingBlockBinding>,
},
/// Explicitly turns thinking off. Required by models where thinking runs
/// by default (see [`requires_explicit_thinking_opt_out`]); only accepted
/// at effort `high` or below.
/// Explicitly turns thinking off. Some models only accept it at effort
/// `high` or below.
Disabled,
/// The lowest thinking setting, for models that think by default but
/// reject [`Thinking::Disabled`] (Claude Sonnet 5.5).
@@ -1855,6 +1897,7 @@ mod tests {
types: Some(ThinkingTypes {
adaptive: SupportedCapability { supported: true },
enabled: SupportedCapability { supported: true },
..Default::default()
}),
}),
..Default::default()
@@ -1876,6 +1919,7 @@ mod tests {
types: Some(ThinkingTypes {
adaptive: SupportedCapability { supported: false },
enabled: SupportedCapability { supported: true },
..Default::default()
}),
}),
..Default::default()
@@ -1896,10 +1940,74 @@ mod tests {
assert_eq!(model.mode, AnthropicModelMode::Default);
}
#[test]
fn thinking_prefix_binding_uses_family_specific_version_cutoffs() {
for (model_id, expected) in [
("claude-opus-5-4", false),
("claude-opus-5-5", true),
("claude-opus-5-10", true),
("claude-sonnet-5", false),
("claude-sonnet-5-5", true),
("claude-sonnet-6", true),
("claude-haiku-5-4", false),
("claude-haiku-5-5", true),
("claude-haiku-6", true),
("claude-fable-5", false),
("claude-fable-5-1", true),
("claude-fable-6", true),
("claude-mythos-preview", false),
("claude-mythos-5", false),
("claude-mythos-5-1", true),
("claude-mythos-6", true),
("claude-opus-5-5-20260921", false),
("claude-opus-latest", false),
("claude-unknown-6", false),
] {
assert_eq!(
binds_thinking_blocks_to_prefix(model_id),
expected,
"{model_id}"
);
}
}
#[test]
fn system_message_support_uses_family_specific_version_cutoffs() {
for (model_id, expected) in [
("claude-opus-4-7", false),
("claude-opus-4-8", true),
("claude-opus-4-10", true),
("claude-opus-5", true),
("claude-sonnet-5", false),
("claude-sonnet-5-4", false),
("claude-sonnet-5-5", true),
("claude-sonnet-6", true),
("claude-haiku-4-5-20251001", false),
("claude-haiku-5-4", false),
("claude-haiku-5-5", true),
("claude-haiku-6", true),
("claude-fable-5", true),
("claude-fable-6", true),
("claude-mythos-5", true),
("claude-mythos-6", true),
("claude-mythos-preview", true),
("claude-opus-4-20250514", false),
("claude-opus-latest", false),
("claude-unknown-6", false),
("custom-opus-6", false),
] {
assert_eq!(
supports_mid_conversation_system_messages(model_id),
expected,
"{model_id}"
);
}
}
#[test]
fn from_listed_enables_fast_mode_and_compaction_for_supported_opus_models() {
for model_id in ["claude-opus-5-5", "claude-opus-5", "claude-opus-4-8"] {
let model = Model::from_listed(listed_entry(model_id, ModelCapabilities::default()));
let model = Model::from_listed(listed_entry(model_id, compaction_capabilities()));
assert!(model.supports_speed);
let beta_headers = model
@@ -1911,34 +2019,24 @@ mod tests {
}
#[test]
fn from_listed_enables_compaction_and_binding_controls_for_fable_5_1() {
let model = Model::from_listed(listed_entry(
"claude-fable-5-1",
ModelCapabilities::default(),
));
let beta_headers = model
.beta_headers()
.expect("model should have beta headers");
assert!(beta_headers.contains(COMPACTION_BETA_HEADER));
assert!(beta_headers.contains(THINKING_BINDING_CONTROLS_BETA_HEADER));
// Mythos 5.1 supports compaction but doesn't run the prefix-binding
// check, so it must not get the binding-controls header.
let model = Model::from_listed(listed_entry(
"claude-mythos-5-1",
ModelCapabilities::default(),
));
let beta_headers = model
.beta_headers()
.expect("model should have beta headers");
assert!(beta_headers.contains(COMPACTION_BETA_HEADER));
assert!(!beta_headers.contains(THINKING_BINDING_CONTROLS_BETA_HEADER));
fn from_listed_enables_compaction_and_binding_controls_for_5_1_models() {
for model_id in ["claude-fable-5-1", "claude-mythos-5-1"] {
let model = Model::from_listed(listed_entry(model_id, compaction_capabilities()));
let beta_headers = model
.beta_headers()
.expect("model should have beta headers");
assert!(beta_headers.contains(COMPACTION_BETA_HEADER), "{model_id}");
assert!(
beta_headers.contains(THINKING_BINDING_CONTROLS_BETA_HEADER),
"{model_id}"
);
}
}
#[test]
fn from_listed_enables_fast_mode_compaction_and_binding_controls_for_5_2_models() {
for model_id in ["claude-fable-5-2", "claude-mythos-5-2"] {
let model = Model::from_listed(listed_entry(model_id, ModelCapabilities::default()));
let model = Model::from_listed(listed_entry(model_id, compaction_capabilities()));
assert!(model.supports_speed, "{model_id} supports fast mode");
assert!(model.supports_compaction, "{model_id} supports compaction");
@@ -1955,6 +2053,51 @@ mod tests {
}
}
fn compaction_capabilities() -> ModelCapabilities {
serde_json::from_value(serde_json::json!({
"context_management": {
"supported": true,
"compact_20260112": { "supported": true }
}
}))
.expect("valid capabilities")
}
#[test]
fn compaction_uses_advertised_capabilities_not_model_names() {
let model = Model::from_listed(listed_entry("new-model", compaction_capabilities()));
assert!(model.supports_compaction);
assert_eq!(
model.beta_headers().as_deref(),
Some(COMPACTION_BETA_HEADER)
);
for capabilities in [
serde_json::json!({}),
serde_json::json!({"context_management": null}),
serde_json::json!({"context_management": {
"supported": true,
"compact_20260112": {"supported": false}
}}),
serde_json::json!({"context_management": {
"supported": false,
"compact_20260112": {"supported": true}
}}),
] {
let model = Model::from_listed(listed_entry(
"claude-opus-5-5",
serde_json::from_value(capabilities).expect("valid capabilities"),
));
assert!(!model.supports_compaction);
assert!(
!model
.beta_headers()
.unwrap_or_default()
.contains(COMPACTION_BETA_HEADER)
);
}
}
#[test]
fn from_listed_collects_supported_effort_levels() {
let entry = listed_entry(
+230 -75
View File
@@ -286,18 +286,24 @@ pub fn into_anthropic(
default_temperature: f32,
max_output_tokens: u64,
mode: AnthropicModelMode,
thinking_types: Option<&crate::ThinkingTypes>,
cache_mode: AnthropicPromptCacheMode,
compaction_state_owner: &LanguageModelProviderId,
) -> Result<crate::Request> {
let requires_adaptive_thinking = matches!(
model.as_str(),
"claude-fable-5-2"
| "claude-mythos-5-2"
| "claude-opus-5-5"
| "claude-sonnet-5-5"
| "claude-haiku-5-5"
);
let mode = if requires_adaptive_thinking {
let mode = if let Some(types) = thinking_types {
if types.adaptive.supported {
AnthropicModelMode::AdaptiveThinking
} else if types.enabled.supported {
match mode {
AnthropicModelMode::Thinking { .. } => mode,
_ => AnthropicModelMode::Thinking {
budget_tokens: Some(4_096),
},
}
} else {
AnthropicModelMode::Default
}
} else if crate::requires_adaptive_thinking(&model) {
AnthropicModelMode::AdaptiveThinking
} else {
mode
@@ -451,24 +457,25 @@ pub fn into_anthropic(
}),
AnthropicModelMode::Default => None,
}
} else if model == "claude-sonnet-5-5" {
Some(Thinking::BetweenTools)
} else if crate::requires_explicit_thinking_opt_out(&model) {
// On Claude Opus 5 and Haiku 5.5, omitting the `thinking` field no longer means
// "off": the model runs adaptive thinking by default, so features
// that suppress thinking (e.g. inline assist) must opt out
// explicitly. `disabled` is only accepted at effort `high` or below;
// that holds here because `output_config` is never sent when thinking
// is disallowed, and the server-side default effort is `high` or below.
// <https://platform.claude.com/docs/en/about-claude/models/migration-guide#migrating-to-claude-opus-5>
Some(Thinking::Disabled)
} else if let Some(types) = thinking_types
&& (types.disabled.is_some() || types.between_tools.is_some())
{
if types.disabled.as_ref().is_some_and(|mode| mode.supported) {
Some(Thinking::Disabled)
} else if types
.between_tools
.as_ref()
.is_some_and(|mode| mode.supported)
{
Some(Thinking::BetweenTools)
} else {
None
}
} else {
None
crate::thinking_opt_out_fallback(&model)
};
// These models reject non-default sampling controls even when thinking is off.
// <https://platform.claude.com/docs/en/build-with-claude/thinking#sampling-parameters>
let temperature = if requires_adaptive_thinking {
let temperature = if crate::rejects_custom_temperature(&model) {
None
} else {
request.temperature.or(Some(default_temperature))
@@ -990,6 +997,7 @@ mod tests {
0.7,
4096,
AnthropicModelMode::Default,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1085,6 +1093,7 @@ mod tests {
0.7,
4096,
AnthropicModelMode::Default,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1104,6 +1113,7 @@ mod tests {
0.7,
4096,
AnthropicModelMode::Default,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1162,6 +1172,7 @@ mod tests {
0.7,
4096,
AnthropicModelMode::Default,
None,
AnthropicPromptCacheMode::Legacy,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1228,6 +1239,7 @@ mod tests {
1.0,
128_000,
AnthropicModelMode::AdaptiveThinking,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1241,41 +1253,202 @@ mod tests {
);
}
#[test]
fn test_advertised_thinking_types() {
for (types, thinking_allowed, expected) in [
(
serde_json::json!({"adaptive": {"supported": true}}),
true,
Some("adaptive"),
),
(
serde_json::json!({"enabled": {"supported": true}}),
true,
Some("enabled"),
),
(serde_json::json!({}), true, None),
(
serde_json::json!({"disabled": {"supported": true}}),
false,
Some("disabled"),
),
(
serde_json::json!({
"disabled": {"supported": true},
"between_tools": {"supported": true}
}),
false,
Some("disabled"),
),
(
serde_json::json!({
"disabled": {"supported": false},
"between_tools": {"supported": true}
}),
false,
Some("between_tools"),
),
(
serde_json::json!({
"disabled": {"supported": false},
"between_tools": {"supported": false}
}),
false,
None,
),
] {
let types = serde_json::from_value(types).expect("valid thinking types");
for model in ["unknown-model", "claude-opus-5-5"] {
let request = LanguageModelRequest {
thinking_allowed,
thinking_effort: Some("max".into()),
..Default::default()
};
let request = into_anthropic(
request,
model.to_string(),
1.0,
64_000,
AnthropicModelMode::Default,
Some(&types),
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
.expect("request should use a supported thinking mode or the model default");
let json = serde_json::to_value(&request).expect("valid request");
assert_eq!(json["thinking"]["type"].as_str(), expected);
if expected.is_none() {
assert!(json.get("thinking").is_none());
}
if !thinking_allowed {
assert!(request.output_config.is_none());
assert!(json["thinking"]["block_binding"].is_null());
}
let count = serde_json::to_value(request.into_count_tokens_request())
.expect("valid token count request");
assert_eq!(count["thinking"], json["thinking"]);
}
}
}
#[test]
fn test_model_fallback_thinking_and_sampling_restrictions() {
for (model, requires_adaptive, accepts_temperature) in [
("claude-fable-5-2", true, false),
("claude-mythos-5-2", true, false),
("claude-fable-5-1", true, false),
("claude-mythos-5-1", true, false),
("claude-fable-5", true, false),
("claude-mythos-5", true, false),
("claude-opus-5-5", true, false),
("claude-opus-5", true, false),
("claude-opus-4-8", true, false),
("claude-opus-4-7", true, false),
("claude-sonnet-5-5", true, false),
("claude-sonnet-5", true, false),
("claude-haiku-5-5", true, false),
("claude-mythos-preview", false, false),
("claude-opus-4-6", false, true),
("claude-haiku-5-4", false, true),
("claude-opus-4-10", true, false),
("claude-sonnet-6", true, false),
("claude-haiku-6", true, false),
("claude-fable-6", true, false),
("claude-mythos-6", true, false),
("claude-unknown-6", false, true),
("claude-opus-latest", false, true),
] {
for thinking_allowed in [false, true] {
let request = into_anthropic(
LanguageModelRequest {
thinking_allowed,
temperature: Some(0.7),
..Default::default()
},
model.to_string(),
1.0,
16_000,
AnthropicModelMode::Thinking {
budget_tokens: Some(4_096),
},
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
.expect("fallback request");
assert_eq!(
request.temperature,
accepts_temperature.then_some(0.7),
"{model}, thinking_allowed={thinking_allowed}"
);
if thinking_allowed {
assert!(
matches!(
(requires_adaptive, request.thinking),
(true, Some(Thinking::Adaptive { .. }))
| (false, Some(Thinking::Enabled { .. }))
),
"{model}, requires_adaptive={requires_adaptive}"
);
}
}
}
}
#[test]
fn test_latest_models_preserve_system_updates() {
for model in ["claude-fable-5-2", "claude-mythos-5-2"] {
let request = into_anthropic(
LanguageModelRequest {
messages: [Role::User, Role::System]
.into_iter()
.map(|role| LanguageModelRequestMessage {
role,
content: vec![MessageContent::Text("Update".into())],
cache: false,
reasoning_details: None,
})
.collect(),
thinking_allowed: true,
..Default::default()
},
model.to_string(),
1.0,
16_000,
AnthropicModelMode::Default,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
.expect("request with system update");
assert!(request.system.is_none());
assert_eq!(
request
.messages
.iter()
.map(|message| &message.role)
.collect::<Vec<_>>(),
vec![&crate::Role::User, &crate::Role::System],
"{model}"
);
}
}
#[test]
fn test_block_binding_sent_only_for_prefix_binding_models() {
// (model, expects_block_binding): Claude Fable 5.1 rejects a replayed
// thinking block with a 400 when the conversation prefix changed, so
// its requests opt into dropping invalidated blocks instead. Models
// without the prefix-binding check must not receive the beta field.
// Opt into dropping invalidated blocks only on models that check the prefix.
for (model, expects_block_binding) in [
("claude-fable-5-2", true),
("claude-mythos-5-2", true),
("claude-opus-5-5", true),
("claude-fable-5-1", true),
("claude-fable-5", false),
("claude-mythos-5-1", false),
("claude-mythos-5-1", true),
("claude-opus-5", false),
] {
let request = LanguageModelRequest {
messages: vec![LanguageModelRequestMessage {
role: Role::User,
content: vec![MessageContent::Text("Hi".to_string())],
cache: false,
reasoning_details: None,
}],
thread_id: None,
prompt_cache_key: None,
prompt_id: None,
intent: None,
stop: vec![],
temperature: None,
tools: vec![],
tool_choice: None,
thinking_allowed: true,
thinking_effort: None,
speed: None,
compact_at_tokens: None,
max_output_tokens: None,
..Default::default()
};
let anthropic_request = into_anthropic(
@@ -1284,6 +1457,7 @@ mod tests {
1.0,
128_000,
AnthropicModelMode::AdaptiveThinking,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1324,6 +1498,8 @@ mod tests {
// outright, so those models must keep omitting the field.
for (model, expects_explicit_opt_out) in [
("claude-opus-5", true),
("claude-sonnet-5", true),
("claude-haiku-5-5", true),
("claude-fable-5-2", false),
("claude-mythos-5-2", false),
("claude-opus-5-5", false),
@@ -1331,25 +1507,8 @@ mod tests {
("claude-fable-5", false),
] {
let request = LanguageModelRequest {
messages: vec![LanguageModelRequestMessage {
role: Role::User,
content: vec![MessageContent::Text("Hi".to_string())],
cache: false,
reasoning_details: None,
}],
thread_id: None,
prompt_cache_key: None,
prompt_id: None,
intent: None,
stop: vec![],
temperature: None,
tools: vec![],
tool_choice: None,
thinking_allowed: false,
thinking_effort: None,
speed: None,
compact_at_tokens: None,
max_output_tokens: None,
thinking_effort: Some("max".into()),
..Default::default()
};
let anthropic_request = into_anthropic(
@@ -1358,6 +1517,7 @@ mod tests {
1.0,
128_000,
AnthropicModelMode::AdaptiveThinking,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1380,15 +1540,6 @@ mod tests {
"{model} should omit the thinking field entirely"
);
}
if matches!(
model,
"claude-opus-5-5" | "claude-fable-5-2" | "claude-mythos-5-2"
) {
assert!(
anthropic_request.temperature.is_none(),
"{model} must omit temperature because adaptive thinking is always on"
);
}
}
}
@@ -1424,6 +1575,7 @@ mod tests {
1.0,
128_000,
AnthropicModelMode::AdaptiveThinking,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1488,6 +1640,7 @@ mod tests {
0.7,
4096,
AnthropicModelMode::Default,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1541,6 +1694,7 @@ mod tests {
AnthropicModelMode::Thinking {
budget_tokens: Some(10000),
},
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)
@@ -1663,6 +1817,7 @@ mod tests {
1.0,
4096,
AnthropicModelMode::Default,
None,
AnthropicPromptCacheMode::Disabled,
&ANTHROPIC_PROVIDER_ID,
)
+1
View File
@@ -153,6 +153,7 @@ pub fn stream_completion(
} else {
AnthropicModelMode::Default
},
None,
AnthropicPromptCacheMode::Legacy,
&PROVIDER_ID,
)?;
@@ -399,13 +399,17 @@ impl LanguageModelClient for AnthropicLanguageModelProvider {
};
let request_limiter = self.request_limiters.for_model(&model.id);
let has_tools = !request.tools.is_empty();
let request_id = config.request_id(has_tools).to_string();
let request_id = config.request_id(has_tools);
let mut request = match into_anthropic(
request,
request_id,
request_id.to_string(),
config.default_temperature,
config.max_output_tokens,
config.mode.clone(),
config
.thinking_types
.as_ref()
.filter(|_| request_id == config.id),
AnthropicPromptCacheMode::Automatic,
&PROVIDER_ID,
) {
@@ -439,13 +443,17 @@ impl LanguageModelClient for AnthropicLanguageModelProvider {
Err(error) => return async move { Err(error) }.boxed(),
};
let request_limiter = self.request_limiters.for_model(&model.id);
let request_id = config.request_id(!request.tools.is_empty()).to_string();
let request_id = config.request_id(!request.tools.is_empty());
let request = into_anthropic(
request,
request_id,
request_id.to_string(),
config.default_temperature,
config.max_output_tokens,
config.mode.clone(),
config
.thinking_types
.as_ref()
.filter(|_| request_id == config.id),
AnthropicPromptCacheMode::Automatic,
&PROVIDER_ID,
);
@@ -505,6 +513,7 @@ impl LanguageModelClient for AnthropicLanguageModelProvider {
config.default_temperature,
config.max_output_tokens,
config.mode.clone(),
config.thinking_types.as_ref(),
AnthropicPromptCacheMode::Automatic,
&PROVIDER_ID,
) {
@@ -597,6 +606,7 @@ fn available_model_to_anthropic_model(available: &AvailableModel) -> anthropic::
max_output_tokens: available.max_output_tokens.unwrap_or(4_096),
default_temperature: available.default_temperature.unwrap_or(1.0),
mode,
thinking_types: None,
supports_thinking,
supports_adaptive_thinking,
supports_images: true,
@@ -965,7 +975,13 @@ mod tests {
display_name: "Claude Opus 4.6".to_string(),
max_input_tokens: 1_000_000,
max_tokens: 128_000,
capabilities: None,
capabilities: Some(anthropic::ModelCapabilities {
context_management: Some(anthropic::ContextManagementCapability {
supported: true,
compact_20260112: Some(anthropic::SupportedCapability { supported: true }),
}),
..Default::default()
}),
});
provider
.state
@@ -73,6 +73,7 @@ fn available_model_to_anthropic_model(available: &AvailableModel) -> anthropic::
max_output_tokens: available.max_output_tokens.unwrap_or(4_096),
default_temperature: available.default_temperature.unwrap_or(1.0),
mode,
thinking_types: None,
supports_thinking,
supports_adaptive_thinking,
supports_images: available.capabilities.images,
@@ -404,6 +405,7 @@ impl LanguageModelClient for AnthropicCompatibleLanguageModelProvider {
config.default_temperature,
config.max_output_tokens,
config.mode.clone(),
None,
cache_mode,
&self.id,
) {
@@ -352,6 +352,7 @@ fn anthropic_request(
} else {
AnthropicModelMode::Default
},
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
)?;
@@ -742,6 +743,7 @@ impl<TP: CloudLlmTokenProvider + 'static> CloudModelProvider<TP> {
1.0,
config.max_output_tokens as u64,
AnthropicModelMode::Default,
None,
AnthropicPromptCacheMode::Automatic,
&ANTHROPIC_PROVIDER_ID,
) {
+1
View File
@@ -55,6 +55,7 @@ pub async fn stream_completion(
1.0,
model.max_output_tokens().unwrap_or(8192),
mode,
None,
AnthropicPromptCacheMode::Automatic,
&config.provider_id,
)?;