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:
Escalation of Exit Action: When invoked, the tool sets the
Escalateflag on the execution context's action set. This flag is interpreted by the loop agent controller to terminate the current loop iteration cycle immediately.Skip Summarization: It also sets
SkipSummarizationto true, preventing any further summarization or aggregation steps that might normally occur at the end of an iteration.Controlled Invocation: The tool is intended to be called only when the agent is explicitly instructed to exit the loop by the LLM or workflow logic, ensuring deliberate control flow changes.
Workflow Interaction:
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".Upon detection, the Exit Loop Tool executes, setting the escalation flags in the agent context.
The loop agent observes the escalation and interrupts the looping process, returning control and results back to the parent agent or caller.
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:
Exiting mid-loop upon
"exit_loop"function call.Reaching the maximum iteration count without exit.
Immediate exit on the first loop iteration.
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:
Looping Workflow Agents: Particularly the
loopagentfrom Agent Workflow Management. It enables these agents to respond dynamically to LLM instructions to stop iterating.Function Tools: It is a concrete instance of the
functiontoolsubtopic (/80570), inheriting the generic JSON schema inference and invocation mechanisms.Agent Execution Runner: The runner observes the
Escalateaction flag set by the tool to halt looping and proceed accordingly.LLM Agents: The tool is included in the toolset for LLM agents that participate in loops, allowing LLMs to trigger loop exit via function calls.
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.