# 全部重试用尽 → 降级,而不是把异常抛给用户
·ThisToken.AI·
Tutorials入门教程ThisToken.AI
print(f"[降级] 重试用尽: {last_exc}")
return fallback
if __name__ == "__main__":
result = chat_with_fallback("用一句话解释什么是指数退避。")
print(result)
How to run:
export THISTOKEN_API_KEY="你的Key"
pip install openai
python retry_demo.py
A few details worth noting:
- **`classify_error` is the core.** It turns the "should I retry?" decision from vague intuition into explicit rules. You can refine the categories based on your own business.
- **Exponential backoff, not fixed intervals.** Under rate limiting, fixed-interval retries are essentially a sustained attack.
- **The fallback result must be "usable."** The example above returns a friendly message; in your business, degradation could be a cached previous result, a rule engine's fallback output, or a clear "try again later" entry point — but never an uncaught stack trace.
## Three Levels of Degradation Strategy
Exhausting retries is just the starting point. How well you handle degradation directly determines what users see during outages:
1. **Message degradation**: return a friendly notice (what the example code does — the lowest barrier to entry).
2. **Cache degradation**: cache the most recent successful result, and during outages return stale data labeled "data updated X minutes ago." Very practical for content generation scenarios.
3. **Pipeline degradation**: switch to a backup model when the primary one is unavailable. If your access layer supports multiple models, you can wrap model selection logic around `chat_with_fallback`.
## Final Thoughts
Error handling isn't a "finishing touch" before launch — it's the first foundation stone of API integration. Get this code running first, then gradually tune the classification rules and backoff parameters based on your business's error distribution. Compared to firefighting after the fact, this upfront investment pays off handsomely.
If you don't have an account yet, go to https://api.thistoken.ai/register to sign up, create an API key, run the example above, and see for yourself how retries and degradation behave in your own network environment.
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.Token.AI を試してみませんか?
プロジェクトレベルの API Key を作成し、コンソールでチャネルを有効にして、ルーティング、予算、監査ログを設定しましょう。
注册 ThisToken.AI 并获取 API Key