Commit e5d7b4d
User
feat(high): Add OpenAI API rate limiting with Token Bucket algorithm
Implemented comprehensive rate limiting:
- Token Bucket algorithm for RPM and TPM limits
- Separate buckets for requests/minute and tokens/minute
- Automatic token refilling at constant rate
- Graceful degradation with queuing when limits reached
- Concurrent request limiting with semaphore
Features:
- Per-model rate limits (gpt-4o: 30K TPM, gpt-4o-mini: 200K TPM)
- Automatic wait time calculation
- Statistics tracking (throttle rate, wait times, etc.)
- Thread-safe with asyncio locks
- Context manager API for easy usage
Integration:
- Integrated into Vision API (_call_openai_vision_api_with_retry)
- Automatic token estimation based on image and schema size
- Added to DI container with lazy initialization
- Global rate limiter registry for singleton pattern
Benefits:
- Prevents 429 rate limit errors from OpenAI
- Automatic queuing instead of failing requests
- Better resource utilization with concurrent limiting
- Detailed metrics for monitoring and optimization
- Configurable limits per model tier
Tests:
- 19 comprehensive unit tests for rate limiter
- Test token bucket algorithm, refilling, wait times
- Test concurrent request limiting
- Test per-model configuration
- All tests passing
Resolves: openai#5 (HIGH priority)
Impact: Eliminates rate limit errors and improves API reliability1 parent d28af64 commit e5d7b4d
File tree
4 files changed
+686
-7
lines changed- examples/erni-foto-agency
- erni_foto_agency
- erni_agents
- performance
- tests/unit
4 files changed
+686
-7
lines changedLines changed: 39 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
65 | | - | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
70 | 78 | | |
71 | 79 | | |
72 | 80 | | |
| |||
113 | 121 | | |
114 | 122 | | |
115 | 123 | | |
| 124 | + | |
| 125 | + | |
116 | 126 | | |
117 | 127 | | |
118 | 128 | | |
| |||
238 | 248 | | |
239 | 249 | | |
240 | 250 | | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
241 | 277 | | |
242 | 278 | | |
243 | 279 | | |
| |||
381 | 417 | | |
382 | 418 | | |
383 | 419 | | |
| 420 | + | |
| 421 | + | |
384 | 422 | | |
385 | 423 | | |
386 | 424 | | |
| |||
Lines changed: 22 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
563 | 564 | | |
564 | 565 | | |
565 | 566 | | |
566 | | - | |
| 567 | + | |
567 | 568 | | |
568 | 569 | | |
569 | 570 | | |
570 | 571 | | |
571 | 572 | | |
572 | 573 | | |
573 | 574 | | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
574 | 580 | | |
575 | 581 | | |
576 | 582 | | |
| |||
585 | 591 | | |
586 | 592 | | |
587 | 593 | | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
588 | 602 | | |
589 | | - | |
590 | | - | |
591 | | - | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
592 | 608 | | |
593 | | - | |
| 609 | + | |
594 | 610 | | |
595 | | - | |
| 611 | + | |
596 | 612 | | |
597 | 613 | | |
598 | 614 | | |
| |||
0 commit comments