in prompt/prompt-executor/prompt-executor-clients/src/commonMain/kotlin/ai/koog/prompt/executor/clients/retry/RetryingLLMClient.kt [67:101]
override fun executeStreaming(
prompt: Prompt,
model: LLModel,
tools: List<ToolDescriptor>
): Flow<StreamFrame> =
flow {
repeat(config.maxAttempts) { attempt ->
var firstFrameReceived = false
try {
delegate.executeStreaming(prompt, model, tools).collect { chunk ->
firstFrameReceived = true
emit(chunk)
}
return@flow
} catch (e: CancellationException) {
throw e // Never retry cancellations
} catch (e: Throwable) {
// If we already received tokens, don't retry - pass error through
if (firstFrameReceived) {
throw e
}
if (!shouldRetry(e) || attempt >= config.maxAttempts - 1) {
throw e
}
val delay = calculateDelay(attempt, e)
logger.warn {
"Stream connection failed before first token (attempt ${attempt + 1}/${config.maxAttempts}). " +
"Retrying in ${delay.inWholeMilliseconds}ms. Error: ${e.message}"
}
delay(delay)
}
}
}