From f07580574b9aa7f82cafa87299ebbac13e205a47 Mon Sep 17 00:00:00 2001 From: Roberto Musso Date: Sat, 21 Mar 2026 22:54:34 +0100 Subject: [PATCH] Replace max_turns cap with 90% confidence stopping criterion in agent setup - Remove fixed _MAX_TURNS=5 instruction from system prompt; LLM now decides when to stop based on self-assessed confidence (>= 90%) - Add _MIN_TURNS_BEFORE_NUDGE=3 and raise safety cap to _MAX_TURNS=15 - Nudge message and hard cap still act as a safety net for infinite loops Co-Authored-By: Claude Sonnet 4.6 --- app/api/routes/agent_setup.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/api/routes/agent_setup.py b/app/api/routes/agent_setup.py index a551f8a..f44fc58 100644 --- a/app/api/routes/agent_setup.py +++ b/app/api/routes/agent_setup.py @@ -43,8 +43,10 @@ _SESSION_TTL_SECONDS: int = 1800 # 30 minutes _TEMPLATE_START = "PROMPT_TEMPLATE_START" _TEMPLATE_END = "PROMPT_TEMPLATE_END" -# Maximum number of conversation turns before the LLM is nudged to wrap up. -_MAX_TURNS: int = 5 +# Minimum turns before we consider nudging the LLM to wrap up. +_MIN_TURNS_BEFORE_NUDGE: int = 3 +# Hard cap to avoid infinite loops (safety net, not the primary stopping criterion). +_MAX_TURNS: int = 15 # Max tool-calling steps per LLM invocation. _MAX_TOOL_STEPS: int = 6 @@ -128,8 +130,10 @@ and must perform CRUD operations using tools to create records. It should speci - Concrete examples of mappings based on what you discovered in the directory. {existing_section}\ -Do not ask more than {max_turns} questions total. Begin by exploring the directory, -then ask your first question.\ +Keep asking clarifying questions until you are at least 90% confident you have +enough information to generate an accurate prompt_template. Once you reach that +confidence level, stop asking and produce the final template immediately. +Begin by exploring the directory, then ask your first question.\ """ @@ -150,7 +154,6 @@ def _build_system_prompt( template_start=_TEMPLATE_START, template_end=_TEMPLATE_END, existing_section=existing_section, - max_turns=_MAX_TURNS, ) @@ -356,8 +359,8 @@ async def handle_journey_message( prompt_template = _extract_template(ai_reply) done = prompt_template is not None - # If the LLM didn't produce a template but we've hit max turns, nudge it - # and call the LLM one more time to force template generation. + # If the LLM didn't produce a template, nudge it once it has asked enough + # questions (>= _MIN_TURNS_BEFORE_NUDGE) or hits the hard safety cap. if not done: turns = sum(1 for t in session.history if t["role"] == "user") if turns >= _MAX_TURNS: