diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs index ccd176a4..eb5017cd 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs @@ -18,6 +18,14 @@ public partial class RoutingService if (Context.CurrentRecursionDepth > agent.LlmConfig.MaxRecursionDepth) { _logger.LogWarning($"Current recursive call depth greater than {agent.LlmConfig.MaxRecursionDepth}, which will cause unexpected result."); + // The caller surfaces the LAST dialog as the turn's reply, so returning here with a tool + // frame on top of the history would answer the user with a raw tool result. Add an + // explicit notice so an exhausted tool loop always ends with an assistant message. + var limit = RoleDialogModel.From(dialogs.Last(), role: AgentRole.Assistant, + content: "Stopped after reaching this agent's tool-call limit without a final answer."); + limit.CurrentAgentId = agent.Id; + dialogs.Add(limit); + Context.AddDialogs([limit]); return false; }