feat(ai): $5 USD 成本上限 + 自動切換 Ollama (2026-03-29 ogt)

統帥要求:
1. 累積成本超過 $5 USD → 自動停用 Gemini,切換回 Ollama
2. 發送 Telegram 告警通知統帥
3. $4 USD 時發送警告

實作:
- ai_rate_limiter.py: 新增 COST_LIMITS, record_cost(), reset_cost()
- openclaw.py: 每次成功呼叫後記錄成本
- 成本存入 Redis (不過期,手動重置)
- 重置指令: redis-cli DEL ai_rate:total_cost:gemini

API 端點: GET /api/v1/health/ai-usage
- 顯示 total_cost_usd.current/limit/remaining
- 顯示 cost_exceeded: true/false

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-03-28 22:34:51 +08:00
parent 863fc5a426
commit c76a10ad6e
2 changed files with 219 additions and 3 deletions

View File

@@ -908,6 +908,11 @@ class OpenClawService:
)
# Langfuse: 記錄成功評分
trace.score(name="provider_success", value=1.0, comment=f"Success via {provider}")
# 2026-03-29 ogt: 記錄累積成本 (Gemini/Claude)
if cost_usd > 0:
await rate_limiter.record_cost(provider, cost_usd)
return response, provider, True, total_tokens, cost_usd
logger.warning("ai_provider_failed_fallback", provider=provider, latency_ms=latency_ms)