You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add rate-limit awareness with automatic 429 retry and adaptive polling
Track X-RateLimit-Limit/Remaining headers from API responses, automatically
retry on HTTP 429 with Retry-After respect, and adaptively slow down
fetchResults() polling when remaining requests are low. Adds public getters
for rate-limit state and configurable retry/threshold settings.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
The SDK automatically handles API rate limits. When the API returns HTTP 429 (Too Many Requests), the client will:
44
+
45
+
1.**Retry automatically** — reads the `Retry-After` header, sleeps for the specified duration, and retries the request (up to 3 times by default).
46
+
2.**Slow down polling** — during `fetchResults()`, when `X-RateLimit-Remaining` drops below the low threshold, polling intervals are automatically increased to avoid hitting the limit.
47
+
48
+
### Inspecting Rate-Limit State
49
+
50
+
After any API call, you can check the current rate-limit values:
51
+
52
+
```php
53
+
$client = new SharpApiClient('your-api-key');
54
+
$client->ping();
55
+
56
+
echo $client->getRateLimitLimit(); // e.g. 60 (requests per window)
57
+
echo $client->getRateLimitRemaining(); // e.g. 58 (remaining in current window)
58
+
```
59
+
60
+
> **Note:**`getRateLimitLimit()` and `getRateLimitRemaining()` return `null` before the first API call or after endpoints that don't return rate-limit headers (e.g. `/ping`, `/quota`).
61
+
62
+
### Configuration
63
+
64
+
```php
65
+
// Max automatic retries on HTTP 429 (default: 3)
66
+
$client->setMaxRetryOnRateLimit(5);
67
+
68
+
// Threshold below which polling intervals are increased (default: 3)
69
+
$client->setRateLimitLowThreshold(5);
70
+
```
71
+
72
+
When `rateLimitRemaining` is at or below the threshold, polling intervals in `fetchResults()` are multiplied by an increasing factor (2x at threshold, growing as remaining approaches 0). This helps avoid 429 errors during long-running job polling.
0 commit comments