跳到正文
DEV Community· Ali T·· 5 小時前AI 評分50

如何在正式環境前測試 AI 供應商超時情況

How to Test AI Provider Timeouts Before Production

AI 導讀

文章建議在接入 AI 供應商前,先設定請求期限並測試失敗處理,避免慢請求佔用工作程序,或重試令同一工作重複執行。可在測試環境使用 stub provider,檢查延遲回應、HTTP 429、格式錯誤回應及重複工作 ID 的處理。對有副作用的操作,穩定工作 ID 應貫穿佇列及資料儲存層;記錄請求 ID 和耗時有助追查失敗,毋須儲存完整提示詞。

正文

Ali T

Disclosure: This article was drafted with AI assistance.

When an application depends on an AI provider, write down what your app should do when the provider times out before you wire up retries. Otherwise, a slow request can tie up a worker and a retry can accidentally start the same job twice.

Give the request a deadline

Start with the time a user can reasonably wait for the feature. If a page has a five-second budget, the model call needs a shorter deadline so the app has time to return a useful response. If the work routinely takes longer, give the user a job ID and show its status instead of holding the page open.

Test the failure paths without calling the provider

Use a stub provider in your test environment. Make it return the same outcomes your integration handles in production:

Stub response What to check
Delayed response The app reaches its deadline and returns the intended fallback.
HTTP 429 Retries stay within the deadline and respect the provider’s retry guidance.
Malformed response Validation fails and the app does not store a partial result.
Duplicate job ID The same job does not produce a second user-visible result.

For operations with side effects, carry a stable job ID through the queue and persistence layer. Test that processing the same job twice leaves a single result. Keep request IDs and timing in the logs so you can trace failures without storing the full prompt.

For related reading on cloud infrastructure and AI, see my GlobalDXB technology blog.

來源:DEV Community · dev.to