Implementation:HKUDS AI Trader STOP SIGNAL Check
| Knowledge Sources | |
|---|---|
| Domains | LLM_Agents, Control_Flow |
| Last Updated | 2026-02-09 14:00 GMT |
Overview
Pattern implementation for detecting the agent's finish signal in LLM output to terminate the trading session reasoning loop.
Description
The STOP_SIGNAL is defined as the string "<FINISH_SIGNAL>" in prompts/agent_prompt.py (L23). It is injected into the system prompt template: "When you think your task is complete, output {STOP_SIGNAL}". In the trading loop at base_agent.py:L490, the check is a simple string containment: if STOP_SIGNAL in agent_response, break.
Usage
This pattern is checked at every iteration of the trading session loop after extracting the final assistant response. If detected, the loop exits and proceeds to _handle_trading_result().
Code Reference
Source Location
- Repository: AI-Trader
- File: agent/base_agent/base_agent.py (check at L490), prompts/agent_prompt.py (definition at L23)
Signature
# Definition (prompts/agent_prompt.py:L23):
STOP_SIGNAL = "<FINISH_SIGNAL>"
# Usage in system prompt template (L57-58):
# "When you think your task is complete, output\n{STOP_SIGNAL}"
# Check in trading loop (base_agent.py:L490):
if STOP_SIGNAL in agent_response:
break # Exit the multi-step reasoning loop
Import
from prompts.agent_prompt import STOP_SIGNAL
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| agent_response | str | Yes | Final assistant content from extract_conversation(response, "final") |
| STOP_SIGNAL | str | Constant | "<FINISH_SIGNAL>" sentinel token |
Outputs
| Name | Type | Description |
|---|---|---|
| detected | bool | True if STOP_SIGNAL found in agent_response (triggers loop break) |
Usage Examples
Check in Trading Loop
from prompts.agent_prompt import STOP_SIGNAL
from tools.general_tools import extract_conversation
response = await self._ainvoke_with_retry(message)
agent_response = extract_conversation(response, "final")
if STOP_SIGNAL in agent_response:
# Agent is done reasoning for today
break