한눈에 보기
목표: OpenAI API 오류 처리 테스트
시간: 10분
Plugins:GenericRandomErrorPlugin, RetryAfterPlugin
필수 구성 요소:개발 프록시 설정
앱에서 OpenAI API를 사용하는 경우 앱에서 API 오류를 처리하는 방법을 테스트해야 합니다. 개발자 프록시를 사용하면 GenericRandomErrorPlugin사용하여 OpenAI API에서 오류를 시뮬레이션할 수 있습니다.
RetryAfterPlugin을 사용하면 개발자 프록시는 앱이 API를 다시 호출하기 전에 Retry-After 헤더에 지정된 시간을 기다리는지 확인합니다.
팁
명령 프롬프트 devproxy config get openai-throttling실행하여 이 사전 설정을 다운로드합니다.
프로젝트 폴더에서 devproxyrc.json라는 이름의 새 파일을 만듭니다. 코드 편집기에서 파일을 엽니다.
plugins참조하는 GenericRandomErrorPlugin 배열에 새 개체를 만듭니다. Dev Proxy가 감시하도록 OpenAI API URL을 정의하고 플러그인 구성에 대한 참조를 추가합니다.
파일: devproxyrc.json
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/rc.schema.json",
"plugins": [
{
"name": "GenericRandomErrorPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "openAIAPI"
}
],
"urlsToWatch": [
"https://api.openai.com/*"
]
}
RetryAfterPlugin을 추가하고 플러그인 구성 개체를 만들어 GenericRandomErrorPlugin에 오류 응답의 위치와 실패할 요청의 비율을 제공합니다.
파일: devproxyrc.json(전체 구성)
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/rc.schema.json",
"plugins": [
{
"name": "RetryAfterPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
},
{
"name": "GenericRandomErrorPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "openAIAPI"
}
],
"urlsToWatch": [
"https://api.openai.com/*"
],
"openAIAPI": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/genericrandomerrorplugin.schema.json",
"errorsFile": "openai-errors.json",
"rate": 90
}
}
Caution
구성 파일에 RetryAfterPlugin 앞에 GenericRandomErrorPlugin을 추가합니다. 나중에 추가하면 GenericRandomErrorPlugin가 확인하기 전에 RetryAfterPlugin가 요청을 실패시킵니다.
동일한 폴더에서 openai-errors.json 파일을 만듭니다. 이 파일에는 Dev Proxy가 요청에 실패할 때 선택하는 오류 응답이 포함되어 있습니다. 이는 OpenAI API가 반환하는 오류와 일치합니다.
| 상태 | error.code |
시뮬레이션하는 내용 |
|---|---|---|
| 429 | rate_limit_exceeded |
앱이 TPM(분당 토큰) 또는 RPM(분당 요청) 제한에 도달했습니다. |
| 429 | slow_down |
앱의 요청 속도가 너무 빠르게 증가했습니다. |
| 429 | credit_balance_exhausted |
조직에 선불 크레딧이 남아 있지 않습니다. 재시도는 도움이 되지 않습니다. |
| 503 | server_is_overloaded |
모델이 일시적으로 과부하 상태입니다. |
이러한 오류에 대한 자세한 내용은 OpenAI 설명서의 오류 코드를 참조하세요.
파일: openai-errors.json
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/genericrandomerrorplugin.errorsfile.schema.json",
"errors": [
{
"request": {
"url": "https://api.openai.com/*"
},
"responses": [
{
"statusCode": 429,
"headers": [
{
"name": "content-type",
"value": "application/json; charset=utf-8"
},
{
"name": "Retry-After",
"value": "@dynamic"
}
],
"body": {
"error": {
"message": "Rate limit reached for gpt-4.1 in organization org-K7hT684bLccDbBRnySOoK9f2 on tokens per min (TPM): Limit 30000, Used 30000, Requested 1200. Please try again in 2.4s. Visit https://platform.openai.com/settings/organization/limits to learn more.",
"type": "tokens",
"param": null,
"code": "rate_limit_exceeded"
}
}
},
{
"statusCode": 429,
"headers": [
{
"name": "content-type",
"value": "application/json; charset=utf-8"
},
{
"name": "Retry-After",
"value": "@dynamic"
}
],
"body": {
"error": {
"message": "Rate limit reached for gpt-4.1 in organization org-K7hT684bLccDbBRnySOoK9f2 on requests per min (RPM): Limit 500, Used 500, Requested 1. Please try again in 120ms. Visit https://platform.openai.com/settings/organization/limits to learn more.",
"type": "requests",
"param": null,
"code": "rate_limit_exceeded"
}
}
},
{
"statusCode": 429,
"headers": [
{
"name": "content-type",
"value": "application/json; charset=utf-8"
},
{
"name": "Retry-After",
"value": "@dynamic"
}
],
"body": {
"error": {
"message": "Your request rate increased too quickly. Reduce your request rate and increase it gradually.",
"type": "rate_limit_error",
"param": null,
"code": "slow_down"
}
}
},
{
"statusCode": 429,
"headers": [
{
"name": "content-type",
"value": "application/json; charset=utf-8"
}
],
"body": {
"error": {
"message": "Your organization has no prepaid credits remaining. Add credits to continue using the API. For more information on this error, read the docs: https://developers.openai.com/api/docs/guides/error-codes.",
"type": "insufficient_quota",
"param": null,
"code": "credit_balance_exhausted"
}
}
},
{
"statusCode": 503,
"headers": [
{
"name": "content-type",
"value": "application/json; charset=utf-8"
}
],
"body": {
"error": {
"message": "The requested model is temporarily overloaded. Please try again later.",
"type": "service_unavailable_error",
"param": null,
"code": "server_is_overloaded"
}
}
}
]
}
]
}
@dynamic 값은 Retry-After 헤더를 설정하고 앱이 얼마나 오래 대기해야 하는지 RetryAfterPlugin에 추적하도록 지시합니다. 기다린다고 해결되지 않으므로 credit_balance_exhausted 응답에는 Retry-After 헤더가 없습니다.
프로젝트 폴더에서 Dev Proxy를 시작하세요.
devproxy
앱이 OpenAI API를 호출하면 Dev Proxy가 openai-errors.json 파일의 임의 오류와 함께 요청의 90%를 실패시킵니다. 앱이 RetryAfterPlugin 헤더에 표시된 시간 전에 API를 다시 호출하면 Retry-After이 이를 보고하고 요청을 제한합니다.
앱이 다음과 같은지 확인하세요:
-
rate_limit_exceeded또는Retry-After오류 후slow_down시간 동안 기다립니다. - 다시 시도하는 대신 오류 발생 후
credit_balance_exhaustedAPI 호출을 중지합니다. -
server_is_overloaded오류 후 지연을 두고 다시 시도하고 재시도 횟수를 모두 소진하면 명확한 메시지를 표시합니다.
GenericRandomErrorPlugin에 대해 자세히 알아봅니다.
참고하십시오
Dev Proxy