Heuristic:24mlight A Share investment Agent Backtest Rate Limiting
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Finance, Backtesting |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
Window-based rate limiter for backtesting that caps LLM API calls at 8 per 60-second window with a 6-second minimum interval between calls.
Description
The Backtester._rate_limit() method implements a dual-constraint rate limiter: a sliding 60-second window allowing at most 8 API calls, plus a minimum 6-second gap between consecutive calls. When the window limit is reached, it sleeps until the window resets. When a call is made too soon after the previous one, it sleeps the remaining time. This prevents overwhelming the LLM API (Gemini/OpenAI) during long backtest runs that may span hundreds of trading days.
Usage
Use this heuristic when:
- Running backtests that call the LLM-based workflow on each trading day
- The LLM API has per-minute rate limits (e.g., Gemini free tier)
- You need to avoid
429 Too Many Requestserrors during extended backtests
The Insight (Rule of Thumb)
- Action: Call
self._rate_limit()before every LLM API call in the backtest loop. - Value:
max_calls_per_window=8,window_seconds=60,min_interval=6s. - Trade-off: Backtests run slower (~7.5s per decision minimum) but avoid API rate limit errors that would cause skipped decisions.
- Compatibility: Works with any LLM provider; rate limits are applied at the
run_hedge_fundcall level.
Reasoning
During a backtest spanning 90+ trading days with decision_interval=1, the system calls the full 12-agent workflow (which makes multiple LLM calls per run) on every business day. Without rate limiting, this would easily exceed Gemini's free-tier limits (60 requests/minute) within minutes. The 8-calls-per-60s window provides a safety margin, while the 6s minimum interval spreads calls evenly. The _get_agent_decision method also wraps the agent call in a 3-attempt retry loop with exponential backoff (2^attempt seconds) for transient failures.
Code Evidence
Rate limiter from src/backtester.py:155-176:
def _rate_limit(self) -> None:
# 重置窗口
now = time.time()
if now - self._api_window_start >= 60:
self._api_call_count = 0
self._api_window_start = now
# 简单的窗口限流(避免跑 workflow 时打爆接口)
if self._api_call_count >= 8:
wait_time = 60 - (now - self._api_window_start)
if wait_time > 0:
time.sleep(wait_time)
self._api_call_count = 0
self._api_window_start = time.time()
if self._last_api_call:
delta = time.time() - self._last_api_call
if delta < 6:
time.sleep(6 - delta)
self._last_api_call = time.time()
self._api_call_count += 1
Retry loop with exponential backoff from src/backtester.py:185-243:
max_retries = 3
for attempt in range(max_retries):
try:
self._rate_limit()
result = self.agent(...)
# ... parse result ...
return payload
except Exception as e:
self.logger.warning("获取智能体决策失败(尝试 %s/%s): %s", attempt + 1, max_retries, e)
time.sleep(2**attempt)
return {"decision": {"action": "hold", "quantity": 0}, "analyst_signals": {}}