File size: 658 Bytes
0772b5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import asyncio
import logging
from typing import Callable, Any, Optional
from llm.errors import ErrorClassifier, ErrorCategory
from llm.key_pool import APIKeyPool

logger = logging.getLogger(__name__)


class KeyRetryPolicy:
    """Level 1 Key-level retry policy for the same model across available keys."""

    def __init__(self, max_key_attempts: int = 3):
        self.max_key_attempts = max_key_attempts

    def should_retry(self, category: ErrorCategory) -> bool:
        return category in (
            ErrorCategory.RATE_LIMIT,
            ErrorCategory.TIMEOUT,
            ErrorCategory.NETWORK,
            ErrorCategory.SERVER_ERROR,
        )