> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Serverless Inference 오류 처리 모범 사례는 무엇인가요?

다음 모범 사례를 따라 Serverless Inference 오류를 처리하고 애플리케이션의 안정성을 유지하세요.

## 항상 오류 처리를 구현하세요

API 호출은 `try-except` 블록으로 감싸세요.

```python theme={"system"}
import openai

try:
    response = client.chat.completions.create(
        model="meta-llama/Llama-3.1-8B-Instruct",
        messages=messages
    )
except Exception as e:
    print(f"Error: {e}")
    # 필요에 따라 오류 처리
```

## 지수 백오프를 적용한 재시도 로직 사용하기

일시적인 실패는 시도 간격을 점차 늘려 가며 재시도하세요.

```python theme={"system"}
import time
from typing import Optional

def call_inference_with_retry(
    client, 
    messages, 
    model: str,
    max_retries: int = 3,
    base_delay: float = 1.0
) -> Optional[str]:
    for attempt in range(max_retries):
        try:
            response = client.chat.completions.create(
                model=model,
                messages=messages
            )
            return response.choices[0].message.content
        except Exception as e:
            if attempt == max_retries - 1:
                raise
            
            # 지수 백오프 방식으로 지연 시간 계산
            delay = base_delay * (2 ** attempt)
            print(f"Attempt {attempt + 1} failed, retrying in {delay}s...")
            time.sleep(delay)
    
    return None
```

## 사용량 모니터링

* Forge **Billing** 페이지에서 크레딧 사용량을 추적하세요.
* 한도에 도달하기 전에 알림을 설정하세요.
* 애플리케이션에서 API 사용량을 로깅하세요.

## 특정 오류 코드 처리

```python theme={"system"}
def handle_inference_error(error):
    error_str = str(error)
    
    if "401" in error_str:
        # 인증 정보가 유효하지 않음
        raise ValueError("Check your API key and project configuration")
    elif "402" in error_str:
        # 크레딧 소진
        raise ValueError("Insufficient credits")
    elif "429" in error_str:
        # 요청 속도 제한 초과
        return "retry"
    elif "500" in error_str or "503" in error_str:
        # 서버 오류
        return "retry"
    else:
        # 알 수 없는 오류
        raise
```

## 적절한 timeout 설정하기

사용 사례에 맞는 적절한 timeout을 설정하세요.

```python theme={"system"}
# 응답이 길어지는 경우
client = openai.OpenAI(
    base_url='https://api.inference.wandb.ai/v1',
    api_key="your-api-key",
    timeout=60.0  # timeout 60초
)
```

## 추가 팁

* 디버깅에 활용할 수 있도록 오류를 타임스탬프와 함께 로깅하세요.
* 동시성을 더 효과적으로 처리하려면 비동기 오퍼레이션을 사용하세요.
* 프로덕션 시스템에는 서킷 브레이커를 구현하세요.
* API 호출을 줄이려면 필요한 경우 응답을 캐시하세요.

***

[Inference](https://docs.coreweave.com/ko/support/models/tags/inference.md)
