Workflow:HKUDS AI Trader Agent Decision Loop
| Knowledge Sources | |
|---|---|
| Domains | LLM_Ops, Autonomous_Agents, Algorithmic_Trading |
| Last Updated | 2026-02-09 14:00 GMT |
Overview
The core iterative reasoning loop where an LLM trading agent receives market context, invokes MCP tools to gather information and execute trades, and produces a trading decision for a single simulated day.
Description
This workflow documents the inner workings of a single trading session. The agent operates as a LangChain agent backed by an OpenAI-compatible LLM, with access to MCP tool servers for price lookups, news/sentiment retrieval, arithmetic operations, and simulated buy/sell execution. For each trading day, the agent receives a system prompt containing its current portfolio positions, yesterday's closing prices, and today's buy prices. It then enters a multi-step reasoning loop where it can call tools, analyze responses, and decide whether to buy, sell, or hold. The loop terminates when the agent emits a finish signal or reaches the maximum step count.
Usage
This workflow represents the core logic inside each day of any AI-Trader simulation. It is automatically invoked by the end-to-end trading workflow for each trading day. Understanding this workflow is essential for customizing agent behavior, modifying the system prompt, or adding new MCP tools.
Execution Steps
Step 1: Construct System Prompt
Build the agent's system prompt for the current trading day. The prompt assembles real-time market context by querying the price tools module for: current positions (from position.jsonl), yesterday's buy and sell prices for all portfolio symbols, and today's opening (buy) prices. This context is injected into a template that instructs the agent on its role as a trading analyst and defines the finish signal token.
Key considerations:
- The prompt includes all NASDAQ-100 (or SSE-50/crypto) symbol prices, providing full market visibility
- Position data comes from the last line of position.jsonl, reflecting the portfolio state after the previous day
- The system prompt is regenerated for each trading day to reflect updated prices and positions
- Market-specific prompts exist for US stocks, A-shares (with T+1 and price limit rules), and crypto (24/7, fractional shares)
Step 2: Initialize LangChain Agent
Create a fresh LangChain agent instance for this trading session, binding the LLM model to the MCP tools retrieved from the MultiServerMCPClient. The agent is configured with the day-specific system prompt, recursion limit, and optional verbose/debug callbacks for logging. Special handling exists for DeepSeek models, which use a custom ChatOpenAI wrapper to fix tool call argument format differences.
Key considerations:
- The agent is recreated each day because the system prompt changes with new market data
- The LLM model connection is reused across days; only the agent wrapper is rebuilt
- Verbose mode enables LangChain's debug and callback handlers for detailed logging
Step 3: Send Initial Query
Send the opening user message that triggers the agent's reasoning process. The message asks the agent to analyze and update today's positions. This serves as the conversation kickoff, prompting the LLM to begin its analysis of the market context provided in the system prompt.
What happens:
- A single user message is sent: "Please analyze and update today's positions"
- The message and all subsequent exchanges are logged to a day-specific JSONL log file
- The agent's response may include direct text reasoning and/or tool calls
Step 4: Execute Tool Call Loop
Enter the iterative reasoning loop where the agent alternates between LLM reasoning and tool execution. In each step, the agent may call one or more MCP tools (price lookup, news search, buy/sell operations, math), receive their results, and decide on next actions. Tool results are appended to the conversation history and fed back to the LLM for the next reasoning step.
Key considerations:
- Maximum steps are configurable (default 30) to prevent infinite loops
- The agent can call multiple tools per step; tool results are concatenated and returned as a user message
- Retry logic with exponential backoff handles transient LLM API failures (up to 3 retries per invocation)
- The recursion limit for LangChain agent execution is set to 100
Step 5: Detect Finish Signal
Monitor each agent response for the finish signal token. When the agent determines its analysis and trading are complete for the day, it outputs the signal in its response text. This terminates the reasoning loop for the current trading day.
What happens:
- The agent explicitly outputs a predefined finish signal string when done
- If the maximum step count is reached without a finish signal, the loop ends regardless
- The final agent response and all intermediate messages are logged
Step 6: Record Trading Outcome
After the reasoning loop ends, check whether any trades were executed during the session. If the agent made buy/sell operations (tracked via a runtime config flag), mark the session as traded. If no trades occurred, write a no-trade record to position.jsonl that carries forward the previous day's positions to the current date, maintaining a complete position history.
Key considerations:
- The IF_TRADE flag is set by the buy/sell MCP tool servers when a trade executes successfully
- No-trade records ensure the position file has an entry for every trading day, enabling continuous metrics calculation
- Position files use file-based locking to handle concurrent access in parallel agent scenarios