添加claude账号维度计算token费用前端修复

This commit is contained in:
leslie
2025-07-25 21:48:54 +08:00
parent 1cf70a627f
commit 53e0577e19
2 changed files with 14 additions and 7 deletions

View File

@@ -258,7 +258,8 @@ async function handleChatCompletion(req, res, apiKeyData) {
outputTokens, outputTokens,
cacheCreateTokens, cacheCreateTokens,
cacheReadTokens, cacheReadTokens,
model model,
accountId
).catch(error => { ).catch(error => {
logger.error('❌ Failed to record usage:', error); logger.error('❌ Failed to record usage:', error);
}); });
@@ -327,7 +328,8 @@ async function handleChatCompletion(req, res, apiKeyData) {
usage.output_tokens || 0, usage.output_tokens || 0,
usage.cache_creation_input_tokens || 0, usage.cache_creation_input_tokens || 0,
usage.cache_read_input_tokens || 0, usage.cache_read_input_tokens || 0,
claudeRequest.model claudeRequest.model,
accountId
).catch(error => { ).catch(error => {
logger.error('❌ Failed to record usage:', error); logger.error('❌ Failed to record usage:', error);
}); });

View File

@@ -249,11 +249,16 @@ class ApiKeyService {
keyData.lastUsedAt = new Date().toISOString(); keyData.lastUsedAt = new Date().toISOString();
await redis.setApiKey(keyId, keyData); await redis.setApiKey(keyId, keyData);
// 记录账户级别的使用统计 // 记录账户级别的使用统计(只统计实际处理请求的账户)
const claudeAccountId = accountId || keyData.claudeAccountId; if (accountId) {
if (claudeAccountId) { await redis.incrementAccountUsage(accountId, totalTokens, inputTokens, outputTokens, cacheCreateTokens, cacheReadTokens, model);
await redis.incrementAccountUsage(claudeAccountId, totalTokens, inputTokens, outputTokens, cacheCreateTokens, cacheReadTokens, model); logger.database(`📊 Recorded account usage: ${accountId} - ${totalTokens} tokens (API Key: ${keyId})`);
logger.database(`📊 Recorded account usage: ${claudeAccountId} - ${totalTokens} tokens`); } else if (keyData.claudeAccountId) {
// 如果没有传入accountId但API Key绑定了专属账户也记录统计
await redis.incrementAccountUsage(keyData.claudeAccountId, totalTokens, inputTokens, outputTokens, cacheCreateTokens, cacheReadTokens, model);
logger.database(`📊 Recorded account usage (from API Key binding): ${keyData.claudeAccountId} - ${totalTokens} tokens (API Key: ${keyId})`);
} else {
logger.debug(`⚠️ No accountId provided and API Key not bound to account, skipping account-level statistics`);
} }
} }