Implementation:HKUDS AI Trader Ainvoke With Retry
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Reliability, LLM_Agents |
| Last Updated | 2026-02-09 14:00 GMT |
Overview
Concrete tool for retrying LLM agent invocations with exponential backoff within the BaseAgent class.
Description
The BaseAgent._ainvoke_with_retry() async method wraps self.agent.ainvoke() with a retry loop. On failure, it waits self.base_delay * attempt seconds (linear backoff) before retrying, up to self.max_retries attempts. The default configuration uses max_retries=3 and base_delay=0.5 seconds.
Usage
Called internally by BaseAgent.run_trading_session() at each step of the reasoning loop. Not intended for direct external use.
Code Reference
Source Location
- Repository: AI-Trader
- File: agent/base_agent/base_agent.py
- Lines: L423-435
Signature
async def _ainvoke_with_retry(self, message: List[Dict[str, str]]) -> Any:
"""
Invoke the agent with retry logic.
Args:
message: Conversation messages to send to the agent
Returns:
Agent response dict with "messages" key
Raises:
Exception: Re-raises the last exception after max_retries exhausted
"""
Import
from agent.base_agent.base_agent import BaseAgent
# Internal method: agent._ainvoke_with_retry(message)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| message | List[Dict[str, str]] | Yes | Conversation messages with role/content |
| self.max_retries | int | Config | Max retry attempts (default 3) |
| self.base_delay | float | Config | Base delay in seconds for backoff (default 0.5) |
Outputs
| Name | Type | Description |
|---|---|---|
| response | Any | Agent response dict from ainvoke() on success |
Usage Examples
Internal Usage in Trading Loop
# Within BaseAgent.run_trading_session():
response = await self._ainvoke_with_retry(message)
agent_response = extract_conversation(response, "final")
Related Pages
Requires Environment
Uses Heuristic
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment