Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

use adaptive thinking for haiku 5.x (#3756)

kt1cc6103a4caa
diff --git a/crates/pa-ai/src/providers/anthropic/mod.rs b/crates/pa-ai/src/providers/anthropic/mod.rs
index e54caa9522..a5db5f927a 100644
--- a/crates/pa-ai/src/providers/anthropic/mod.rs
+++ b/crates/pa-ai/src/providers/anthropic/mod.rs
@@ -232,7 +232,8 @@ pub(crate) fn is_always_on_adaptive_thinking_model(model_id: &str) -> bool {
|| model_id.contains("opus-5.5")
}
-/// Check if a model supports adaptive thinking (Opus 4.6+, Sonnet 4.6+).
+/// Check if a model supports adaptive thinking (Opus 4.6+, Sonnet 4.6+, Haiku 5+). Haiku 5.x
+/// rejects `thinking: {type: "enabled", budget_tokens}` with a 400, like the other 5.x families.
pub(crate) fn supports_adaptive_thinking(model_id: &str) -> bool {
model_id.contains("opus-4-6")
|| model_id.contains("opus-4.6")
@@ -244,6 +245,7 @@ pub(crate) fn supports_adaptive_thinking(model_id: &str) -> bool {
|| model_id.contains("sonnet-4-6")
|| model_id.contains("sonnet-4.6")
|| model_id.contains("sonnet-5")
+ || model_id.contains("haiku-5")
|| model_id.contains("fable-5")
|| model_id.contains("mythos-5")
|| model_id.contains("mythos-preview")
diff --git a/crates/pa-ai/src/providers/anthropic/params.rs b/crates/pa-ai/src/providers/anthropic/params.rs
index bedf9e963e..d0673f4b82 100644
--- a/crates/pa-ai/src/providers/anthropic/params.rs
+++ b/crates/pa-ai/src/providers/anthropic/params.rs
@@ -146,3 +146,78 @@ pub(crate) fn build_params(
Value::Object(params)
}
+
+#[cfg(test)]
+mod tests {
+ use super::build_params;
+ use crate::providers::anthropic::{AnthropicEffort, AnthropicOptions};
+ use crate::types::{
+ zero_model_cost, Context, Message, Model, ModelInput, UserMessage, UserMessageContent,
+ };
+ use serde_json::{json, Map};
+
+ fn thinking_params(model_id: &str) -> (serde_json::Value, serde_json::Value) {
+ let model = Model {
+ id: model_id.into(),
+ name: model_id.into(),
+ api: "anthropic-messages".into(),
+ provider: "anthropic".into(),
+ base_url: "https://api.anthropic.com".into(),
+ reasoning: true,
+ thinking_level_map: None,
+ input: vec![ModelInput::Text],
+ cost: zero_model_cost(),
+ context_window: 200_000,
+ max_tokens: 64_000,
+ max_tokens_explicit: false,
+ featured: None,
+ headers: None,
+ compat: None,
+ };
+ let context = Context {
+ system_prompt: None,
+ messages: vec![Message::User(UserMessage {
+ content: UserMessageContent::Text("Hi".into()),
+ timestamp: 1,
+ rest: Map::default(),
+ })],
+ tools: None,
+ };
+ let options = AnthropicOptions {
+ thinking_enabled: Some(true),
+ thinking_budget_tokens: Some(2048),
+ effort: Some(AnthropicEffort::Medium),
+ ..Default::default()
+ };
+ let params = build_params(&model, &context, false, Some(&options), None);
+ (params["thinking"].clone(), params["output_config"].clone())
+ }
+
+ /// Discussion #3710: Haiku 5.x rejects `thinking.type: "enabled"` with a 400 and needs
+ /// adaptive thinking with an effort, in both the dashed and dotted id spellings.
+ #[test]
+ fn haiku_5_requests_adaptive_thinking() {
+ for model_id in ["claude-haiku-5-5", "claude-haiku-5.5"] {
+ assert_eq!(
+ thinking_params(model_id),
+ (
+ json!({ "type": "adaptive", "display": "summarized" }),
+ json!({ "effort": "medium" }),
+ ),
+ "{model_id}"
+ );
+ }
+ }
+
+ /// Haiku 4.5 still takes budget-based thinking.
+ #[test]
+ fn haiku_4_5_keeps_budget_thinking() {
+ assert_eq!(
+ thinking_params("claude-haiku-4-5"),
+ (
+ json!({ "type": "enabled", "budget_tokens": 2048, "display": "summarized" }),
+ serde_json::Value::Null,
+ )
+ );
+ }
+}
diff --git a/crates/pa-ai/src/providers/bedrock/convert.rs b/crates/pa-ai/src/providers/bedrock/convert.rs
index d3679cd0b2..51d95d0a72 100644
--- a/crates/pa-ai/src/providers/bedrock/convert.rs
+++ b/crates/pa-ai/src/providers/bedrock/convert.rs
@@ -12,7 +12,7 @@ use crate::types::{
};
use crate::utils_inner::sanitize_unicode::sanitize_surrogates;
-fn get_model_match_candidates(model_id: &str, model_name: Option<&str>) -> Vec<String> {
+pub(crate) fn get_model_match_candidates(model_id: &str, model_name: Option<&str>) -> Vec<String> {
let mut values = vec![model_id.to_string()];
if let Some(name) = model_name {
values.push(name.to_string());
@@ -30,7 +30,7 @@ fn get_model_match_candidates(model_id: &str, model_name: Option<&str>) -> Vec<S
.collect()
}
-/// Adaptive thinking support (Opus 4.6+, Sonnet 4.6).
+/// Adaptive thinking support (Opus 4.6+, Sonnet 4.6+, Haiku 5+).
pub fn supports_adaptive_thinking(model_id: &str, model_name: Option<&str>) -> bool {
get_model_match_candidates(model_id, model_name)
.iter()
@@ -41,6 +41,7 @@ pub fn supports_adaptive_thinking(model_id: &str, model_name: Option<&str>) -> b
|| s.contains("opus-5")
|| s.contains("sonnet-4-6")
|| s.contains("sonnet-5")
+ || s.contains("haiku-5")
|| s.contains("fable-5")
|| s.contains("mythos-5")
|| s.contains("mythos-preview")
@@ -403,6 +404,24 @@ pub fn map_stop_reason(reason: Option<&str>) -> crate::types::StopReason {
}
}
+#[cfg(test)]
+mod supports_adaptive_thinking_tests {
+ use super::supports_adaptive_thinking;
+
+ /// Discussion #3710: Haiku 5.x rejects budget-based thinking, so it must take the adaptive path.
+ #[test]
+ fn bedrock_haiku_5_uses_adaptive_thinking() {
+ assert!(supports_adaptive_thinking(
+ "us.anthropic.claude-haiku-5-5-v1",
+ Some("Claude Haiku 5.5")
+ ));
+ assert!(!supports_adaptive_thinking(
+ "us.anthropic.claude-haiku-4-5-v1",
+ Some("Claude Haiku 4.5")
+ ));
+ }
+}
+
#[cfg(test)]
mod supports_always_on_adaptive_thinking_tests {
use super::supports_always_on_adaptive_thinking;
diff --git a/crates/pa-ai/src/providers/bedrock/mod.rs b/crates/pa-ai/src/providers/bedrock/mod.rs
index afd028f0d0..6bb4eda778 100644
--- a/crates/pa-ai/src/providers/bedrock/mod.rs
+++ b/crates/pa-ai/src/providers/bedrock/mod.rs
@@ -22,8 +22,8 @@ use crate::event_stream::{
};
use crate::providers::bedrock::auth::{resolve_credentials, resolve_endpoint, sigv4_headers};
use crate::providers::bedrock::convert::{
- convert_messages, convert_tool_config, map_stop_reason, map_thinking_level_to_effort,
- supports_always_on_adaptive_thinking, BedrockToolChoice,
+ convert_messages, convert_tool_config, get_model_match_candidates, map_stop_reason,
+ map_thinking_level_to_effort, supports_always_on_adaptive_thinking, BedrockToolChoice,
};
pub(crate) use crate::providers::bedrock::convert::{
is_anthropic_claude_model, supports_adaptive_thinking,
@@ -326,6 +326,16 @@ fn build_additional_model_request_fields(model: &Model, options: &BedrockOptions
return None;
}
+ if reasoning == ModelThinkingLevel::Off {
+ // Haiku 5.x thinks by default when the request carries no `thinking` field, so "off" has to
+ // say so explicitly, as the Anthropic route does.
+ let thinks_by_default = is_anthropic_claude_model(model)
+ && get_model_match_candidates(&model.id, Some(&model.name))
+ .iter()
+ .any(|candidate| candidate.contains("haiku-5"));
+ return thinks_by_default.then(|| json!({ "thinking": { "type": "disabled" } }));
+ }
+
if is_anthropic_claude_model(model) {
// GovCloud Bedrock currently rejects the Claude thinking.display field. Omit it there until
// the GovCloud Converse schema catches up.
@@ -719,7 +729,7 @@ pub fn stream_simple_bedrock(
context,
Some(&BedrockOptions {
base,
- reasoning: None,
+ reasoning: Some(ModelThinkingLevel::Off),
..Default::default()
}),
);
@@ -999,6 +1009,59 @@ mod tests {
));
}
+ /// Haiku 5.x thinks by default, so thinking off must send `thinking: {type: "disabled"}` on
+ /// Bedrock, as the Anthropic route does. Haiku 4.5 does not think unasked and gets no field.
+ #[tokio::test]
+ async fn thinking_off_disables_haiku_5_default_thinking() {
+ for (id, name, expected) in [
+ (
+ "us.anthropic.claude-haiku-5-5-v1",
+ "Claude Haiku 5.5",
+ json!({ "thinking": { "type": "disabled" } }),
+ ),
+ (
+ "us.anthropic.claude-haiku-4-5-v1",
+ "Claude Haiku 4.5",
+ Value::Null,
+ ),
+ ] {
+ let model = Model {
+ name: name.into(),
+ // A closed local port: the payload hook below cancels the request before it sends.
+ base_url: "http://127.0.0.1:9".into(),
+ ..test_model(id)
+ };
+ let captured = std::sync::Arc::new(std::sync::Mutex::new(None));
+ let signal = tokio_util::sync::CancellationToken::new();
+ let options = SimpleStreamOptions {
+ base: StreamOptions {
+ signal: Some(signal.clone()),
+ on_payload: Some(std::sync::Arc::new({
+ let captured = std::sync::Arc::clone(&captured);
+ move |payload: Value, _model: &Model| {
+ *captured.lock().unwrap() = Some(payload);
+ signal.cancel();
+ None
+ }
+ })),
+ ..Default::default()
+ },
+ reasoning: Some(ModelThinkingLevel::Off),
+ thinking_budgets: None,
+ };
+ let context = Context {
+ system_prompt: None,
+ messages: Vec::new(),
+ tools: None,
+ };
+ stream_simple_bedrock(&model, &context, Some(&options))
+ .collect()
+ .await;
+ let payload = captured.lock().unwrap().take().expect("payload captured");
+ assert_eq!(payload["additionalModelRequestFields"], expected, "{id}");
+ }
+ }
+
fn test_model(id: &str) -> Model {
Model {
id: id.into(),