Principle:HKUDS AI Trader Finish Signal Detection
| Knowledge Sources | |
|---|---|
| Domains | LLM_Agents, Control_Flow |
| Last Updated | 2026-02-09 14:00 GMT |
Overview
A control flow pattern that uses a sentinel string token in LLM output to signal the end of the agent's multi-step reasoning loop.
Description
Finish Signal Detection solves the problem of knowing when an LLM agent has completed its work. Since LLMs generate free-form text, the agent needs an explicit way to communicate "I'm done" to the controlling loop. A predefined sentinel token (STOP_SIGNAL) is injected into the system prompt and the agent is instructed to output it when its task is complete.
The controlling loop checks each agent response for the presence of this token. When detected, the loop breaks and proceeds to record the trading outcome. If not detected within max_steps iterations, the loop also terminates to prevent infinite reasoning.
Usage
Use this principle in any LLM agent loop where the agent needs multiple reasoning steps and must explicitly signal completion. The signal token must be defined consistently in both the system prompt and the loop's termination condition.
Theoretical Basis
# Pseudocode for finish signal detection
STOP_SIGNAL = "<FINISH_SIGNAL>"
# Injected into system prompt:
# "When you think your task is complete, output {STOP_SIGNAL}"
for step in range(max_steps):
response = invoke_agent(messages)
final_text = extract_final(response)
if STOP_SIGNAL in final_text:
break # Agent signaled completion
# Otherwise, continue the reasoning loop
tool_results = extract_tools(response)
messages.extend(tool_results)
# Fallback: loop exits after max_steps even without signal
Key properties:
- Explicit termination: Agent must actively output the signal (not just stop generating)
- String containment: Simple "in" check, robust to surrounding text
- Bounded: max_steps provides a hard upper limit regardless of signal
- Prompt-injected: The signal token is defined once and injected into the prompt