Skip to content

A platform for community discussion. Free, open, simple.

DEV: Make AI context compaction global (#41317)

Samf86bdd738a1a
diff --git a/db/structure.sql b/db/structure.sql
index b9cdea601d9..101d3de2f85 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -449,7 +449,6 @@ CREATE TABLE public.ai_agents (
temperature double precision,
top_p double precision,
user_id integer,
- max_context_posts integer,
vision_enabled boolean DEFAULT false NOT NULL,
vision_max_pixels integer DEFAULT 1048576 NOT NULL,
rag_chunk_tokens integer DEFAULT 374 NOT NULL,
@@ -469,8 +468,7 @@ CREATE TABLE public.ai_agents (
examples jsonb,
show_thinking boolean DEFAULT true NOT NULL,
max_turn_tokens integer,
- compression_threshold integer,
- execution_mode character varying DEFAULT 'default'::character varying NOT NULL,
+ compression_threshold integer DEFAULT 80 NOT NULL,
require_approval boolean DEFAULT false NOT NULL,
thinking_effort character varying
);
@@ -22593,6 +22591,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20260703163425'),
('20260702102111'),
('20260701073045'),
+('20260701013609'),
+('20260701013606'),
('20260630034050'),
('20260629233141'),
('20260629081606'),
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs
index 92f0b0bc264..d71cdb2f8a1 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs
@@ -69,6 +69,8 @@ export default class AgentEditor extends Component {
data.toolOptions = this.mapToolOptions(data.toolOptions, data.tools);
}
+ data.compression_threshold ??= 80;
+
return data;
}
}
@@ -95,19 +97,6 @@ export default class AgentEditor extends Component {
];
}
- get executionModes() {
- return [
- {
- id: "default",
- name: i18n("discourse_ai.ai_agent.execution_mode_options.default"),
- },
- {
- id: "agentic",
- name: i18n("discourse_ai.ai_agent.execution_mode_options.agentic"),
- },
- ];
- }
-
get thinkingEffortValues() {
return [
"default",
@@ -281,17 +270,6 @@ export default class AgentEditor extends Component {
}
}
- @action
- onExecutionModeChange(mode, { set }) {
- set("execution_mode", mode);
- if (mode === "default") {
- set("max_turn_tokens", null);
- set("compression_threshold", null);
- } else {
- set("compression_threshold", 80);
- }
- }
-
@action
availableForcedTools(tools) {
return this.allTools.filter((tool) => tools.includes(tool.id));
@@ -1118,59 +1096,27 @@ export default class AgentEditor extends Component {
{{/if}}
<form.Field
- @name="execution_mode"
- @title={{i18n "discourse_ai.ai_agent.execution_mode"}}
- @tooltip={{i18n "discourse_ai.ai_agent.execution_mode_help"}}
+ @name="max_turn_tokens"
+ @title={{i18n "discourse_ai.ai_agent.max_turn_tokens"}}
+ @tooltip={{i18n "discourse_ai.ai_agent.max_turn_tokens_help"}}
@format="large"
- @onSet={{this.onExecutionModeChange}}
- @type="select"
+ @type="input-number"
as |field|
>
- <field.Control @includeNone={{false}} as |select|>
- {{#each this.executionModes as |mode|}}
- <select.Option @value={{mode.id}}>{{mode.name}}</select.Option>
- {{/each}}
- </field.Control>
+ <field.Control @min={{1}} lang="en" />
</form.Field>
- {{#if (eq data.execution_mode "agentic")}}
- <form.Field
- @name="max_turn_tokens"
- @title={{i18n "discourse_ai.ai_agent.max_turn_tokens"}}
- @tooltip={{i18n "discourse_ai.ai_agent.max_turn_tokens_help"}}
- @format="large"
- @type="input-number"
- as |field|
- >
- <field.Control @min={{1}} lang="en" />
- </form.Field>
-
- <form.Field
- @name="compression_threshold"
- @title={{i18n "discourse_ai.ai_agent.compression_threshold"}}
- @tooltip={{i18n
- "discourse_ai.ai_agent.compression_threshold_help"
- }}
- @format="large"
- @type="input-number"
- as |field|
- >
- <field.Control @min={{20}} @max={{99}} lang="en" />
- </form.Field>
- {{/if}}
-
- {{#unless (eq data.execution_mode "agentic")}}
- <form.Field
- @name="max_context_posts"
- @title={{i18n "discourse_ai.ai_agent.max_context_posts"}}
- @tooltip={{i18n "discourse_ai.ai_agent.max_context_posts_help"}}
- @format="large"
- @type="input-number"
- as |field|
- >
- <field.Control lang="en" />
- </form.Field>
- {{/unless}}
+ <form.Field
+ @name="compression_threshold"
+ @title={{i18n "discourse_ai.ai_agent.compression_threshold"}}
+ @tooltip={{i18n "discourse_ai.ai_agent.compression_threshold_help"}}
+ @showOptional={{false}}
+ @format="large"
+ @type="input-number"
+ as |field|
+ >
+ <field.Control @min={{20}} @max={{99}} lang="en" />
+ </form.Field>
{{#if (gt data.tools.length 0)}}
<AiAgentToolOptions
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb
index b75174ba29d..b9ec8a601cd 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb
@@ -463,7 +463,6 @@ module DiscourseAi
:thinking_effort,
:default_llm_id,
:user_id,
- :max_context_posts,
:vision_enabled,
:vision_max_pixels,
:rag_chunk_tokens,
@@ -477,7 +476,6 @@ module DiscourseAi
:show_thinking,
:forced_tool_count,
:force_default_llm,
- :execution_mode,
:max_turn_tokens,
:compression_threshold,
:require_approval,
@@ -642,8 +640,6 @@ module DiscourseAi
},
user_id: {
},
- max_context_posts: {
- },
vision_enabled: {
},
vision_max_pixels: {
@@ -671,8 +667,6 @@ module DiscourseAi
},
force_default_llm: {
},
- execution_mode: {
- },
max_turn_tokens: {
},
compression_threshold: {
diff --git a/plugins/discourse-ai/app/models/ai_agent.rb b/plugins/discourse-ai/app/models/ai_agent.rb
index 41b808489d3..93c04eb5b7d 100644
--- a/plugins/discourse-ai/app/models/ai_agent.rb
+++ b/plugins/discourse-ai/app/models/ai_agent.rb
@@ -4,7 +4,13 @@ class AiAgent < ActiveRecord::Base
# TODO remove tool_details from ignored_columns 01-02-2027
# question_consolidator_llm_id is intentionally ignored after the RAG tool
# migration; the column can be dropped in a later schema cleanup.
- self.ignored_columns = %i[tool_details question_consolidator_llm_id]
+ # TODO remove max_context_posts and execution_mode from ignored_columns 01-01-2027
+ self.ignored_columns = %i[
+ tool_details
+ question_consolidator_llm_id
+ max_context_posts
+ execution_mode
+ ]
# Between the regular migration (which creates ai_agents as a VIEW over
# ai_personas) and the post-migration (which does the actual rename_table),
@@ -21,8 +27,6 @@ class AiAgent < ActiveRecord::Base
validate :system_agent_unchangeable, on: :update, if: :system
validate :chat_preconditions
validate :well_formated_examples
- validates :max_context_posts, numericality: { greater_than: 0 }, allow_nil: true
- validates :execution_mode, inclusion: { in: %w[default agentic] }
validates :max_turn_tokens,
numericality: {
greater_than: 0,
@@ -34,8 +38,7 @@ class AiAgent < ActiveRecord::Base
numericality: {
greater_than_or_equal_to: 20,
less_than_or_equal_to: 99,
- },
- if: -> { execution_mode == "agentic" }
+ }
# leaves some room for growth but sets a maximum to avoid memory issues
# we may want to revisit this in the future
validates :vision_max_pixels, numericality: { greater_than: 0, maximum: 4_000_000 }
@@ -66,6 +69,8 @@ class AiAgent < ActiveRecord::Base
has_many :upload_references, as: :target, dependent: :destroy
has_many :uploads, through: :upload_references
+ before_validation :set_default_compression_threshold
+
before_update :regenerate_rag_fragments
before_destroy :ensure_not_system
@@ -243,7 +248,6 @@ class AiAgent < ActiveRecord::Base
system
mentionable
default_llm_id
- max_context_posts
vision_enabled
vision_max_pixels
rag_conversation_chunks
@@ -258,7 +262,6 @@ class AiAgent < ActiveRecord::Base
show_thinking
thinking_effort
enabled
- execution_mode
max_turn_tokens
compression_threshold
require_approval
@@ -419,6 +422,10 @@ class AiAgent < ActiveRecord::Base
user
end
+ def set_default_compression_threshold
+ self.compression_threshold ||= 80
+ end
+
def regenerate_rag_fragments
if rag_chunk_tokens_changed? || rag_chunk_overlap_tokens_changed?
RagDocumentFragment.where(target: self).delete_all
@@ -508,14 +515,12 @@ end
# allow_personal_messages :boolean default(TRUE), not null
# allow_topic_mentions :boolean default(FALSE), not null
# allowed_group_ids :integer default([]), not null, is an Array
-# compression_threshold :integer
+# compression_threshold :integer default(80), not null
# description :string(2000) not null
# enabled :boolean default(TRUE), not null
# examples :jsonb
-# execution_mode :string default("default"), not null
# force_default_llm :boolean default(FALSE), not null
# forced_tool_count :integer default(-1), not null
-# max_context_posts :integer
# max_turn_tokens :integer
# name :string(100) not null
# priority :boolean default(FALSE), not null
diff --git a/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb b/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb
index 53ac6bca43f..4d78b770b0a 100644
--- a/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb
+++ b/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb
@@ -17,7 +17,6 @@ class LocalizedAiAgentSerializer < ApplicationSerializer
:top_p,
:default_llm_id,
:user_id,
- :max_context_posts,
:vision_enabled,
:vision_max_pixels,
:rag_chunk_tokens,
@@ -31,7 +30,6 @@ class LocalizedAiAgentSerializer < ApplicationSerializer
:allow_topic_mentions,
:allow_personal_messages,
:force_default_llm,
- :execution_mode,
:max_turn_tokens,
:compression_threshold,
:require_approval,
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js
index ec134c5381c..f2ba2701b16 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js
@@ -19,7 +19,6 @@ const CREATE_ATTRIBUTES = [
"default_llm_id",
"force_default_llm",
"user",
- "max_context_posts",
"vision_enabled",
"vision_max_pixels",
"rag_uploads",
@@ -35,7 +34,6 @@ const CREATE_ATTRIBUTES = [
"allow_chat_direct_messages",
"mcp_server_ids",
"mcp_server_tool_names",
- "execution_mode",
"max_turn_tokens",
"compression_threshold",
@@ -55,7 +53,6 @@ const SYSTEM_ATTRIBUTES = [
"default_llm_id",
"force_default_llm",
"user",
- "max_context_posts",
"vision_enabled",
"vision_max_pixels",
"rag_uploads",
@@ -71,7 +68,6 @@ const SYSTEM_ATTRIBUTES = [
"allow_chat_direct_messages",
"mcp_server_ids",
"mcp_server_tool_names",
- "execution_mode",
"max_turn_tokens",
"compression_threshold",
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 49fa8c148a9..833853fd168 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -589,16 +589,9 @@ en:
description: "Description"
no_llm_selected: "No language model selected"
use_parent_llm: "Use agent's language model"
- max_context_posts: "Max context posts"
- max_context_posts_help: "The maximum number of posts to use as context for the AI when responding to a user. (empty for default)"
- execution_mode: "Execution Mode"
- execution_mode_help: "Controls how the agent manages tool execution and conversation context. Default uses fixed turn/tool limits. Agentic uses a token budget with automatic context compression for longer, more capable tool-use sessions."
- execution_mode_options:
- default: "Default (Fixed Limits)"
- agentic: "Agentic (Token Budget)"
- max_turn_tokens: "Max Turn Tokens"
- max_turn_tokens_help: "Maximum total tokens (input + output) consumed across all LLM calls during a single bot reply. When set, replaces the fixed completion/tool limits with a token budget, enabling more agentic behavior. Leave empty to default to half of the model's context window."
- compression_threshold: "Compression Threshold"
+ max_turn_tokens: "Max turn tokens"
+ max_turn_tokens_help: "Maximum total tokens (input + output) consumed across all LLM calls during a single bot reply. Leave empty to default to half of the model's context window."
+ compression_threshold: "Compression threshold"
compression_threshold_help: "Percentage of context usage (20–99) at which automatic compression kicks in. Lower values trigger compression earlier, preserving more headroom."
vision_enabled: Include image uploads
vision_enabled_help: If enabled, this agent can include images from posts and chats when the selected model supports image input.
diff --git a/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb b/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb
index dc322a2c05e..314421c7033 100644
--- a/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb
+++ b/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb
@@ -98,14 +98,8 @@ DiscourseAi::Agents::Agent.system_agents.each do |agent_class, id|
forced_tool_names = instance.force_tool_use.map { |tool| tool.to_s.split("::").last }
agent.tools = tools.map { |name, value| [name, value, forced_tool_names.include?(name)] }
agent.forced_tool_count = instance.forced_tool_count
- agent.execution_mode = agent_class.execution_mode
agent.max_turn_tokens = agent_class.max_turn_tokens
- agent.compression_threshold =
- if agent.execution_mode == "agentic"
- agent_class.compression_threshold || 85
- else
- agent_class.compression_threshold
- end
+ agent.compression_threshold = agent_class.compression_threshold || 80
agent.response_format = instance.response_format
agent.examples = instance.examples
diff --git a/plugins/discourse-ai/db/migrate/20260701013606_mark_ai_agent_fixed_limit_columns_readonly.rb b/plugins/discourse-ai/db/migrate/20260701013606_mark_ai_agent_fixed_limit_columns_readonly.rb
new file mode 100644
index 00000000000..f9d5c91011a
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20260701013606_mark_ai_agent_fixed_limit_columns_readonly.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class MarkAiAgentFixedLimitColumnsReadonly < ActiveRecord::Migration[8.0]
+ COLUMNS = %i[max_context_posts execution_mode]
+
+ def up
+ execute "UPDATE ai_agents SET compression_threshold = 80 WHERE compression_threshold IS NULL"
+ change_column_default :ai_agents, :compression_threshold, 80
+ change_column_null :ai_agents, :execution_mode, true
+ change_column_default :ai_agents, :execution_mode, nil
+ COLUMNS.each { |column| Migration::ColumnDropper.mark_readonly(:ai_agents, column) }
+ end
+
+ def down
+ change_column_default :ai_agents, :compression_threshold, nil
+ COLUMNS.each { |column| Migration::ColumnDropper.drop_readonly(:ai_agents, column) }
+ execute "UPDATE ai_agents SET execution_mode = 'default' WHERE execution_mode IS NULL"
+ change_column_default :ai_agents, :execution_mode, "default"
+ change_column_null :ai_agents, :execution_mode, false
+ end
+end
diff --git a/plugins/discourse-ai/db/post_migrate/20260701013609_drop_ai_agent_fixed_limit_columns.rb b/plugins/discourse-ai/db/post_migrate/20260701013609_drop_ai_agent_fixed_limit_columns.rb
new file mode 100644
index 00000000000..5e3ca19bc88
--- /dev/null
+++ b/plugins/discourse-ai/db/post_migrate/20260701013609_drop_ai_agent_fixed_limit_columns.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class DropAiAgentFixedLimitColumns < ActiveRecord::Migration[8.0]
+ DROPPED_COLUMNS = { ai_agents: %i[max_context_posts execution_mode] }
+
+ def up
+ execute "UPDATE ai_agents SET compression_threshold = 80 WHERE compression_threshold IS NULL"
+ change_column_null :ai_agents, :compression_threshold, false
+ DROPPED_COLUMNS.each { |table, columns| Migration::ColumnDropper.execute_drop(table, columns) }
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/discourse-ai/lib/agents/agent.rb b/plugins/discourse-ai/lib/agents/agent.rb
index 595593fa324..7a89c52d965 100644
--- a/plugins/discourse-ai/lib/agents/agent.rb
+++ b/plugins/discourse-ai/lib/agents/agent.rb
@@ -20,10 +20,6 @@ module DiscourseAi
1_048_576
end
- def execution_mode
- "default"
- end
-
def max_turn_tokens
nil
end
diff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rb
index 724049df701..13c1119e11a 100644
--- a/plugins/discourse-ai/lib/agents/bot.rb
+++ b/plugins/discourse-ai/lib/agents/bot.rb
@@ -5,11 +5,14 @@ module DiscourseAi
class Bot
BOT_NOT_FOUND = Class.new(StandardError)
- # the future is agentic, allow for more turns
- MAX_COMPLETIONS = 12
-
- # limit is arbitrary, but 5 which was used in the past was too low
- MAX_TOOLS = 30
+ DEFAULT_MAX_TURN_TOKENS = 32_000
+ CONTEXT_TOKEN_BUDGET_RATIO = 0.5
+ COMPRESSED_CONTEXT_PREFIX =
+ DiscourseAi::Completions::PromptMessagesBuilder::COMPRESSED_CONTEXT_PREFIX
+ COMPRESSED_CONTEXT_SUFFIX =
+ DiscourseAi::Completions::PromptMessagesBuilder::COMPRESSED_CONTEXT_SUFFIX
+ COMPRESSED_CONTEXT_ACK =
+ DiscourseAi::Completions::PromptMessagesBuilder::COMPRESSED_CONTEXT_ACK
BUDGET_EXHAUSTED_HINT = <<~TEXT.strip
[Turn budget exhausted — you cannot call any more tools.]
@@ -22,16 +25,21 @@ module DiscourseAi
prompt.push(type: :user, content: BUDGET_EXHAUSTED_HINT)
end
- # When an agentic agent has no explicit max_turn_tokens, default the turn
- # budget to half of the LLM's context window (see the max_turn_tokens_help
- # string). Returns nil when the LLM exposes no usable context window, in
- # which case callers fall back to the fixed completion/tool limits.
+ # When an agent has no explicit max_turn_tokens, default the turn budget
+ # to half of the LLM's context window. Use a conservative fallback when
+ # the LLM exposes no usable context window.
def self.default_max_turn_tokens(llm)
max_prompt_tokens = llm&.max_prompt_tokens.to_i
- return if max_prompt_tokens <= 0
+ return DEFAULT_MAX_TURN_TOKENS if max_prompt_tokens <= 0
+
max_prompt_tokens / 2
end
+ def self.context_token_budget(llm, max_turn_tokens = nil)
+ turn_budget = max_turn_tokens.presence || default_max_turn_tokens(llm)
+ (turn_budget * CONTEXT_TOKEN_BUDGET_RATIO).to_i
+ end
+
def self.as(bot_user, agent: DiscourseAi::Agents::General.new, model: nil)
new(bot_user, agent, model)
end
@@ -98,30 +106,20 @@ module DiscourseAi
end
needs_newlines = false
- tools_ran = 0
-
- token_budget = agent.class.max_turn_tokens
- use_token_budget = agent.class.execution_mode == "agentic"
-
- if use_token_budget && token_budget.blank?
- # max_turn_tokens is optional; default the turn budget to half of the
- # LLM context window. If the LLM exposes no context window, fall back
- # to the fixed completion/tool limits rather than crashing on a nil
- # budget comparison.
- token_budget = self.class.default_max_turn_tokens(current_llm)
- use_token_budget = token_budget.present?
- end
- # In token budget mode, compression manages context size instead of
- # the dialect's destructive trim_messages. Disabling trim prevents
- # the two strategies from fighting each other.
- prompt.skip_trim = true if use_token_budget
+ supports_context_compression = current_llm.max_prompt_tokens.to_i > 0
+ token_budget =
+ agent.class.max_turn_tokens.presence || self.class.default_max_turn_tokens(current_llm)
- if use_token_budget
- execution_context ||= DiscourseAi::Completions::ExecutionContext.new
- execution_context.token_usage_tracker ||= DiscourseAi::Completions::TokenUsageTracker.new
- end
- llm_kwargs[:execution_context] = execution_context if execution_context
+ # Compression manages context size instead of the dialect's destructive
+ # trim_messages. Disabling trim prevents the two strategies from
+ # fighting each other. Keep dialect trimming enabled for models without
+ # a usable context window because compression cannot estimate when to run.
+ prompt.skip_trim = true if supports_context_compression
+
+ execution_context ||= DiscourseAi::Completions::ExecutionContext.new
+ execution_context.token_usage_tracker ||= DiscourseAi::Completions::TokenUsageTracker.new
+ llm_kwargs[:execution_context] = execution_context
enable_gemini_thought_summaries!(llm_kwargs, current_llm, context)
token_usage_tracker = execution_context&.token_usage_tracker
@@ -132,12 +130,7 @@ module DiscourseAi
break # already ran the final text-only generate
end
- should_stop =
- if use_token_budget
- token_usage_tracker.total >= token_budget
- else
- total_completions >= MAX_COMPLETIONS
- end
+ should_stop = token_usage_tracker.total >= token_budget
if should_stop
if prompt.messages.last&.dig(:type) == :tool
@@ -149,7 +142,15 @@ module DiscourseAi
end
end
- maybe_compress_context(prompt, current_llm, execution_context:) if use_token_budget
+ compression_result =
+ maybe_compress_context(prompt, current_llm, execution_context:, raw_context:)
+ if supports_context_compression
+ prompt.skip_trim = compression_result != :skipped
+ if compression_result == :compressed &&
+ prompt_exceeds_compression_threshold?(prompt, current_llm)
+ prompt.skip_trim = false
+ end
+ end
tool_found = false
force_tool_if_needed(prompt, context)
@@ -178,11 +179,6 @@ module DiscourseAi
context: context,
existing_tools: existing_tools,
)
-
- if !use_token_budget
- tool = nil if tools_ran >= MAX_TOOLS
- end
-
if tool.present?
existing_tools << tool
tool_call = partial
@@ -211,7 +207,6 @@ module DiscourseAi
current_thinking: current_thinking,
)
- tools_ran += 1
ongoing_chain &&= tool.chain_next_response?
tool_halted = true if !tool.chain_next_response?
@@ -262,16 +257,11 @@ module DiscourseAi
total_completions += 1
if !final_answer_requested
- if use_token_budget
- total_used = token_usage_tracker.total
- prompt.tool_choice = :none if total_used >= (token_budget * 0.85)
- else
- prompt.tool_choice = :none if total_completions == MAX_COMPLETIONS - 1 ||
- tools_ran >= MAX_TOOLS
- end
+ total_used = token_usage_tracker.total
+ prompt.tool_choice = :none if total_used >= (token_budget * 0.85)
- # safety valve even in token budget mode
- break if use_token_budget && total_completions >= 100
+ # Safety valve against pathological tool loops.
+ break if total_completions >= 100
end
end
@@ -522,65 +512,60 @@ module DiscourseAi
Do not discard information from the previous summary unless it has been superseded.
TEXT
- def maybe_compress_context(prompt, current_llm, execution_context: nil)
+ def maybe_compress_context(prompt, current_llm, execution_context: nil, raw_context: nil)
max_tokens = current_llm.max_prompt_tokens
- return if max_tokens.blank? || max_tokens <= 0
+ return :not_needed if max_tokens.blank? || max_tokens <= 0
tokenizer = current_llm.tokenizer
- threshold_pct = (agent.class.compression_threshold || 85) / 100.0
+ threshold_pct = (agent.class.compression_threshold || 80) / 100.0
threshold = (max_tokens * threshold_pct).to_i
estimated_tokens =
prompt.messages.sum do |msg|
tokenizer.size(DiscourseAi::Completions::Prompt.text_only(msg).to_s)
end
- return if estimated_tokens < threshold
+ return :not_needed if estimated_tokens < threshold
- # keep system message (index 0) and a tail of recent messages
+ # keep system message (index 0) and a tail of recent messages. Always
+ # preserve the latest message, even if it exceeds the nominal tail budget.
tail_budget = (max_tokens * (1.0 - threshold_pct)).to_i
tail_tokens = 0
- tail_start = prompt.messages.length
+ selected_tail_indexes = Set.new
+ i = prompt.messages.length - 1
- (prompt.messages.length - 1).downto(1) do |i|
+ while i >= 1
msg = prompt.messages[i]
- msg_tokens = tokenizer.size(DiscourseAi::Completions::Prompt.text_only(msg).to_s)
- if tail_tokens + msg_tokens > tail_budget
- # ensure tool_call/tool pairs stay together
- if msg[:type] == :tool_call && i + 1 < prompt.messages.length &&
- prompt.messages[i + 1][:type] == :tool
- tail_start = i
- tail_tokens += msg_tokens
- tail_tokens +=
- tokenizer.size(
- DiscourseAi::Completions::Prompt.text_only(prompt.messages[i + 1]).to_s,
- )
- end
- break
- end
- tail_tokens += msg_tokens
- tail_start = i
- # ensure tool_call/tool pairs stay together
+ # scanning tail-first means a tool result is always visited before
+ # its tool_call, so keeping the pair together only needs to look back
+ pair_start = i
if msg[:type] == :tool && i > 1 && prompt.messages[i - 1][:type] == :tool_call
- i_prev = i - 1
- prev_tokens =
+ pair_start = i - 1
+ end
+
+ indexes = (pair_start..i).to_a
+ pair_tokens =
+ indexes.sum do |index|
tokenizer.size(
- DiscourseAi::Completions::Prompt.text_only(prompt.messages[i_prev]).to_s,
+ DiscourseAi::Completions::Prompt.text_only(prompt.messages[index]).to_s,
)
- tail_tokens += prev_tokens
- tail_start = i_prev
- end
+ end
+
+ break if selected_tail_indexes.present? && tail_tokens + pair_tokens > tail_budget
+
+ selected_tail_indexes.merge(indexes)
+ tail_tokens += pair_tokens
+ i = pair_start - 1
end
+ tail_start = selected_tail_indexes.min || prompt.messages.length
+
middle_messages = prompt.messages[1...tail_start]
- return if middle_messages.length < 6
+ return :skipped if middle_messages.length < 6
# Build a compression prompt from the current messages plus an instruction.
# This reuses the LLM's KV cache since it shares the same prefix.
- has_prior_compression =
- prompt.messages.any? do |msg|
- msg[:type] == :user && msg[:content].to_s.include?("<compressed_context>")
- end
+ has_prior_compression = has_compressed_context_checkpoint?(prompt.messages)
instruction = COMPRESSION_INSTRUCTION
instruction = "#{instruction}\n#{COMPRESSION_MERGE_INSTRUCTION}" if has_prior_compression
@@ -607,22 +592,22 @@ module DiscourseAi
)
rescue => e
Rails.logger.warn("DiscourseAi: Context compression failed, skipping: #{e.message}")
- return
+ return :skipped
end
summary = summary.is_a?(Array) ? summary.select { |s| s.is_a?(String) }.join : summary
- return if summary.blank?
+ return :skipped if summary.blank?
summary_tokens = tokenizer.size(summary)
middle_tokens =
- middle_messages.sum do |msg|
- tokenizer.size(DiscourseAi::Completions::Prompt.text_only(msg).to_s)
+ middle_messages.sum do |message|
+ tokenizer.size(DiscourseAi::Completions::Prompt.text_only(message).to_s)
end
if summary_tokens >= middle_tokens
Rails.logger.warn(
"DiscourseAi: Compression produced larger output than input (#{summary_tokens} >= #{middle_tokens}), skipping",
)
- return
+ return :skipped
end
# replace middle messages with compressed summary
@@ -632,12 +617,68 @@ module DiscourseAi
new_messages = [system_message]
new_messages << {
type: :user,
- content: "<compressed_context>#{summary}</compressed_context>",
+ content: "#{COMPRESSED_CONTEXT_PREFIX}#{summary}#{COMPRESSED_CONTEXT_SUFFIX}",
}
- new_messages << { type: :model, content: "Understood, I have the context." }
+ new_messages << { type: :model, content: COMPRESSED_CONTEXT_ACK }
new_messages.concat(tail_messages)
prompt.messages.replace(new_messages)
+ raw_context&.replace(messages_to_raw_context(new_messages.drop(1)))
+ :compressed
+ end
+
+ def prompt_exceeds_compression_threshold?(prompt, current_llm)
+ max_tokens = current_llm.max_prompt_tokens.to_i
+ return false if max_tokens <= 0
+
+ threshold = (max_tokens * ((agent.class.compression_threshold || 80) / 100.0)).to_i
+ estimate_prompt_tokens(prompt, current_llm) >= threshold
+ end
+
+ def estimate_prompt_tokens(prompt, current_llm)
+ tokenizer = current_llm.tokenizer
+ prompt.messages.sum do |message|
+ tokenizer.size(DiscourseAi::Completions::Prompt.text_only(message).to_s)
+ end
+ end
+
+ def has_compressed_context_checkpoint?(messages)
+ DiscourseAi::Completions::PromptMessagesBuilder.compression_checkpoint_index(
+ messages,
+ ).present?
+ end
+
+ def messages_to_raw_context(messages)
+ messages
+ .map do |message|
+ case message[:type]
+ when :tool_call
+ [
+ message[:content],
+ message[:id],
+ "tool_call",
+ message[:name],
+ thinking_context(message),
+ message[:provider_data].presence,
+ ]
+ when :tool
+ [message[:content], message[:id], "tool", message[:name]]
+ when :user
+ [message[:content], message[:id], "user"]
+ when :model
+ [message[:content], nil, "model", nil, thinking_context(message)]
+ end
+ end
+ .compact
+ end
+
+ def thinking_context(message)
+ if message[:thinking] || message[:thinking_provider_info]
+ {
+ "message" => message[:thinking],
+ "provider_info" => message[:thinking_provider_info],
+ }.compact
+ end
end
def self.guess_model(bot_user)
diff --git a/plugins/discourse-ai/lib/agents/tool_runner/discourse.rb b/plugins/discourse-ai/lib/agents/tool_runner/discourse.rb
index a538f415d48..44e4b3745c5 100644
--- a/plugins/discourse-ai/lib/agents/tool_runner/discourse.rb
+++ b/plugins/discourse-ai/lib/agents/tool_runner/discourse.rb
@@ -394,7 +394,6 @@ module DiscourseAi
"top_p",
"vision_enabled",
"tools",
- "max_context_posts",
"allow_chat_channel_mentions",
"allow_chat_direct_messages",
"allow_topic_mentions",
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index 2077715b8f0..e74beb8e216 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -306,16 +306,12 @@ module DiscourseAi
context_post_ids = nil if !channel.direct_message_channel?
- max_chat_messages = 40
- if bot.agent.class.respond_to?(:max_context_posts)
- max_chat_messages = bot.agent.class.max_context_posts || 40
- end
-
if !channel.direct_message_channel?
# we are interacting via mentions ... strip mention
instruction_message = message.message.gsub(/@#{bot.bot_user.username}/i, "").strip
end
+ context_llm = bot.llm
context =
DiscourseAi::Agents::BotContext.new(
participants: participants,
@@ -330,7 +326,9 @@ module DiscourseAi
include_image_uploads: include_image_uploads?,
include_document_uploads: include_document_uploads?,
allowed_attachment_types: bot.model.allowed_attachment_types,
- max_messages: max_chat_messages,
+ max_messages: DiscourseAi::Completions::PromptMessagesBuilder::MAX_CONTEXT_MESSAGES,
+ context_token_budget: context_token_budget(context_llm),
+ tokenizer: context_llm.tokenizer,
bot_user_ids: available_bot_user_ids,
instruction_message: instruction_message,
),
@@ -370,6 +368,9 @@ module DiscourseAi
reply = streamer.reply
if new_prompts.length > 1 && reply
+ # Note: messages_from_chat does not read these back, so compressed
+ # context checkpoints only persist across turns for post-based
+ # replies; chat rebuilds context from the raw messages each turn.
ChatMessageCustomPrompt.create!(message_id: reply.id, custom_prompt: new_prompts)
end
@@ -485,12 +486,7 @@ module DiscourseAi
end
)
- # safeguard
- max_context_posts = 40
- if bot.agent.class.respond_to?(:max_context_posts)
- max_context_posts = bot.agent.class.max_context_posts || 40
- end
-
+ context_llm = bot.llm
context =
DiscourseAi::Agents::BotContext.new(
post: post,
@@ -502,7 +498,9 @@ module DiscourseAi
DiscourseAi::Completions::PromptMessagesBuilder.messages_from_post(
post,
style: context_style,
- max_posts: max_context_posts,
+ max_posts: DiscourseAi::Completions::PromptMessagesBuilder::MAX_CONTEXT_MESSAGES,
+ context_token_budget: context_token_budget(context_llm),
+ tokenizer: context_llm.tokenizer,
include_image_uploads: include_image_uploads?,
include_document_uploads: include_document_uploads?,
allowed_attachment_types: bot.model.allowed_attachment_types,
@@ -741,6 +739,10 @@ module DiscourseAi
end
end
+ def context_token_budget(llm)
+ DiscourseAi::Agents::Bot.context_token_budget(llm, bot.agent.class.max_turn_tokens)
+ end
+
def available_bot_usernames
@bot_usernames ||=
AiAgent.joins(:user).pluck(:username).concat(available_bot_users.map(&:username))
diff --git a/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb b/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb
index 8d325bbaeeb..7426969bc73 100644
--- a/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb
+++ b/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb
@@ -103,7 +103,7 @@ module DiscourseAi
@reply_user = @bot.bot_user
@llm_model_id = @bot.model.id
- max_context_posts = @bot.agent.class.max_context_posts || 40
+ context_llm = @bot.llm
context =
DiscourseAi::Agents::BotContext.new(
post: @source_post,
@@ -112,7 +112,13 @@ module DiscourseAi
messages:
DiscourseAi::Completions::PromptMessagesBuilder.messages_from_post(
@source_post,
- max_posts: max_context_posts,
+ max_posts: DiscourseAi::Completions::PromptMessagesBuilder::MAX_CONTEXT_MESSAGES,
+ context_token_budget:
+ DiscourseAi::Agents::Bot.context_token_budget(
+ context_llm,
+ @bot.agent.class.max_turn_tokens,
+ ),
+ tokenizer: context_llm.tokenizer,
include_image_uploads: @bot.agent.class.vision_enabled,
include_document_uploads: @bot.model.allowed_attachment_types.present?,
allowed_attachment_types: @bot.model.allowed_attachment_types,
@@ -120,7 +126,7 @@ module DiscourseAi
),
)
- @prompt = @bot.agent.craft_prompt(context, llm: @bot.llm)
+ @prompt = @bot.agent.craft_prompt(context, llm: context_llm)
# This endpoint supports caller-owned tool execution. We replace agent tools so every
# emitted tool call can be completed through the resume protocol.
@prompt.tools =
@@ -175,35 +181,25 @@ module DiscourseAi
turn_reply = +""
streamed_tool_calls = []
- token_budget = resolve_token_budget
- use_token_budget = resolve_execution_mode == "agentic"
-
- if use_token_budget && token_budget.blank?
- # max_turn_tokens is optional; default the turn budget to half of the
- # LLM context window. If the LLM exposes no context window, fall back
- # to the fixed completion/tool limits rather than crashing on a nil
- # budget comparison.
- token_budget = DiscourseAi::Agents::Bot.default_max_turn_tokens(llm)
- use_token_budget = token_budget.present?
- end
-
- execution_context = nil
- token_usage_tracker = nil
- if use_token_budget
- token_usage_tracker =
- DiscourseAi::Completions::TokenUsageTracker.new(
- base_request: @accumulated_request_tokens,
- base_response: @accumulated_response_tokens,
- )
- execution_context =
- DiscourseAi::Completions::ExecutionContext.new(token_usage_tracker: token_usage_tracker)
- end
- generate_options = { user: @user, temperature: @temperature, top_p: @top_p }
- generate_options[:execution_context] = execution_context if execution_context
+ token_budget =
+ resolve_token_budget.presence || DiscourseAi::Agents::Bot.default_max_turn_tokens(llm)
+ token_usage_tracker =
+ DiscourseAi::Completions::TokenUsageTracker.new(
+ base_request: @accumulated_request_tokens,
+ base_response: @accumulated_response_tokens,
+ )
+ execution_context =
+ DiscourseAi::Completions::ExecutionContext.new(token_usage_tracker: token_usage_tracker)
+ generate_options = {
+ user: @user,
+ temperature: @temperature,
+ top_p: @top_p,
+ execution_context: execution_context,
+ }
# Pre-check: if budget already exhausted on resume, force a final
# text-only call or finalize immediately — don't overshoot.
- if use_token_budget && @accumulated_tokens >= token_budget
+ if @accumulated_tokens >= token_budget
if @prompt.messages.last&.dig(:type) == :tool
DiscourseAi::Agents::Bot.inject_budget_exhausted_hint(@prompt)
@prompt.tool_choice = :none
@@ -239,11 +235,9 @@ module DiscourseAi
normalized_result = normalize_result(result)
tool_calls = unique_tool_calls(streamed_tool_calls + extract_tool_calls(normalized_result))
- if use_token_budget
- @accumulated_request_tokens = token_usage_tracker.request
- @accumulated_response_tokens = token_usage_tracker.response
- @accumulated_tokens = token_usage_tracker.total
- end
+ @accumulated_request_tokens = token_usage_tracker.request
+ @accumulated_response_tokens = token_usage_tracker.response
+ @accumulated_tokens = token_usage_tracker.total
if tool_calls.present?
non_tool_result =
@@ -263,7 +257,7 @@ module DiscourseAi
end
if tool_calls.present?
- if use_token_budget && @accumulated_tokens >= token_budget
+ if @accumulated_tokens >= token_budget
# Budget exhausted — can't hand tools to client. Push synthetic
# "not executed" results and give the model one final text-only call.
tool_calls.each do |call|
@@ -521,10 +515,6 @@ module DiscourseAi
resolve_agent_record&.max_turn_tokens
end
- def resolve_execution_mode
- resolve_agent_record&.execution_mode || "default"
- end
-
def persist_reply_post!
llm_model = LlmModel.find(@llm_model_id)
reply_post =
diff --git a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
index faca8d25d2a..5ced9238051 100644
--- a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
+++ b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
@@ -5,6 +5,10 @@ module DiscourseAi
class PromptMessagesBuilder
MAX_CHAT_UPLOADS = 5
MAX_TOPIC_UPLOADS = 5
+ MAX_CONTEXT_MESSAGES = 1000
+ COMPRESSED_CONTEXT_PREFIX = "<compressed_context>"
+ COMPRESSED_CONTEXT_SUFFIX = "</compressed_context>"
+ COMPRESSED_CONTEXT_ACK = "Understood, I have the context."
IMAGE_UPLOAD_EXTENSIONS = %w[jpg jpeg png gif webp].freeze
attr_reader :chat_context_posts
attr_accessor :topic
@@ -14,6 +18,8 @@ module DiscourseAi
channel:,
context_post_ids:,
max_messages:,
+ context_token_budget: nil,
+ tokenizer: nil,
include_uploads: nil,
include_image_uploads: nil,
include_document_uploads: nil,
@@ -37,6 +43,7 @@ module DiscourseAi
elsif !channel.direct_message_channel? && !message.thread_id
messages =
Chat::Message
+ .includes(:user, :uploads, :thread)
.joins("left join chat_threads on chat_threads.id = chat_messages.thread_id")
.where(chat_channel_id: channel.id)
.where(
@@ -103,6 +110,8 @@ module DiscourseAi
end
end
+ builder.trim_to_token_budget!(context_token_budget, tokenizer:)
+
builder.to_a(
limit: max_messages,
style: channel.direct_message_channel? ? :chat_with_context : :chat,
@@ -113,6 +122,8 @@ module DiscourseAi
post,
style: nil,
max_posts:,
+ context_token_budget: nil,
+ tokenizer: nil,
bot_usernames:,
include_uploads: nil,
include_image_uploads: nil,
@@ -203,9 +214,43 @@ module DiscourseAi
end
end
+ builder.trim_to_token_budget!(context_token_budget, tokenizer:)
+
builder.to_a(style: style || (post.topic.private_message? ? :bot : :topic))
end
+ def self.message_text(value)
+ case value
+ when Hash
+ message_text(value[:content] || value["content"])
+ when Array
+ value.map { |item| message_text(item) }.join
+ else
+ value.to_s
+ end
+ end
+
+ # Finds the last compression checkpoint: a bot-authored :user message
+ # (blank id) wrapped in the compressed context markers, acknowledged by
+ # the :model message that follows it.
+ def self.compression_checkpoint_index(messages)
+ (messages.length - 2).downto(0) do |index|
+ message = messages[index]
+ next if message[:type] != :user
+ next if message[:id].present?
+ next if !message_text(message).start_with?(COMPRESSED_CONTEXT_PREFIX)
+
+ next_message = messages[index + 1]
+ if next_message[:type] != :model || message_text(next_message) != COMPRESSED_CONTEXT_ACK
+ next
+ end
+
+ return index
+ end
+
+ nil
+ end
+
def self.normalize_upload_inclusion(
include_uploads,
include_image_uploads,
@@ -367,7 +412,8 @@ module DiscourseAi
return topic_array if style == :topic
# the rest of the styles can include multiple messages
- result = valid_messages_array(@raw_messages)
+ raw_messages = messages_after_last_compression(@raw_messages).map(&:dup)
+ result = valid_messages_array(raw_messages)
prepend_chat_post_context(result) if style == :chat_with_context
if limit
@@ -377,6 +423,32 @@ module DiscourseAi
end
end
+ def trim_to_token_budget!(token_budget, tokenizer: nil)
+ token_budget = token_budget.to_i
+ return if token_budget <= 0 || @raw_messages.length <= 1
+
+ checkpoint_index = compression_checkpoint_index(@raw_messages)
+ scoped_messages = checkpoint_index ? @raw_messages[checkpoint_index..] : @raw_messages
+ checkpoint_messages = checkpoint_index ? scoped_messages.first(2) : []
+ candidates = checkpoint_index ? scoped_messages.drop(2) : scoped_messages
+
+ kept = []
+ used_tokens =
+ checkpoint_messages.sum { |message| estimate_message_tokens(message, tokenizer) }
+
+ candidates.reverse_each do |message|
+ message_tokens = estimate_message_tokens(message, tokenizer)
+ # stop at the first message that does not fit so kept history stays
+ # contiguous; always keep the latest message even when over budget
+ break if kept.present? && used_tokens + message_tokens > token_budget
+
+ kept << message
+ used_tokens += message_tokens
+ end
+
+ @raw_messages = checkpoint_messages + kept.reverse
+ end
+
def push(
type:,
content:,
@@ -395,6 +467,7 @@ module DiscourseAi
raise ArgumentError, "provider_data must be a hash"
end
+ content = normalize_content_uploads(content) if content.is_a?(Array)
content = [content, *upload_ids.map { |upload_id| { upload_id: upload_id } }] if upload_ids
message = { type: type, content: content }
message[:name] = name.to_s if name
@@ -440,6 +513,44 @@ module DiscourseAi
private
+ # Custom prompts round-trip through JSON (post_custom_prompt), which
+ # turns {upload_id: 1} into {"upload_id" => 1}. Prompt#validate_message
+ # only accepts the symbol-keyed form, so normalize on the way in.
+ def normalize_content_uploads(content)
+ content.map do |part|
+ if part.is_a?(Hash) && (upload_id = part[:upload_id] || part["upload_id"])
+ { upload_id: upload_id }
+ else
+ part
+ end
+ end
+ end
+
+ def estimate_message_tokens(message, tokenizer)
+ text = message_text(message)
+ return 0 if text.blank?
+
+ if tokenizer
+ tokenizer.size(text)
+ else
+ (text.bytesize / 3.0).ceil
+ end
+ end
+
+ def message_text(value)
+ self.class.message_text(value)
+ end
+
+ def compression_checkpoint_index(messages)
+ self.class.compression_checkpoint_index(messages)
+ end
+
+ def messages_after_last_compression(messages)
+ compression_index = compression_checkpoint_index(messages)
+
+ compression_index ? messages[compression_index..] : messages
+ end
+
def valid_messages_array(messages)
result = []
@@ -620,7 +731,7 @@ module DiscourseAi
end
def topic_array
- raw_messages = @raw_messages.dup
+ raw_messages = messages_after_last_compression(@raw_messages.dup)
content_array = []
content_array << "You are operating in a Discourse forum.\n\n"
content_array << format_topic_info(@topic) if @topic
@@ -659,12 +770,12 @@ module DiscourseAi
end
def chat_array(limit:)
- if @raw_messages.length > 1
- buffer = [
- +"You are replying inside a Discourse chat channel. Here is a summary of the conversation so far:\n{{{",
- ]
+ raw_messages = messages_after_last_compression(@raw_messages)
+ buffer = []
+ if raw_messages.length > 1
+ buffer << +"You are replying inside a Discourse chat channel. Here is a summary of the conversation so far:\n{{{"
- @raw_messages[0..-2].each do |message|
+ raw_messages[0..-2].each do |message|
buffer << "\n"
if message[:type] == :user
@@ -682,7 +793,7 @@ module DiscourseAi
buffer << "\n"
end
- last_message = @raw_messages[-1]
+ last_message = raw_messages[-1]
buffer << "#{last_message[:id] || "User"}: "
buffer << last_message[:content]
diff --git a/plugins/discourse-ai/lib/completions/token_usage_tracker.rb b/plugins/discourse-ai/lib/completions/token_usage_tracker.rb
index 87cd8b3dacb..947d6121f76 100644
--- a/plugins/discourse-ai/lib/completions/token_usage_tracker.rb
+++ b/plugins/discourse-ai/lib/completions/token_usage_tracker.rb
@@ -27,12 +27,25 @@ module DiscourseAi
# request_tokens = non-cached input (already excludes cached)
# cache_write_tokens = newly cached (full cost)
# cache_read_tokens = served from cache (1/10 cost)
- add_effective(
- request:
- log.request_tokens.to_i + log.cache_write_tokens.to_i +
- (log.cache_read_tokens.to_i * 0.1).to_i,
- response: log.response_tokens.to_i,
- )
+ request =
+ log.request_tokens.to_i + log.cache_write_tokens.to_i +
+ (log.cache_read_tokens.to_i * 0.1).to_i
+ response = log.response_tokens.to_i
+
+ request = estimate_tokens(raw_payload(log, :raw_request_payload)) if request <= 0
+ response = estimate_tokens(raw_payload(log, :raw_response_payload)) if response <= 0
+
+ add_effective(request: request, response: response)
+ end
+
+ def raw_payload(log, name)
+ log.public_send(name) if log.respond_to?(name)
+ end
+
+ def estimate_tokens(payload)
+ return 0 if payload.blank?
+
+ (payload.to_s.bytesize / 3.0).ceil
end
def add_effective(request:, response:)
diff --git a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb
index 8ba6b398e7d..a2ade9de196 100644
--- a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb
@@ -133,11 +133,10 @@ RSpec.describe DiscourseAi::Agents::Bot do
end
end
- context "with max_turn_tokens token budget" do
+ context "with token budget execution" do
fab!(:agent_record) do
Fabricate(
:ai_agent,
- execution_mode: "agentic",
max_turn_tokens: 5000,
compression_threshold: 80,
tools: [["ListCategories", nil, false]],
@@ -186,7 +185,6 @@ RSpec.describe DiscourseAi::Agents::Bot do
big_budget_agent =
Fabricate(
:ai_agent,
- execution_mode: "agentic",
max_turn_tokens: 10_000,
compression_threshold: 80,
tools: [["ListCategories", nil, false]],
@@ -226,22 +224,28 @@ RSpec.describe DiscourseAi::Agents::Bot do
expect(tool_choice_values[1]).to eq(:none)
end
- it "preserves legacy MAX_COMPLETIONS behavior when max_turn_tokens is nil" do
+ it "defaults the turn budget to half the context window without max_turn_tokens" do
no_budget_agent =
- Fabricate(:ai_agent, max_turn_tokens: nil, tools: [["ListCategories", nil, false]])
+ Fabricate(
+ :ai_agent,
+ max_turn_tokens: nil,
+ compression_threshold: 80,
+ tools: [["ListCategories", nil, false]],
+ )
klass = no_budget_agent.class_instance
- expect(klass.max_turn_tokens).to be_nil
+
+ # gpt_4 has max_prompt_tokens 131_072, so the default budget is 65_536.
+ expect(DiscourseAi::Agents::Bot.default_max_turn_tokens(bot.send(:llm))).to eq(65_536)
tool_call =
DiscourseAi::Completions::ToolCall.new(id: "call_1", name: "categories", parameters: {})
- # MAX_COMPLETIONS tool calls + 1 final text-only call (budget exhaustion path)
- responses = Array.new(described_class::MAX_COMPLETIONS) { tool_call } + ["Final"]
+ responses = [tool_call, "Final answer"]
call_count = 0
DiscourseAi::Completions::Llm.with_prepared_responses(responses) do
- bot = described_class.as(bot_user, agent: klass.new)
+ agent_bot = described_class.as(bot_user, agent: klass.new)
context =
DiscourseAi::Agents::BotContext.new(messages: [{ type: :user, content: "test" }])
@@ -249,64 +253,139 @@ RSpec.describe DiscourseAi::Agents::Bot do
:generate,
).and_wrap_original do |original, *args, **kwargs, &blk|
call_count += 1
- original.call(*args, **kwargs, &blk)
+ result = original.call(*args, **kwargs, &blk)
+ # 70_000 tokens per call exceeds the 65_536 default budget after the
+ # first call, so the loop stops on the token budget.
+ if (tracker = kwargs[:execution_context]&.token_usage_tracker)
+ tracker.add_effective(request: 40_000, response: 30_000)
+ end
+ result
end
- bot.reply(context) { |_partial| }
+ agent_bot.reply(context) { |_partial| }
end
- # +1 for the final text-only call after budget/turn exhaustion
- expect(call_count).to eq(described_class::MAX_COMPLETIONS + 1)
+ expect(call_count).to eq(2)
end
- it "defaults the turn budget to half the context window when agentic without max_turn_tokens" do
- # The editor lets an agentic agent be saved without a max_turn_tokens
- # budget ("Leave empty for default limits"). This previously crashed
- # with "comparison of Integer with nil failed". Instead the agent stays
- # agentic and the budget defaults to half the LLM context window.
- agentic_no_budget_agent =
+ it "uses conservative default budget and keeps trimming for models without a context window" do
+ no_budget_agent =
Fabricate(
:ai_agent,
- execution_mode: "agentic",
max_turn_tokens: nil,
compression_threshold: 80,
tools: [["ListCategories", nil, false]],
)
- klass = agentic_no_budget_agent.class_instance
+ captured_skip_trim = nil
- # gpt_4 has max_prompt_tokens 131_072, so the default budget is 65_536.
- expect(DiscourseAi::Agents::Bot.default_max_turn_tokens(bot.send(:llm))).to eq(65_536)
+ DiscourseAi::Completions::Llm.with_prepared_responses(["Final answer"]) do
+ agent_bot = described_class.as(bot_user, agent: no_budget_agent.class_instance.new)
+ context =
+ DiscourseAi::Agents::BotContext.new(messages: [{ type: :user, content: "test" }])
- tool_call =
- DiscourseAi::Completions::ToolCall.new(id: "call_1", name: "categories", parameters: {})
+ allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(
+ :max_prompt_tokens,
+ ).and_return(0)
+ allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(
+ :generate,
+ ).and_wrap_original do |original, *args, **kwargs, &blk|
+ captured_skip_trim = args.first.skip_trim
+ original.call(*args, **kwargs, &blk)
+ end
- responses = [tool_call, "Final answer"]
- call_count = 0
+ agent_bot.reply(context) { |_partial| }
+ end
- DiscourseAi::Completions::Llm.with_prepared_responses(responses) do
- agent_bot = described_class.as(bot_user, agent: klass.new)
- context =
- DiscourseAi::Agents::BotContext.new(messages: [{ type: :user, content: "test" }])
+ expect(described_class.default_max_turn_tokens(nil)).to eq(
+ described_class::DEFAULT_MAX_TURN_TOKENS,
+ )
+ expect(captured_skip_trim).to be_falsey
+ end
+
+ it "uses explicit max_turn_tokens to size the initial context budget" do
+ llm = bot.send(:llm)
+
+ expect(described_class.context_token_budget(llm, 5000)).to eq(2500)
+ end
+
+ it "defaults context budget to half of the default turn budget" do
+ llm = bot.send(:llm)
+
+ expect(described_class.context_token_budget(llm)).to eq(32_768)
+ end
+
+ it "re-enables dialect trimming when compression fails" do
+ large_messages = [{ type: :user, content: "Start" }]
+ 10.times do |index|
+ large_messages << { type: :model, content: "Response #{index} " * 200 }
+ large_messages << { type: :user, content: "Message #{index} " * 200 }
+ end
+
+ main_generate_skip_trim_values = []
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["Done"]) do
+ agent_bot = described_class.as(bot_user, agent: agent_class.new)
+ context = DiscourseAi::Agents::BotContext.new(messages: large_messages)
+ allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(
+ :max_prompt_tokens,
+ ).and_return(2000)
+ allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(:tokenizer).and_return(
+ DiscourseAi::Tokenizer::OpenAiTokenizer,
+ )
allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(
:generate,
).and_wrap_original do |original, *args, **kwargs, &blk|
- call_count += 1
- result = original.call(*args, **kwargs, &blk)
- # 70_000 tokens per call exceeds the 65_536 default budget after the
- # first call, so the loop stops on the token budget (not the legacy
- # MAX_COMPLETIONS limit).
- if (tracker = kwargs[:execution_context]&.token_usage_tracker)
- tracker.add_effective(request: 40_000, response: 30_000)
+ if kwargs[:feature_name] == "context_compression"
+ raise RuntimeError, "compression failed"
end
- result
+
+ main_generate_skip_trim_values << args.first.skip_trim
+ original.call(*args, **kwargs, &blk)
end
agent_bot.reply(context) { |_partial| }
end
- expect(call_count).to eq(2)
+ expect(main_generate_skip_trim_values).to eq([false])
+ end
+
+ it "re-enables dialect trimming when compression still leaves the prompt over threshold" do
+ large_messages = [{ type: :user, content: "Start" }]
+ 10.times do |index|
+ large_messages << { type: :model, content: "Response #{index} " * 200 }
+ large_messages << { type: :user, content: "Message #{index} " * 200 }
+ end
+ large_messages << { type: :model, content: "Latest oversized response " * 1000 }
+
+ main_generate_skip_trim_values = []
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["Done"]) do
+ agent_bot = described_class.as(bot_user, agent: agent_class.new)
+ context = DiscourseAi::Agents::BotContext.new(messages: large_messages)
+
+ allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(
+ :max_prompt_tokens,
+ ).and_return(2000)
+ allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(:tokenizer).and_return(
+ DiscourseAi::Tokenizer::OpenAiTokenizer,
+ )
+ allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(
+ :generate,
+ ).and_wrap_original do |original, *args, **kwargs, &blk|
+ if kwargs[:feature_name] == "context_compression"
+ "Summary of the conversation."
+ else
+ main_generate_skip_trim_values << args.first.skip_trim
+ original.call(*args, **kwargs, &blk)
+ end
+ end
+
+ agent_bot.reply(context) { |_partial| }
+ end
+
+ expect(main_generate_skip_trim_values).to eq([false])
end
it "forces a final text-only call with budget hint when budget exhausted after tool execution" do
@@ -315,7 +394,6 @@ RSpec.describe DiscourseAi::Agents::Bot do
small_budget_agent =
Fabricate(
:ai_agent,
- execution_mode: "agentic",
max_turn_tokens: 2000,
compression_threshold: 80,
tools: [["ListCategories", nil, false]],
@@ -362,42 +440,6 @@ RSpec.describe DiscourseAi::Agents::Bot do
expect(prompt_messages[1]).to include(:user)
end
- it "forces a final text-only call in legacy MAX_COMPLETIONS mode too" do
- one_turn_agent =
- Fabricate(:ai_agent, max_turn_tokens: nil, tools: [["ListCategories", nil, false]])
-
- klass = one_turn_agent.class_instance
-
- tool_call =
- DiscourseAi::Completions::ToolCall.new(id: "call_1", name: "categories", parameters: {})
-
- # MAX_COMPLETIONS tool calls, then one final text response
- responses = Array.new(described_class::MAX_COMPLETIONS) { tool_call } + ["Final summary"]
- call_count = 0
- last_tool_choice = nil
-
- DiscourseAi::Completions::Llm.with_prepared_responses(responses) do
- bot = described_class.as(bot_user, agent: klass.new)
- context =
- DiscourseAi::Agents::BotContext.new(messages: [{ type: :user, content: "test" }])
-
- allow_any_instance_of(DiscourseAi::Completions::Llm).to receive(
- :generate,
- ).and_wrap_original do |original, *args, **kwargs, &blk|
- call_count += 1
- prompt_arg = args.first
- last_tool_choice = prompt_arg.tool_choice
- original.call(*args, **kwargs, &blk)
- end
-
- bot.reply(context) { |_partial| }
- end
-
- # MAX_COMPLETIONS tool calls + 1 final text-only call
- expect(call_count).to eq(described_class::MAX_COMPLETIONS + 1)
- expect(last_tool_choice).to eq(:none)
- end
-
it "keeps the caller execution context intact on error" do
responses = [RuntimeError.new("boom")]
tracker = DiscourseAi::Completions::TokenUsageTracker.new
@@ -422,7 +464,6 @@ RSpec.describe DiscourseAi::Agents::Bot do
fab!(:agent_record) do
Fabricate(
:ai_agent,
- execution_mode: "agentic",
max_turn_tokens: 500_000,
compression_threshold: 75,
tools: [["ListCategories", nil, false]],
@@ -461,6 +502,37 @@ RSpec.describe DiscourseAi::Agents::Bot do
expect(prompt.messages.length).to be < 41
end
+ it "compacts raw context so compressed checkpoints persist to later turns" do
+ bot = described_class.as(bot_user, agent: agent_class.new)
+
+ messages = [{ type: :system, content: "You are a bot" }]
+ raw_context = []
+ 20.times do |index|
+ user_message = { type: :user, content: "Message #{index} " * 200, id: user.username }
+ model_message = { type: :model, content: "Response #{index} " * 200 }
+ messages << user_message
+ messages << model_message
+ raw_context << [user_message[:content], user_message[:id], "user"]
+ raw_context << [model_message[:content], nil, "model"]
+ end
+
+ prompt = DiscourseAi::Completions::Prompt.new(messages: messages, tools: [])
+ llm = bot.send(:llm)
+ allow(llm).to receive(:max_prompt_tokens).and_return(2000)
+ allow(llm).to receive(:tokenizer).and_return(DiscourseAi::Tokenizer::OpenAiTokenizer)
+ allow(llm).to receive(:generate).and_return("Summary of the conversation.")
+
+ bot.send(:maybe_compress_context, prompt, llm, raw_context: raw_context)
+
+ expect(raw_context.first).to eq(
+ ["<compressed_context>Summary of the conversation.</compressed_context>", nil, "user"],
+ )
+ expect(raw_context.second).to eq(
+ ["Understood, I have the context.", nil, "model", nil, nil],
+ )
+ expect(raw_context.flatten.join).not_to include("Message 0")
+ end
+
it "skips compression when under threshold" do
bot = described_class.as(bot_user, agent: agent_class.new)
@@ -481,6 +553,28 @@ RSpec.describe DiscourseAi::Agents::Bot do
expect(prompt.messages.length).to eq(3)
end
+ it "keeps the latest message even when it exceeds the tail budget" do
+ bot = described_class.as(bot_user, agent: agent_class.new)
+
+ messages = [{ type: :system, content: "You are a bot" }]
+ 10.times do |index|
+ messages << { type: :user, content: "Message #{index} " * 200 }
+ messages << { type: :model, content: "Response #{index} " * 200 }
+ end
+ messages << { type: :user, content: "Latest request " * 1000 }
+
+ prompt = DiscourseAi::Completions::Prompt.new(messages: messages, tools: [])
+ llm = bot.send(:llm)
+ allow(llm).to receive(:max_prompt_tokens).and_return(2000)
+ allow(llm).to receive(:tokenizer).and_return(DiscourseAi::Tokenizer::OpenAiTokenizer)
+ allow(llm).to receive(:generate).and_return("Summary of the conversation.")
+
+ bot.send(:maybe_compress_context, prompt, llm)
+
+ expect(prompt.messages.last[:content]).to include("Latest request")
+ expect(prompt.messages[1][:content]).to include("<compressed_context>")
+ end
+
it "uses agent compression_threshold to control when compression triggers" do
agent_record.update!(compression_threshold: 50)
agent_class_with_threshold = agent_record.class_instance
@@ -517,7 +611,7 @@ RSpec.describe DiscourseAi::Agents::Bot do
messages << { type: :user, content: "Question #{i} " * 200 }
messages << { type: :model, content: "Answer #{i} " * 200 }
end
- # add a tool_call/tool pair near the end
+ # add a tool_call/tool pair at the end so the pair must be retained as the tail
messages << {
type: :tool_call,
id: "call_1",
@@ -525,8 +619,6 @@ RSpec.describe DiscourseAi::Agents::Bot do
name: "categories",
}
messages << { type: :tool, id: "call_1", content: "tool result", name: "categories" }
- messages << { type: :user, content: "Final question " * 200 }
- messages << { type: :model, content: "Final answer " * 200 }
prompt = DiscourseAi::Completions::Prompt.new(messages: messages, tools: [])
@@ -545,7 +637,8 @@ RSpec.describe DiscourseAi::Agents::Bot do
tool_call_idx = types.index(:tool_call)
tool_idx = types.index(:tool)
- expect(tool_idx).to eq(tool_call_idx + 1) if tool_call_idx
+ expect(tool_call_idx).to be_present
+ expect(tool_idx).to eq(tool_call_idx + 1)
# verify compression happened
expect(prompt.messages[1][:content]).to include("<compressed_context>")
diff --git a/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb b/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
index 286d93ada32..abe5b5df974 100644
--- a/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
@@ -673,6 +673,31 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
)
end
+ it "restores upload references persisted in custom prompts" do
+ # compression tails can persist user messages with upload content into
+ # post_custom_prompt; the JSON round trip stringifies {upload_id: N} keys
+ PostCustomPrompt.create!(
+ post_id: second_post.id,
+ custom_prompt: [
+ [["look at this image", { upload_id: image_upload1.id }], user.username, "user"],
+ ],
+ )
+
+ context =
+ described_class.messages_from_post(
+ third_post,
+ max_posts: 10,
+ bot_usernames: [bot_user.username],
+ include_uploads: true,
+ )
+
+ flattened_content = context.flat_map { |message| Array(message[:content]) }
+ expect(flattened_content).to include({ upload_id: image_upload1.id })
+ expect {
+ DiscourseAi::Completions::Prompt.new("system", messages: context)
+ }.not_to raise_error
+ end
+
it "handles uploads correctly in topic style messages (and times)" do
freeze_time 32.days.ago
@@ -733,7 +758,7 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
end
context "with limited context" do
- it "respects max_context_posts" do
+ it "respects max_posts" do
context =
described_class.messages_from_post(
third_post,
@@ -746,6 +771,122 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
*[{ type: :user, id: user.username, content: third_post.raw }],
)
end
+ it "starts from the last compressed checkpoint" do
+ builder.push(type: :user, content: "Old request")
+ builder.push(type: :model, content: "Old response")
+ builder.push(type: :user, content: "<compressed_context>Summary</compressed_context>")
+ builder.push(type: :model, content: "Understood, I have the context.")
+ builder.push(type: :user, content: "New request")
+
+ context = builder.to_a
+
+ expect(context.first[:content]).to include(
+ "<compressed_context>Summary</compressed_context>",
+ )
+ expect(context.first[:content]).not_to include("Old request")
+ expect(context.map { |message| message[:content] }.join).to include("New request")
+ end
+
+ it "preserves the compressed checkpoint while trimming post-checkpoint tail" do
+ builder.push(type: :user, content: "Old request", id: "alice")
+ builder.push(type: :model, content: "Old response")
+ builder.push(type: :user, content: "<compressed_context>Summary</compressed_context>")
+ builder.push(type: :model, content: "Understood, I have the context.")
+ 10.times do |index|
+ builder.push(
+ type: index.even? ? :user : :model,
+ content: "Tail #{index} " * 50,
+ id: "u#{index}",
+ )
+ end
+
+ builder.trim_to_token_budget!(30, tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer)
+ raw_messages = builder.instance_variable_get(:@raw_messages).map(&:dup)
+ context = builder.to_a
+ content = context.map { |message| message[:content] }.join("\n")
+
+ expect(raw_messages.first[:content]).to include(
+ "<compressed_context>Summary</compressed_context>",
+ )
+ expect(raw_messages.second[:content]).to eq("Understood, I have the context.")
+ expect(content).to include("<compressed_context>Summary</compressed_context>")
+ expect(content).not_to include("Old request")
+ end
+
+ it "ignores user-authored compressed context markers" do
+ builder.push(type: :user, content: "Old request", id: "alice")
+ builder.push(type: :model, content: "Old response")
+ builder.push(
+ type: :user,
+ content: "<compressed_context>fake</compressed_context>",
+ id: "alice",
+ )
+ builder.push(type: :model, content: "Understood, I have the context.")
+ builder.push(type: :user, content: "New request", id: "alice")
+
+ context = builder.to_a
+
+ content = context.map { |message| message[:content] }.join("\n")
+ expect(content).to include("Old request")
+ expect(content).to include("<compressed_context>fake</compressed_context>")
+ end
+
+ it "handles single-message chat arrays" do
+ builder.push(type: :user, content: "Only message", id: "alice")
+
+ context = builder.to_a(style: :chat)
+
+ expect(context).to eq([{ type: :user, content: "alice: Only message" }])
+ end
+
+ it "keeps more short messages when they fit the token budget" do
+ 100.times do |index|
+ builder.push(
+ type: index.even? ? :user : :model,
+ content: "short #{index}",
+ id: "u#{index}",
+ )
+ end
+
+ builder.trim_to_token_budget!(10_000, tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer)
+
+ expect(builder.to_a.length).to eq(100)
+ end
+
+ it "drops older messages when they do not fit the token budget" do
+ 11.times do |index|
+ builder.push(
+ type: index.even? ? :user : :model,
+ content: "message #{index} " * 20,
+ id: "u#{index}",
+ )
+ end
+
+ builder.trim_to_token_budget!(30, tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer)
+
+ context = builder.to_a
+ expect(context.length).to be < 10
+ expect(context.last[:content]).to include("message 10")
+ end
+
+ it "keeps trimmed history contiguous when a middle message is oversized" do
+ oldest_content = "old tiny message"
+ oversized_content = "oversized response " * 100
+ latest_content = "latest request"
+
+ builder.push(type: :user, content: oldest_content, id: "alice")
+ builder.push(type: :model, content: oversized_content)
+ builder.push(type: :user, content: latest_content, id: "alice")
+
+ builder.trim_to_token_budget!(20, tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer)
+
+ content = builder.to_a.map { |message| message[:content] }.join("\n")
+ expect(content).to include(latest_content)
+ # trimming must stop at the first message over budget instead of
+ # skipping it and resurrecting older messages that still fit
+ expect(content).not_to include(oldest_content)
+ expect(content).not_to include(oversized_content)
+ end
end
it "includes previous posts ordered by post_number" do
diff --git a/plugins/discourse-ai/spec/lib/completions/token_usage_tracker_spec.rb b/plugins/discourse-ai/spec/lib/completions/token_usage_tracker_spec.rb
index bbffeb11f29..1a5c7adabd4 100644
--- a/plugins/discourse-ai/spec/lib/completions/token_usage_tracker_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/token_usage_tracker_spec.rb
@@ -18,6 +18,44 @@ RSpec.describe DiscourseAi::Completions::TokenUsageTracker do
expect(tracker.total).to eq(1130)
end
+ it "estimates usage from payloads when audit logs do not report tokens" do
+ tracker = described_class.new
+ log =
+ Struct.new(
+ :request_tokens,
+ :cache_write_tokens,
+ :cache_read_tokens,
+ :response_tokens,
+ :raw_request_payload,
+ :raw_response_payload,
+ ).new(0, 0, 0, 0, "a" * 10, "b" * 7)
+
+ tracker.add_from_audit_log(log)
+
+ expect(tracker.request).to eq(4)
+ expect(tracker.response).to eq(3)
+ expect(tracker.total).to eq(7)
+ end
+
+ it "uses reported usage before payload estimates" do
+ tracker = described_class.new
+ log =
+ Struct.new(
+ :request_tokens,
+ :cache_write_tokens,
+ :cache_read_tokens,
+ :response_tokens,
+ :raw_request_payload,
+ :raw_response_payload,
+ ).new(10, 0, 0, 2, "a" * 300, "b" * 300)
+
+ tracker.add_from_audit_log(log)
+
+ expect(tracker.request).to eq(10)
+ expect(tracker.response).to eq(2)
+ expect(tracker.total).to eq(12)
+ end
+
it "supports starting from a previous total budget" do
tracker = described_class.new(base_total: 101)
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
index 1525253267c..b96f9de9bda 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
@@ -765,7 +765,7 @@ RSpec.describe DiscourseAi::AiBot::Playground do
)
end
- agent.update!(max_context_posts: 4, enabled: true)
+ agent.update!(enabled: true)
prompts = nil
DiscourseAi::Completions::Llm.with_prepared_responses(
@@ -790,11 +790,15 @@ RSpec.describe DiscourseAi::AiBot::Playground do
.join("\n")
.strip
- # why?
- # 1. we set context to 4
- # 2. however PromptMessagesBuilder will enforce rules of starting with :user and ending with it
- # so one of the model messages is dropped
expected = <<~TEXT.strip
+ user: Hello
+ model: World
+ user: request 0
+ model: response 0
+ user: request 1
+ model: response 1
+ user: request 2
+ model: response 2
user: request 3
model: response 3
user: Hello
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb
index ca676010cc8..c1eac65f331 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb
@@ -10,7 +10,6 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
default_llm_id: llm.id,
allow_personal_messages: true,
- execution_mode: "agentic",
max_turn_tokens: 5000,
compression_threshold: 80,
)
@@ -141,10 +140,9 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
end
end
- it "defaults the budget to half the context window when agentic without max_turn_tokens" do
- # "Leave empty for default limits": an agentic agent with no
- # max_turn_tokens must not crash, and the budget defaults to half the LLM
- # context window (fake_llm has max_prompt_tokens 131_072 → 65_536).
+ it "defaults the budget to half the context window without max_turn_tokens" do
+ # An agent with no max_turn_tokens uses half the LLM context window
+ # (fake_llm has max_prompt_tokens 131_072 → 65_536).
ai_agent.update!(max_turn_tokens: nil)
tool_call =
diff --git a/plugins/discourse-ai/spec/models/ai_agent_spec.rb b/plugins/discourse-ai/spec/models/ai_agent_spec.rb
index f7b15fa86be..b6cf9be275a 100644
--- a/plugins/discourse-ai/spec/models/ai_agent_spec.rb
+++ b/plugins/discourse-ai/spec/models/ai_agent_spec.rb
@@ -79,20 +79,6 @@ RSpec.describe AiAgent do
expect(DiscourseAi::AiHelper::Assistant.prompt_cache[:value]).to eq("cached prompts")
end
- it "validates context settings" do
- expect(basic_agent.valid?).to eq(true)
-
- basic_agent.max_context_posts = 0
- expect(basic_agent.valid?).to eq(false)
- expect(basic_agent.errors[:max_context_posts]).to eq(["must be greater than 0"])
-
- basic_agent.max_context_posts = 1
- expect(basic_agent.valid?).to eq(true)
-
- basic_agent.max_context_posts = nil
- expect(basic_agent.valid?).to eq(true)
- end
-
it "validates tools" do
Fabricate(:ai_tool, id: 1)
Fabricate(:ai_tool, id: 2, name: "Archie search", tool_name: "search")
@@ -205,7 +191,6 @@ RSpec.describe AiAgent do
forum_helper.update!(
user_id: 1,
default_llm_id: llm_model.id,
- max_context_posts: 3,
allow_topic_mentions: true,
allow_personal_messages: true,
allow_chat_channel_mentions: true,
@@ -220,7 +205,6 @@ RSpec.describe AiAgent do
expect(klass.allowed_group_ids).to eq([10])
expect(klass.user_id).to eq(1)
expect(klass.default_llm_id).to eq(llm_model.id)
- expect(klass.max_context_posts).to eq(3)
expect(klass.allow_topic_mentions).to eq(true)
expect(klass.allow_personal_messages).to eq(true)
expect(klass.allow_chat_channel_mentions).to eq(true)
@@ -236,7 +220,6 @@ RSpec.describe AiAgent do
tools: [],
allowed_group_ids: [],
default_llm_id: llm_model.id,
- max_context_posts: 3,
allow_topic_mentions: true,
allow_personal_messages: true,
allow_chat_channel_mentions: true,
@@ -251,7 +234,6 @@ RSpec.describe AiAgent do
expect(klass.allowed_group_ids).to eq([])
expect(klass.user_id).to eq(1)
expect(klass.default_llm_id).to eq(llm_model.id)
- expect(klass.max_context_posts).to eq(3)
expect(klass.allow_topic_mentions).to eq(true)
expect(klass.allow_personal_messages).to eq(true)
expect(klass.allow_chat_channel_mentions).to eq(true)
@@ -499,48 +481,24 @@ RSpec.describe AiAgent do
end
end
- describe "agentic execution mode defaults and propagation" do
- it "assigns safe defaults to a agent created without agentic fields" do
+ describe "token budget settings" do
+ it "allows an agent to use the default token budget" do
agent =
AiAgent.create!(
- name: "legacy_agent",
- description: "no agentic fields set",
+ name: "token_budget_agent",
+ description: "test",
system_prompt: "test",
tools: [],
allowed_group_ids: [],
)
- expect(agent.execution_mode).to eq("default")
expect(agent.max_turn_tokens).to be_nil
- expect(agent.compression_threshold).to be_nil
+ expect(agent.compression_threshold).to eq(80)
klass = agent.class_instance
- expect(klass.execution_mode).to eq("default")
expect(klass.max_turn_tokens).to be_nil
- expect(klass.compression_threshold).to be_nil
- end
-
- it "requires compression_threshold for agentic mode but not for default mode" do
- agent =
- AiAgent.new(
- name: "agentic_agent",
- description: "test",
- system_prompt: "test",
- tools: [],
- allowed_group_ids: [],
- execution_mode: "agentic",
- )
-
- expect(agent.valid?).to eq(false)
- expect(agent.errors[:compression_threshold]).to be_present
-
- agent.compression_threshold = 75
- expect(agent.valid?).to eq(true)
-
- agent.execution_mode = "default"
- agent.compression_threshold = nil
- expect(agent.valid?).to eq(true)
+ expect(klass.compression_threshold).to eq(80)
end
end
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
index f2541b9f35c..f5dc16b3bc3 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
@@ -284,7 +284,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController do
mcp_server_tool_names: {
ai_mcp_server.id.to_s => ["search_issues"],
},
- execution_mode: "agentic",
max_turn_tokens: 5000,
compression_threshold: 80,
response_format: [{ key: "summary", type: "string" }],
@@ -309,7 +308,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController do
expect(agent_json["thinking_effort"]).to eq("high")
expect(agent_json["default_llm_id"]).to eq(llm_model.id)
expect(agent_json["forced_tool_count"]).to eq(2)
- expect(agent_json["execution_mode"]).to eq("agentic")
expect(agent_json["max_turn_tokens"]).to eq(5000)
expect(agent_json["allow_topic_mentions"]).to eq(true)
@@ -561,13 +559,12 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController do
expect(agent.rag_llm_model_id).to eq(llm_model.id)
end
- it "supports updating agentic params" do
+ it "supports updating token budget params" do
agent = Fabricate(:ai_agent, name: "test_bot2")
put "/admin/plugins/discourse-ai/ai-agents/#{agent.id}.json",
params: {
ai_agent: {
- execution_mode: "agentic",
max_turn_tokens: 8000,
compression_threshold: 75,
},
@@ -576,7 +573,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController do
expect(response).to have_http_status(:ok)
agent.reload
- expect(agent.execution_mode).to eq("agentic")
expect(agent.max_turn_tokens).to eq(8000)
expect(agent.compression_threshold).to eq(75)
diff --git a/plugins/discourse-ai/test/javascripts/unit/models/ai-agent-test.js b/plugins/discourse-ai/test/javascripts/unit/models/ai-agent-test.js
index 126bbee1eef..ab9e498e4d0 100644
--- a/plugins/discourse-ai/test/javascripts/unit/models/ai-agent-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/models/ai-agent-test.js
@@ -39,7 +39,6 @@ module("Unit | Model | ai-agent", function () {
force_default_llm: false,
user: null,
user_id: null,
- max_context_posts: 5,
vision_enabled: true,
vision_max_pixels: 100,
rag_uploads: [],
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_generator.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_generator.rb
index 2af58c6e08b..757e2c66772 100644
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_generator.rb
+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_generator.rb
@@ -15,10 +15,6 @@ module DiscourseDataExplorer
]
end
- def self.execution_mode
- "agentic"
- end
-
def self.max_turn_tokens
100_000
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
index e1e6a2a3dd8..362c0b406c5 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
@@ -9,10 +9,6 @@ module DiscourseWorkflows
DiscourseAi::Agents::Tools::Time
].freeze
- def self.execution_mode
- "agentic"
- end
-
def self.max_turn_tokens
100_000
end