feat(chat): NemoClaw Claude API 加 token+費用統計
Some checks failed
CD Pipeline / build-and-deploy (push) Has been cancelled
Some checks failed
CD Pipeline / build-and-deploy (push) Has been cancelled
Claude Haiku 4.5: Input $0.80/1M, Output $4.00/1M 每次回覆顯示: token 數 | 本次費用 | 本月累計 Redis key: claude_cost:YYYY-MM,TTL 40 天 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -176,7 +176,30 @@ class ChatManager:
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
return data["content"][0]["text"].strip()
|
||||
text = data["content"][0]["text"].strip()
|
||||
|
||||
# Token/費用統計 — Claude Haiku 4.5: Input $0.80/1M, Output $4.00/1M
|
||||
usage = data.get("usage", {})
|
||||
in_tok = usage.get("input_tokens", 0)
|
||||
out_tok = usage.get("output_tokens", 0)
|
||||
cost = (in_tok * 0.0000008) + (out_tok * 0.000004)
|
||||
|
||||
# 月累計到 Redis
|
||||
from src.core.redis_client import get_redis
|
||||
from src.utils.timezone import now_taipei
|
||||
redis = get_redis()
|
||||
month_key = f"claude_cost:{now_taipei().strftime('%Y-%m')}"
|
||||
try:
|
||||
current = float(await redis.get(month_key) or 0)
|
||||
new_total = current + cost
|
||||
await redis.set(month_key, str(round(new_total, 6)), ex=40 * 24 * 3600)
|
||||
except Exception:
|
||||
new_total = cost
|
||||
|
||||
logger.info("nemotron_claude_usage", in_tokens=in_tok, out_tokens=out_tok,
|
||||
cost_usd=round(cost, 6), monthly_total_usd=round(new_total, 4))
|
||||
|
||||
return f"{text}\n\n<i>📊 {in_tok+out_tok} tokens | ${cost:.4f} | 本月累計 ${new_total:.4f}</i>"
|
||||
except Exception as e:
|
||||
logger.warning("nemotron_chat_failed", error=str(e))
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user