Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions open-sse/handlers/chatCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred
thinking: null,
finish_reason: jsonResponse.status || "unknown"
},
status: "success"
status: "success",
endpoint: clientRawRequest?.endpoint || null
}).catch(() => { });

return {
Expand Down Expand Up @@ -656,7 +657,8 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred
tokens: usage,
timestamp: new Date().toISOString(),
connectionId: connectionId || undefined,
apiKey: apiKey || undefined
apiKey: apiKey || undefined,
endpoint: clientRawRequest?.endpoint || null
}).catch(() => { });
}

Expand All @@ -676,7 +678,8 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred
thinking: parsed.choices?.[0]?.message?.reasoning_content || null,
finish_reason: parsed.choices?.[0]?.finish_reason || "unknown"
},
status: "success"
status: "success",
endpoint: clientRawRequest?.endpoint || null
}).catch(() => { });

return {
Expand Down Expand Up @@ -741,7 +744,8 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred
tokens: usage,
timestamp: new Date().toISOString(),
connectionId: connectionId || undefined,
apiKey: apiKey || undefined
apiKey: apiKey || undefined,
endpoint: clientRawRequest?.endpoint || null
}).catch(err => {
console.error("Failed to save usage stats:", err.message);
});
Expand Down Expand Up @@ -781,7 +785,8 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred
null,
finish_reason: translatedResponse?.choices?.[0]?.finish_reason || "unknown"
},
status: "success"
status: "success",
endpoint: clientRawRequest?.endpoint || null
};

// Async save (don't block response)
Expand Down Expand Up @@ -859,7 +864,8 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred
tokens: usage,
timestamp: new Date().toISOString(),
connectionId: connectionId || undefined,
apiKey: apiKey || undefined
apiKey: apiKey || undefined,
endpoint: clientRawRequest?.endpoint || null
}).catch(err => {
console.error("Failed to save streaming usage stats:", err.message);
});
Expand Down
26 changes: 26 additions & 0 deletions src/lib/usageDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ export async function getUsageStats() {
byModel: {},
byAccount: {},
byApiKey: {},
byEndpoint: {},
last10Minutes: [],
pending: pendingRequests,
activeRequests: []
Expand Down Expand Up @@ -585,6 +586,31 @@ export async function getUsageStats() {
apiKeyEntry.lastUsed = entry.timestamp;
}
}

// By Endpoint (endpoint + model + provider combination)
const endpoint = entry.endpoint || "Unknown";
const endpointModelKey = `${endpoint}|${entry.model}|${entry.provider || 'unknown'}`;

if (!stats.byEndpoint[endpointModelKey]) {
stats.byEndpoint[endpointModelKey] = {
requests: 0,
promptTokens: 0,
completionTokens: 0,
cost: 0,
endpoint: endpoint,
rawModel: entry.model,
provider: entry.provider,
lastUsed: entry.timestamp
};
}
const endpointEntry = stats.byEndpoint[endpointModelKey];
endpointEntry.requests++;
endpointEntry.promptTokens += promptTokens;
endpointEntry.completionTokens += completionTokens;
endpointEntry.cost += entryCost;
if (new Date(entry.timestamp) > new Date(endpointEntry.lastUsed)) {
endpointEntry.lastUsed = entry.timestamp;
}
}

return stats;
Expand Down
Loading