A platform for community discussion. Free, open, simple.
FEATURE: support for Opus 4.5 (#36216)
Sam9461b199eb12
diff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rbindex 2f3d11bc8b8..d687919d18c 100644--- a/plugins/discourse-ai/app/models/llm_model.rb+++ b/plugins/discourse-ai/app/models/llm_model.rb@@ -47,6 +47,11 @@ class LlmModel < ActiveRecord::Basevalues: %w[never tool_results always],default: "never",},+ effort: {+ type: :enum,+ values: %w[default low medium high],+ default: "default",+ },},anthropic: {disable_native_tools: :checkbox,@@ -59,6 +64,11 @@ class LlmModel < ActiveRecord::Basevalues: %w[never tool_results always],default: "never",},+ effort: {+ type: :enum,+ values: %w[default low medium high],+ default: "default",+ },},open_ai: {organization: :text,diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 11dfd2e97b4..666e2b132de 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -687,7 +687,7 @@ en:model_description:none: "General settings that work for most language models"- anthropic-claude-opus-4-1: "Anthropic's most intelligent model"+ anthropic-claude-opus-4-5: "Anthropic's most intelligent model"anthropic-claude-sonnet-4-5: "Optimal balance of speed and cost"anthropic-claude-haiku-4-5: "Fast and cost-effective"google-gemini-3-pro: "Google's most intelligent model"@@ -781,6 +781,7 @@ en:prompt_caching_never: "Never - Do not use prompt caching"prompt_caching_tool_results: "Tool Results - Cache when tool results are present (recommended)"prompt_caching_always: "Always - Always use prompt caching"+ effort: "Effort level (Claude Opus 4.5 only)"related_topics:title: "Related topics"diff --git a/plugins/discourse-ai/lib/completions/endpoints/anthropic.rb b/plugins/discourse-ai/lib/completions/endpoints/anthropic.rbindex 5122a8585c4..d1cf2c6ebbd 100644--- a/plugins/discourse-ai/lib/completions/endpoints/anthropic.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/anthropic.rb@@ -62,6 +62,10 @@ module DiscourseAioptions[:stop_sequences] = ["</function_calls>"] if !dialect.native_tool_support? &&dialect.prompt.has_tools?+ # effort parameter for Claude Opus 4.5+ effort = llm_model.lookup_custom_param("effort")+ options[:output_config] = { effort: effort } if %w[low medium high].include?(effort)+optionsend@@ -150,12 +154,25 @@ module DiscourseAi"content-type" => "application/json",}- # Add caching headers if configured- headers.merge!(anthropic_cache_headers)+ headers.merge!(combined_anthropic_beta_headers)Net::HTTP::Post.new(model_uri, headers).tap { |r| r.body = payload }end+ def combined_anthropic_beta_headers+ beta_features = []++ caching_mode = llm_model.lookup_custom_param("prompt_caching") || "never"+ beta_features << "prompt-caching-2024-07-31" if caching_mode != "never"++ effort = llm_model.lookup_custom_param("effort")+ beta_features << "effort-2025-11-24" if %w[low medium high].include?(effort)++ return {} if beta_features.empty?++ { "anthropic-beta" => beta_features.join(",") }+ end+def decode_chunk(partial_data)@decoder ||= JsonStreamDecoder.new(@decoder << partial_data)diff --git a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock.rb b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock.rbindex b733fe40654..7183d362208 100644--- a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock.rb@@ -43,6 +43,19 @@ module DiscourseAiendresult[:max_tokens] = max_tokens+ # effort parameter for Claude Opus 4.5+ effort = llm_model.lookup_custom_param("effort")+ if %w[low medium high].include?(effort)+ result[:output_config] = { effort: effort }+ result[:anthropic_beta] = ["effort-2025-11-24"]+ end++ caching_mode = llm_model.lookup_custom_param("prompt_caching") || "never"+ if caching_mode != "never"+ result[:anthropic_beta] ||= []+ result[:anthropic_beta] << "prompt-caching-2024-07-31"+ end+resultelse{}diff --git a/plugins/discourse-ai/lib/completions/llm.rb b/plugins/discourse-ai/lib/completions/llm.rbindex f9dd52177dd..a5d51bae360 100644--- a/plugins/discourse-ai/lib/completions/llm.rb+++ b/plugins/discourse-ai/lib/completions/llm.rb@@ -25,6 +25,16 @@ module DiscourseAi{id: "anthropic",models: [+ {+ name: "claude-opus-4-5",+ tokens: 200_000,+ display_name: "Claude 4.5 Sonnet",+ input_cost: 5,+ cached_input_cost: 0.50,+ cache_write_cost: 10,+ output_cost: 15,+ vision_enabled: true,+ },{name: "claude-sonnet-4-5",tokens: 200_000,@@ -45,16 +55,6 @@ module DiscourseAicache_write_cost: 1.25,vision_enabled: true,},- {- name: "claude-opus-4-1",- tokens: 200_000,- display_name: "Claude 4.1 Opus",- input_cost: 15,- cached_input_cost: 1.50,- cache_write_cost: 18.75,- output_cost: 75,- vision_enabled: true,- },],tokenizer: DiscourseAi::Tokenizer::AnthropicTokenizer,endpoint: "https://api.anthropic.com/v1/messages",diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rbindex 3f7913ab583..cfffd1584a1 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb@@ -1190,4 +1190,142 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Anthropic doexpect(parsed_body).to eq(expected_body)endend++ describe "effort parameter" do+ it "includes effort in output_config and beta header when set to low, medium, or high" do+ model.update!(provider_params: { effort: "high" })++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ headers: {+ "Content-Type" => "application/json",+ "X-Api-Key" => "123",+ "Anthropic-Version" => "2023-06-01",+ "Anthropic-Beta" => "effort-2025-11-24",+ },+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "test response" }],+ model: "claude-3-opus-20240229",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user)+ expect(parsed_body.dig(:output_config, :effort)).to eq("high")+ end++ it "combines effort and caching beta headers when both are configured" do+ model.update!(provider_params: { effort: "medium", prompt_caching: "always" })++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ headers: {+ "Content-Type" => "application/json",+ "X-Api-Key" => "123",+ "Anthropic-Version" => "2023-06-01",+ "Anthropic-Beta" => "prompt-caching-2024-07-31,effort-2025-11-24",+ },+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "test response" }],+ model: "claude-3-opus-20240229",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user)+ expect(parsed_body.dig(:output_config, :effort)).to eq("medium")+ end++ it "omits effort and beta header when set to default" do+ model.update!(provider_params: { effort: "default" })++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ headers: {+ "Content-Type" => "application/json",+ "X-Api-Key" => "123",+ "Anthropic-Version" => "2023-06-01",+ },+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "test response" }],+ model: "claude-3-opus-20240229",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user)+ expect(parsed_body).not_to have_key(:output_config)+ end++ it "omits effort and beta header when not configured" do+ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ headers: {+ "Content-Type" => "application/json",+ "X-Api-Key" => "123",+ "Anthropic-Version" => "2023-06-01",+ },+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "test response" }],+ model: "claude-3-opus-20240229",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user)+ expect(parsed_body).not_to have_key(:output_config)+ end+ endenddiff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_spec.rbindex b7e581b807a..272e3d85db0 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_spec.rb@@ -1127,4 +1127,80 @@ RSpec.describe DiscourseAi::Completions::Endpoints::AwsBedrock doexpect(endpoint.respond_to?(:should_apply_prompt_caching?)).to be(true)endend++ describe "effort parameter" do+ it "includes effort in output_config and anthropic_beta when set to low, medium, or high" do+ model.update!(+ provider_params: {+ access_key_id: "123",+ region: "us-east-1",+ effort: "medium",+ },+ )++ proxy = DiscourseAi::Completions::Llm.proxy(model)+ request = nil++ content = {+ content: [text: "test response"],+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json++ stub_request(+ :post,+ "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-sonnet-20240229-v1:0/invoke",+ )+ .with do |inner_request|+ request = inner_request+ true+ end+ .to_return(status: 200, body: content)++ proxy.generate("test prompt", user: user)++ request_body = JSON.parse(request.body)+ expect(request_body.dig("output_config", "effort")).to eq("medium")+ expect(request_body["anthropic_beta"]).to eq(["effort-2025-11-24"])+ end++ it "omits effort and anthropic_beta when set to default" do+ model.update!(+ provider_params: {+ access_key_id: "123",+ region: "us-east-1",+ effort: "default",+ },+ )++ proxy = DiscourseAi::Completions::Llm.proxy(model)+ request = nil++ content = {+ content: [text: "test response"],+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json++ stub_request(+ :post,+ "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-sonnet-20240229-v1:0/invoke",+ )+ .with do |inner_request|+ request = inner_request+ true+ end+ .to_return(status: 200, body: content)++ proxy.generate("test prompt", user: user)++ request_body = JSON.parse(request.body)+ expect(request_body).not_to have_key("output_config")+ expect(request_body).not_to have_key("anthropic_beta")+ end+ endenddiff --git a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rbindex 314a44769dd..0bf3a0217bb 100644--- a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb+++ b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb@@ -15,7 +15,7 @@ RSpec.describe "Managing LLM configurations", type: :system doit "correctly sets defaults" dovisit "/admin/plugins/discourse-ai/ai-llms"- find("[data-llm-id='anthropic-claude-opus-4-1'] button").click()+ find("[data-llm-id='anthropic-claude-opus-4-5'] button").click()form.field("api_key").fill_in("abcd")form.field("enabled_chat_bot").toggleform.submit@@ -27,9 +27,9 @@ RSpec.describe "Managing LLM configurations", type: :system doexpect(llm.api_key).to eq("abcd")preset = DiscourseAi::Completions::Llm.presets.find { |p| p[:id] == "anthropic" }- model_preset = preset[:models].find { |m| m[:name] == "claude-opus-4-1" }+ model_preset = preset[:models].find { |m| m[:name] == "claude-opus-4-5" }- expect(llm.name).to eq("claude-opus-4-1")+ expect(llm.name).to eq("claude-opus-4-5")expect(llm.url).to eq(preset[:endpoint])expect(llm.tokenizer).to eq(preset[:tokenizer].to_s)expect(llm.max_prompt_tokens.to_i).to eq(model_preset[:tokens])