[GH-ISSUE #4901] [BUG]: Agent custom skill repeat loop indefinitely thinking(with solution) #3084

Closed
opened 2026-02-22 18:32:32 -05:00 by yindo · 1 comment
Owner

Originally created by @unixzilla on GitHub (Jan 25, 2026).
Original GitHub issue: https://github.com/Mintplex-Labs/anything-llm/issues/4901

How are you running AnythingLLM?

Docker (local)

What happened?

When a custom agent skill (e.g., sending an email via SMTP) executes successfully and returns a string response, the LLM continues to re-invoke the same tool in a loop—rephrasing the input and calling the skill repeatedly instead of concluding the interaction.

This happens even though:

The handler.js follows official guidelines (exports a handler function, returns a string, uses this.logger).
The skill executes correctly (email is sent, logs appear as expected).
The user request only requires a single action.
Expected behavior: After a successful tool call, the agent should return the result to the user and stop further tool invocations.

Environment: AnythingLLM v1.9.1 (Docker)

I have a solution to fix the loop, the retun string is exit to end the AI loop thinking..

For Example: (handler.js)

let hasSentEmail = false;
module.exports.runtime = {
  handler: async function ({ userMessage }) {
    try {
	    if (hasSentEmail) {
		    this.logger("Email already sent. Skipping duplicate call.");
		    this.introspect("Email sent!Exit");
		    return "exit";
	    }
      this.logger("Preparing to send email for message: ${userMessage}");
     ...
      this.introspect("SMTP connection Verified");
     ...
      hasSentEmail = true; 
     ...
      this.logger("Email sent: ${info.messageId}");
      this.introspect("Email Sent, executed");
      return "exit";
    } catch (error) {
      const errorMsg = "Failed to send email: ${error.message}";
      this.logger(errorMsg);
      this.introspect(errorMsg);
      return "Sorry, I could not send the email. Error: ${error.message}";
    }
  }
};

You could add system prompt to stop agent thinking:

...
when tool retun a string "exit", you should stop thinking.
...

Are there known steps to reproduce?

No response

Originally created by @unixzilla on GitHub (Jan 25, 2026). Original GitHub issue: https://github.com/Mintplex-Labs/anything-llm/issues/4901 ### How are you running AnythingLLM? Docker (local) ### What happened? When a custom agent skill (e.g., sending an email via SMTP) executes successfully and returns a string response, the LLM continues to re-invoke the same tool in a loop—rephrasing the input and calling the skill repeatedly instead of concluding the interaction. This happens even though: The handler.js follows official guidelines (exports a handler function, returns a string, uses this.logger). The skill executes correctly (email is sent, logs appear as expected). The user request only requires a single action. Expected behavior: After a successful tool call, the agent should return the result to the user and stop further tool invocations. Environment: AnythingLLM v1.9.1 (Docker) I have a solution to fix the loop, the retun string is `exit` to end the AI loop thinking.. For Example: (handler.js) ``` let hasSentEmail = false; module.exports.runtime = { handler: async function ({ userMessage }) { try { if (hasSentEmail) { this.logger("Email already sent. Skipping duplicate call."); this.introspect("Email sent!Exit"); return "exit"; } this.logger("Preparing to send email for message: ${userMessage}"); ... this.introspect("SMTP connection Verified"); ... hasSentEmail = true; ... this.logger("Email sent: ${info.messageId}"); this.introspect("Email Sent, executed"); return "exit"; } catch (error) { const errorMsg = "Failed to send email: ${error.message}"; this.logger(errorMsg); this.introspect(errorMsg); return "Sorry, I could not send the email. Error: ${error.message}"; } } }; ``` You could add system prompt to stop agent thinking: ``` ... when tool retun a string "exit", you should stop thinking. ... ``` ### Are there known steps to reproduce? _No response_
yindo added the possible bug label 2026-02-22 18:32:32 -05:00
yindo closed this issue 2026-02-22 18:32:32 -05:00
Author
Owner

@timothycarambat commented on GitHub (Jan 26, 2026):

This is simply a "model quirk". Some models just love to over-call tools. We actually already have a tool to stop this for any model called the Deduplicator

Which you can use like this
https://github.com/Mintplex-Labs/anything-llm/blob/f52e2866acc11f1d5a6e1c7cb87c3d8ce8eb6bb4/server/utils/agents/aibitat/plugins/rechart.js#L17

Then in the handler

 if (this.tracker.isMarkedUnique(this.name)) {
                this.super.handlerProps.log(
                  `${this.name} has been called for this chat response already. It can only be called once per chat.`
                );
                return "The chart was generated and returned to the user. This function completed successfully. Do not call this function again.";
              }

Depending on if you want to stop any calls, only unique calls (func+payload), or calling the function even twice in a session.

@timothycarambat commented on GitHub (Jan 26, 2026): This is simply a "model quirk". Some models just love to over-call tools. We actually already have a tool to stop this for any model called the [`Deduplicator`](https://github.com/Mintplex-Labs/anything-llm/blob/f52e2866acc11f1d5a6e1c7cb87c3d8ce8eb6bb4/server/utils/agents/aibitat/utils/dedupe.js#L1) Which you can use like this https://github.com/Mintplex-Labs/anything-llm/blob/f52e2866acc11f1d5a6e1c7cb87c3d8ce8eb6bb4/server/utils/agents/aibitat/plugins/rechart.js#L17 Then in the handler ``` if (this.tracker.isMarkedUnique(this.name)) { this.super.handlerProps.log( `${this.name} has been called for this chat response already. It can only be called once per chat.` ); return "The chart was generated and returned to the user. This function completed successfully. Do not call this function again."; } ``` Depending on if you want to stop any calls, only unique calls (func+payload), or calling the function even twice in a session.
yindo changed title from [BUG]: Agent custom skill repeat loop indefinitely thinking(with solution) to [GH-ISSUE #4901] [BUG]: Agent custom skill repeat loop indefinitely thinking(with solution) 2026-06-05 14:50:12 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mintplex-Labs/anything-llm#3084