Heuristic:AI4Finance Foundation FinGPT Rate Limit Management
| Knowledge Sources | |
|---|---|
| Domains | API, Optimization, Financial_Data |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Throttle API calls to Finnhub and OpenAI using `time.sleep` and retry loops to respect rate limits and handle transient failures.
Description
External financial data and LLM APIs impose rate limits (queries per minute for Finnhub, requests per minute for OpenAI). The FinGPT Forecaster pipeline throttles Finnhub calls with `time.sleep(1)` between weekly news fetches and retries OpenAI GPT-4 completions up to 5 times on exception. Without these measures, API calls fail with HTTP 429 or timeout errors, resulting in incomplete datasets.
Usage
Use this heuristic when fetching data from Finnhub (company news, basic financials) or calling OpenAI (GPT-4 label distillation). Apply whenever building datasets via `prepare_data_for_symbol` or `query_gpt4`.
The Insight (Rule of Thumb)
- Action: Add `time.sleep(1)` between Finnhub API calls and wrap OpenAI calls in a retry loop with max 5 attempts.
- Value: 1 second sleep for Finnhub QPM control; 5 retries for OpenAI resilience.
- Trade-off: Slower data acquisition (~1s per weekly batch) but reliable, complete datasets.
- Compatibility: Applies to all Finnhub and OpenAI API integrations.
Reasoning
Finnhub's free tier enforces a queries-per-minute limit. Calling `company_news` for each weekly period in rapid succession triggers HTTP 429. The `time.sleep(1)` throttle ensures calls stay within QPM. For OpenAI, network transients and rate limits cause `chat.completions.create` to fail intermittently; the retry loop with up to 5 attempts recovers gracefully, storing an empty string only after all retries are exhausted.
Code Evidence
Finnhub rate limiting from `fingpt/FinGPT_Forecaster/data.py:69-70`:
time.sleep(1) # control qpm
weekly_news = finnhub_client.company_news(symbol, _from=start_date, to=end_date)
OpenAI retry logic from `fingpt/FinGPT_Forecaster/data.py:222-237`:
cnt = 0
while cnt < 5:
try:
completion = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
]
)
break
except Exception:
cnt += 1
print(f'retry cnt {cnt}')
answer = completion.choices[0].message.content if cnt < 5 else ""