Exit Loop Tool

Purpose

Within the modular tool framework of the Tooling System (80556), the Exit Loop Tool provides a targeted mechanism for agents executing looping workflows—such as those managed by the loopagent in the Agent Workflow Management topic—to gracefully exit the loop before reaching a predefined iteration limit.

Looping workflows allow agents to repeatedly execute sub-agents or tasks. However, there are scenarios where continuing the loop is unnecessary or undesired once a specific condition is met. The Exit Loop Tool addresses this by enabling an agent to programmatically escalate an exit action, signaling the workflow manager to break out of the loop early. This avoids wasted computation and enables more responsive and context-aware agent behavior.

Functionality

The Exit Loop Tool is implemented as a specialized function tool (leveraging the generic functiontool subtopic /80570) that agents can invoke through an LLM function call named "exit_loop". It does not require any input arguments and returns an empty response upon execution.

Key Behaviors:

Workflow Interaction:

  1. During each iteration of a looping workflow agent, the sub-agent (often an LLM agent) generates output which may include a function call to "exit_loop".

  2. Upon detection, the Exit Loop Tool executes, setting the escalation flags in the agent context.

  3. The loop agent observes the escalation and interrupts the looping process, returning control and results back to the parent agent or caller.

  4. The tool's response is appended to the event stream, confirming the exit action was processed.

Code Illustration:

The core execution function sets escalation flags in the context:

func exitLoop(ctx tool.Context, myArgs EmptyArgs) (map[string]string, error) {
	ctx.Actions().Escalate = true
	ctx.Actions().SkipSummarization = true
	return map[string]string{}, nil
}

The tool is instantiated as a function tool with a descriptive name and usage instructions:

func New() (tool.Tool, error) {
	return functiontool.New(functiontool.Config{
		Name:        "exit_loop",
		Description: "Exits the loop.\nCall this function only when you are instructed to do so.\n",
	}, exitLoop)
}

Testing and Validation:

The tool has comprehensive unit tests simulating various scenarios within a loop agent:

Tests use a mock LLM model to inject predefined responses and verify the agent's event stream matches expected behavior (see tool/exitlooptool/tool_test.go).

Integration

The Exit Loop Tool integrates tightly with:

This subtopic complements other tools in the framework such as the Artifact Loading Tool or the Google Search Tool by focusing on control flow rather than data retrieval or external interaction.

Diagram

sequenceDiagram
participant Agent
participant LoopAgent
participant LLM
participant ExitLoopTool
Agent->>LLM: Request iteration output
LLM->>Agent: Response with function call "exit_loop"
Agent->>ExitLoopTool: Invoke exit_loop()
ExitLoopTool-->>Agent: Set Escalate flag
Agent->>LoopAgent: Notify loop exit requested
LoopAgent-->>Agent: Stop further iterations
Agent->>Runner: Return loop exit events

This sequence diagram illustrates the process flow when the Exit Loop Tool is invoked during a looping agent’s iteration cycle, highlighting the escalation and loop termination mechanism.