Skip to content

feat: add rate limiting, 429 retry, and error recovery for LLM calls#343

Open
prthm2910 wants to merge 6 commits into
VectifyAI:mainfrom
prthm2910:main
Open

feat: add rate limiting, 429 retry, and error recovery for LLM calls#343
prthm2910 wants to merge 6 commits into
VectifyAI:mainfrom
prthm2910:main

Conversation

@prthm2910

@prthm2910 prthm2910 commented Jul 3, 2026

Copy link
Copy Markdown

Summary

Adds client-side rate limiting, concurrency control, 429 detection with Retry-After header parsing, exponential backoff with jitter, and cascading KeyError prevention when LLM responses are malformed.

Changes

  • SlidingWindowRateLimiter: Prevents API burst overload with configurable RPM and minimum spacing
  • Concurrency semaphore: Caps concurrent async LLM calls to avoid flooding endpoints
  • 429 detection: Parses provider rate-limit headers and falls back to exponential backoff
  • Graceful degradation: generate_toc_init, generate_toc_continue, toc_detector_single_page, and 3 other functions now return defaults instead of crashing with KeyError
  • CLI flags: --rpm-limit and --max-concurrent-requests for both PDF and markdown paths
  • Config defaults: max_concurrent_requests: 0 and rpm_limit: 0 (disabled by default, opt-in)
  • 40 tests covering the rate limiter, 429 handling, concurrency control, and error recovery

Closes #283

@prthm2910

Copy link
Copy Markdown
Author

Testing summary

All 40 tests pass against the current codebase:

Test suite Tests What it covers
test_rate_limiter.py 8 SlidingWindowRateLimiter timing, spacing, sync/async, disabled states
test_concurrency.py 7 Semaphore limits, no deadlock, settings management, lazy init
test_429_handling.py 16 429 detection, Retry-After parsing, exponential backoff, jitter, retry exhaustion
test_error_recovery.py 9 KeyError prevention in toc_detector, generate_toc_init/continue, add_page_number_to_toc
$ pytest tests/ -v
...
40 passed in 2.39s

Also verified ConfigLoader accepts the new max_concurrent_requests and rpm_limit keys correctly (defaults to 0 = disabled, opt-in).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Unthrottled concurrent LLM requests lead to HTTP 429 Rate Limits and cascading KeyError in tree generation

1 participant