RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(agent): finish responses within configured tool budgets (#20847)
Sbaaoui Idriss2a4bbd822494
diff --git a/internal/agent/component/agent.go b/internal/agent/component/agent.goindex d0fd1cf479..a9a8b21007 100644--- a/internal/agent/component/agent.go+++ b/internal/agent/component/agent.go@@ -22,6 +22,7 @@ import ("strings""time"+ "github.com/cloudwego/eino/components/model"einotool "github.com/cloudwego/eino/components/tool""github.com/cloudwego/eino/compose""github.com/cloudwego/eino/flow/agent/react"@@ -183,7 +184,7 @@ func runEinoReActAgent(ctx context.Context, p AgentParam) (*schema.Message, erro}agent, err := react.NewAgent(ctx, &react.AgentConfig{- ToolCallingModel: chatModel,+ ToolCallingModel: &roundLimitedChatModel{ToolCallingChatModel: chatModel, remaining: maxSteps / 2},ToolsConfig: compose.ToolsNodeConfig{Tools: tools,},@@ -256,6 +257,47 @@ func runEinoReActAgent(ctx context.Context, p AgentParam) (*schema.Message, erroreturn msg, nil}+// The reserved final model step must answer rather than schedule more tools.+type roundLimitedChatModel struct {+ model.ToolCallingChatModel+ remaining int+}++func (m *roundLimitedChatModel) WithTools(tools []*schema.ToolInfo) (model.ToolCallingChatModel, error) {+ bound, err := m.ToolCallingChatModel.WithTools(tools)+ if err != nil {+ return nil, err+ }+ return &roundLimitedChatModel{ToolCallingChatModel: bound, remaining: m.remaining}, nil+}++func (m *roundLimitedChatModel) roundOptions(opts []model.Option) []model.Option {+ if m.remaining <= 0 {+ opts = append(opts, model.WithToolChoice(schema.ToolChoiceForbidden))+ }+ m.remaining--+ return opts+}++func (m *roundLimitedChatModel) Generate(ctx context.Context, input []*schema.Message, opts ...model.Option) (*schema.Message, error) {+ input = m.roundInput(input)+ return m.ToolCallingChatModel.Generate(ctx, input, m.roundOptions(opts)...)+}++func (m *roundLimitedChatModel) Stream(ctx context.Context, input []*schema.Message, opts ...model.Option) (*schema.StreamReader[*schema.Message], error) {+ input = m.roundInput(input)+ return m.ToolCallingChatModel.Stream(ctx, input, m.roundOptions(opts)...)+}++func (m *roundLimitedChatModel) roundInput(input []*schema.Message) []*schema.Message {+ if m.remaining > 0 {+ return input+ }+ final := make([]*schema.Message, 0, len(input)+1)+ final = append(final, input...)+ return append(final, schema.SystemMessage("The configured tool-call budget is exhausted. Do not call any more tools. Produce your final answer using the evidence already collected. State any missing evidence or uncertainty; do not claim that unperformed searches were completed."))+}+// scanAllStreamForToolCall consumes the whole model response, then branches// to the Tools node only when any streamed message contains a ToolCall. It// must read to EOF because providers append the tool-call message at the enddiff --git a/internal/entity/models/base_model.go b/internal/entity/models/base_model.goindex 7488431394..5a648d1ffe 100644--- a/internal/entity/models/base_model.go+++ b/internal/entity/models/base_model.go@@ -496,6 +496,8 @@ func buildRequestBody(cfg *ChatConfig, modelName string, messages []Message, strif cfg.Tools != nil {reqBody["tools"] = cfg.Tools+ }+ if cfg.Tools != nil || cfg.ToolChoice != nil || cfg.ToolChoiceValue != nil {switch {case cfg.ToolChoiceValue != nil:reqBody["tool_choice"] = cfg.ToolChoiceValuediff --git a/internal/entity/models/llm.go b/internal/entity/models/llm.goindex e3391d60b1..118d64531c 100644--- a/internal/entity/models/llm.go+++ b/internal/entity/models/llm.go@@ -409,13 +409,6 @@ func (m *EinoChatModel) generateOnce(ctx context.Context, cm *ChatModel, msgs []if err != nil {return nil, err}- // eino's ChatModelAgent binds tools via the per-call model.WithTools option- // (not by calling WithTools). Merge those into the config so the model- // actually emits tool_calls; otherwise the ReAct loop would have no tools.- chatCfg, err = m.chatConfigWithOptsTools(chatCfg, opts)- if err != nil {- return nil, err- }// Once a tool result is in context the model must be free to answer in// prose. Code-exec agents pin tool_choice to "required"/execute_code, and// keeping that pin on the follow-up turn would loop forever instead of@@ -425,6 +418,11 @@ func (m *EinoChatModel) generateOnce(ctx context.Context, cm *ChatModel, msgs []chatCfg.ToolChoice = &choicechatCfg.ToolChoiceValue = nil}+ // Per-call options override defaults, including the release after tool results.+ chatCfg, err = m.chatConfigWithOptsTools(chatCfg, opts)+ if err != nil {+ return nil, err+ }common.Debug("models: eino generate request",zap.String("model", *cm.ModelName),zap.Int("messages", len(internal)),@@ -618,6 +616,7 @@ func (m *EinoChatModel) chatConfigWithOptsTools(base *ChatConfig, opts []model.Ochoice = "auto"case schema.ToolChoiceForbidden:choice = "none"+ cfg.Tools = nilcase schema.ToolChoiceForced:choice = "required"if len(co.AllowedToolNames) == 1 {@@ -817,12 +816,6 @@ func (m *EinoChatModel) Stream(ctx context.Context, msgs []*schema.Message, optsif err != nil {return nil, err}- // Same tool-binding fix as Generate: honor the per-call model.WithTools- // option so the ReAct loop's model requests actually carry tool definitions.- chatCfg, err = m.chatConfigWithOptsTools(chatCfg, opts)- if err != nil {- return nil, err- }// Same tool_choice release as generateOnce: a turn carrying tool results// must be able to produce the final text instead of being forced to call// a tool again, unless an explicit per-call choice requires it.@@ -831,6 +824,11 @@ func (m *EinoChatModel) Stream(ctx context.Context, msgs []*schema.Message, optschatCfg.ToolChoice = &choicechatCfg.ToolChoiceValue = nil}+ // Per-call options override defaults, including the release after tool results.+ chatCfg, err = m.chatConfigWithOptsTools(chatCfg, opts)+ if err != nil {+ return nil, err+ }common.Debug("models: eino stream request",zap.String("model", modelNameOf(m.chain[0])),zap.Int("messages", len(internalMessage)),