<?php
use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Http\Exceptions\HttpRequestException;
function withRetry(callable $fn, int $maxRetries = 3): mixed {
$attempt = 0;
$lastException = null;
while ($attempt < $maxRetries) {
try {
return $fn();
} catch (HttpRequestException $e) {
$lastException = $e;
$attempt++;
// Only retry on rate limit errors
if (strpos($e->getMessage(), 'rate limit') === false &&
$e->getCode() !== 429) {
throw $e;
}
if ($attempt >= $maxRetries) {
break;
}
// Exponential backoff
$sleepTime = (2 ** $attempt);
echo "Rate limit hit. Retrying in $sleepTime seconds...\n";
sleep($sleepTime);
}
}
throw $lastException;
}
// Usage
$inference = new Inference();
try {
$response = withRetry(function() use ($inference) {
return $inference->with(
messages: 'What is the capital of France?'
)->get();
});
echo "Response: $response\n";
} catch (HttpRequestException $e) {
echo "All retry attempts failed: " . $e->getMessage() . "\n";
}
// @doctest id="229e"