Skip to content

Commit ed72bf0

Browse files
committed
fix(ai-gateway): 修复 BUDGET_DAILY_TOKEN_LIMIT 默认 0 导致全站 429
1 parent 6562568 commit ed72bf0

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎server/ai-gateway/.env.example‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ===========================================
2+
# 熵减 AI 网关 — 环境变量模板
3+
# ===========================================
4+
# 复制此文件为 .env 并填入实际值
5+
# 切勿将含有真实密钥的 .env 文件提交到 Git
6+
7+
# --- 预算控制(每日 token/费用上限) ---
8+
# 0 = 不限制;默认 free 档 200K tokens / ¥0.5
9+
BUDGET_DAILY_TOKEN_LIMIT=200000
10+
BUDGET_DAILY_COST_LIMIT=0.5

‎server/ai-gateway/cost/budget.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async def dispatch(self, request: Request, call_next):
7777
tracker = get_cost_tracker()
7878
usage = await tracker.get_user_daily_usage(user_id)
7979

80-
if usage["tokens"] >= DAILY_TOKEN_LIMIT:
80+
if DAILY_TOKEN_LIMIT > 0 and usage["tokens"] >= DAILY_TOKEN_LIMIT:
8181
logger.warning(
8282
"预算拦截(token): user=%s, tokens=%d/%d",
8383
user_id, usage["tokens"], DAILY_TOKEN_LIMIT,
@@ -94,7 +94,7 @@ async def dispatch(self, request: Request, call_next):
9494
},
9595
)
9696

97-
if usage["yuan"] >= daily_cost_limit:
97+
if daily_cost_limit > 0 and usage["yuan"] >= daily_cost_limit:
9898
logger.warning(
9999
"预算拦截(cost): user=%s, yuan=%.2f/%.2f",
100100
user_id, usage["yuan"], daily_cost_limit,

‎server/docker-compose.prod.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ services:
121121
SUPABASE_SERVICE_KEY: ${SUPABASE_SERVICE_KEY:-}
122122
# 开发者白名单(邮箱或 user_id,逗号分隔):完全豁免配额/费用限制并赋予 lifetime 身份
123123
DEV_USER_IDS: ${DEV_USER_IDS:-}
124-
# 预算控制(每日 token/费用上限,0=不限制)
125-
BUDGET_DAILY_TOKEN_LIMIT: ${BUDGET_DAILY_TOKEN_LIMIT:-0}
126-
BUDGET_DAILY_COST_LIMIT: ${BUDGET_DAILY_COST_LIMIT:-0}
124+
# 预算控制(0 = 不限制;默认 free 档 200K tokens / ¥0.5)
125+
BUDGET_DAILY_TOKEN_LIMIT: ${BUDGET_DAILY_TOKEN_LIMIT:-200000}
126+
BUDGET_DAILY_COST_LIMIT: ${BUDGET_DAILY_COST_LIMIT:-0.5}
127127
# 付费系统(面包多,可选)
128128
PAYMENT_PROVIDER: ${PAYMENT_PROVIDER:-}
129129
PAYMENT_API_KEY: ${PAYMENT_API_KEY:-}

0 commit comments

Comments
 (0)