Note: Code examples in this guide are illustrative pseudocode showing recommended patterns. For working examples using the actual LLM4S API, see modules/samples.
Resilience strategies for handling failures, recovering from errors, and gracefully degrading functionality. This guide covers retries, circuit breakers, fallbacks, and failure scenarios.
object GracefulDegradation {
case class RAGResult(
answer: String,
sources: List[String],
groundingScore: Double,
degradedMode: Boolean = false
)
def answerWithDegradation(
query: String,
ragSystem: RAGSystem,
agent: Agent
): RAGResult = {
// Try full RAG pipeline
try {
val sources = ragSystem.retrieve(query)
val context = sources.map(_.content).mkString("\n")
val answer = agent.run(
s"Answer this question using the context:\n$context\n\nQuestion: $query"
)
val groundingScore = ragSystem.checkGrounding(answer.message, sources)
RAGResult(
answer = answer.message,
sources = sources.map(_.url),
groundingScore = groundingScore,
degradedMode = false
)
} catch {
case e: Exception =>
println(s"RAG pipeline failed: ${e.getMessage}. Using degraded mode.")
// Fall back to simple generation without RAG
val answer = agent.run(query)
RAGResult(
answer = answer.message,
sources = List(), // No sources
groundingScore = 0.0, // Unknown grounding
degradedMode = true
)
}
}
// Progressive degradation - disable expensive features
def progressiveDegradation(
operationCost: Double,
costBudgetRemaining: Double
): List[String] = {
val disabledFeatures = scala.collection.mutable.ListBuffer[String]()
if (operationCost > costBudgetRemaining * 0.5) {
disabledFeatures += "grounding_check" // Disable grounding check
}
if (operationCost > costBudgetRemaining * 0.7) {
disabledFeatures += "reranking" // Disable reranking
}
if (operationCost > costBudgetRemaining * 0.9) {
disabledFeatures += "multi_stage_retrieval" // Use single-stage
}
disabledFeatures.toList
}
}
Failure Scenarios & Responses
Timeout Handling
1
2
3
4
5
6
7
8
9
10
11
12
13
object TimeoutHandling {
def handleTimeout[T](
operation: String,
timeoutMs: Long
): Result[String] = {
Result.failure(
s"$operation timed out after ${timeoutMs}ms. " +
s"This may indicate the service is overloaded or network is slow. " +
s"Please try again in a moment."
)
}
}
Rate Limit Handling
1
2
3
4
5
6
7
8
9
10
11
object RateLimitHandling {
def handleRateLimit(
retryAfterSeconds: Int
): Result[String] = {
Result.failure(
s"Rate limit exceeded. Please retry after $retryAfterSeconds seconds. " +
s"Consider implementing exponential backoff and jitter in your client."
)
}
}
Provider Outage Handling
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
object OutageHandling {
def handleProviderOutage(
provider: String,
fallbackAvailable: Boolean
): Result[String] = {
if (fallbackAvailable) {
Result.failure(
s"$provider is temporarily unavailable. Using fallback provider."
)
} else {
Result.failure(
s"$provider is temporarily unavailable and no fallback is configured. " +
s"Please try again later."
)
}
}
}