From 8897dab282f58e38b103dcff7ba4eaad896b6995 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Fri, 5 Dec 2025 19:31:36 +0100 Subject: [PATCH] feat: configurable max ai agent iterations (#7302) * feat: configurable max ai agent iterations * nit * increase limit --- backend/windmill-worker/src/ai/types.rs | 1 + backend/windmill-worker/src/ai_executor.rs | 12 +++++++++--- frontend/src/lib/components/flows/flowInfers.ts | 9 ++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/backend/windmill-worker/src/ai/types.rs b/backend/windmill-worker/src/ai/types.rs index eb22066436..c68bf16a0b 100644 --- a/backend/windmill-worker/src/ai/types.rs +++ b/backend/windmill-worker/src/ai/types.rs @@ -118,6 +118,7 @@ pub struct AIAgentArgs { pub user_images: Option>, pub streaming: Option, pub messages_context_length: Option, + pub max_iterations: Option, } #[derive(Deserialize, Debug)] diff --git a/backend/windmill-worker/src/ai_executor.rs b/backend/windmill-worker/src/ai_executor.rs index d18e32e845..ca62324bdb 100644 --- a/backend/windmill-worker/src/ai_executor.rs +++ b/backend/windmill-worker/src/ai_executor.rs @@ -75,7 +75,8 @@ lazy_static::lazy_static! { }; } -const MAX_AGENT_ITERATIONS: usize = 10; +const DEFAULT_MAX_AGENT_ITERATIONS: usize = 10; +const HARD_MAX_AGENT_ITERATIONS: usize = 1000; pub async fn handle_ai_agent_job( // connection @@ -547,8 +548,13 @@ pub async fn run_agent( None }; + let max_iterations = args + .max_iterations + .map(|m| m.clamp(1, HARD_MAX_AGENT_ITERATIONS)) + .unwrap_or(DEFAULT_MAX_AGENT_ITERATIONS); + // Main agent loop - for i in 0..MAX_AGENT_ITERATIONS { + for i in 0..max_iterations { if used_structured_output_tool { break; } @@ -717,7 +723,7 @@ pub async fn run_agent( if tool_calls.is_empty() { break; - } else if i == MAX_AGENT_ITERATIONS - 1 { + } else if i == max_iterations - 1 { return Err(Error::internal_err( "AI agent reached max iterations, but there are still tool calls" .to_string(), diff --git a/frontend/src/lib/components/flows/flowInfers.ts b/frontend/src/lib/components/flows/flowInfers.ts index 80b9c3b078..9d24adf5d9 100644 --- a/frontend/src/lib/components/flows/flowInfers.ts +++ b/frontend/src/lib/components/flows/flowInfers.ts @@ -65,6 +65,12 @@ export const AI_AGENT_SCHEMA = { description: 'Controls randomness in text generation. Range: 0.0 (deterministic) to 2.0 (random).', showExpr: "fields.output_type === 'text'" + }, + max_iterations: { + type: 'number', + description: + 'Limits how many times the agent can loop through reasoning and tool use. Range: 1-1000.', + default: 10 } }, required: ['provider', 'user_message', 'output_type'], @@ -78,7 +84,8 @@ export const AI_AGENT_SCHEMA = { 'output_schema', 'user_images', 'max_completion_tokens', - 'temperature' + 'temperature', + 'max_iterations' ] }