Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:HKUDS AI Trader Agent Ainvoke

From Leeroopedia


Knowledge Sources
Domains LLM_Agents, Inference
Last Updated 2026-02-09 14:00 GMT

Overview

Wrapper documentation for LangChain's agent.ainvoke() method as used in the AI-Trader trading loop.

Description

The agent.ainvoke() call is the core inference step within BaseAgent's trading loop. The agent Runnable is created per-session via create_agent(self.model, tools=self.tools, system_prompt=...) and invoked with the conversation message list. The response dict contains a "messages" key with the full conversation chain including any tool call results. The recursion_limit=100 prevents infinite tool-calling loops.

Usage

Used internally within BaseAgent._ainvoke_with_retry() at base_agent.py:L429. Not called directly by external code; always wrapped in the retry mechanism.

Code Reference

Source Location

  • Repository: AI-Trader
  • File: agent/base_agent/base_agent.py
  • Lines: L429 (call site), L450-454 (agent creation)

Signature

# Agent creation (per trading session):
self.agent = create_agent(self.model, tools=self.tools, system_prompt=system_prompt)

# Invocation:
response = await self.agent.ainvoke(
    {"messages": message},    # message: List[Dict[str, str]]
    {"recursion_limit": 100}  # Prevent infinite tool loops
)

Import

from langchain.agents import create_agent
# agent.ainvoke() is a method on the returned Runnable

I/O Contract

Inputs

Name Type Required Description
messages List[Dict[str, str]] Yes Conversation message list with role and content keys
recursion_limit int No Max tool-call recursion depth (default 100)

Outputs

Name Type Description
response dict Dict with "messages" key containing full conversation chain (AIMessage + ToolMessage objects)

Usage Examples

Single Invocation

message = [{"role": "user", "content": "Please analyze and update today's (2025-01-15) positions."}]

response = await self.agent.ainvoke(
    {"messages": message},
    {"recursion_limit": 100}
)

# Extract final assistant response:
from tools.general_tools import extract_conversation
final_response = extract_conversation(response, "final")

Related Pages

Requires Environment

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment