Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🪙 fix: Use Fallback Token Transaction if No Collected Usage (#8503)

Danny Avila35d8ef50f497
diff --git a/api/app/clients/BaseClient.js b/api/app/clients/BaseClient.js
index 3815f159be..19f7073fe2 100644
--- a/api/app/clients/BaseClient.js
+++ b/api/app/clients/BaseClient.js
@@ -108,12 +108,15 @@ class BaseClient {
/**
* Abstract method to record token usage. Subclasses must implement this method.
* If a correction to the token usage is needed, the method should return an object with the corrected token counts.
+ * Should only be used if `recordCollectedUsage` was not used instead.
+ * @param {string} [model]
* @param {number} promptTokens
* @param {number} completionTokens
* @returns {Promise<void>}
*/
- async recordTokenUsage({ promptTokens, completionTokens }) {
+ async recordTokenUsage({ model, promptTokens, completionTokens }) {
logger.debug('[BaseClient] `recordTokenUsage` not implemented.', {
+ model,
promptTokens,
completionTokens,
});
@@ -741,9 +744,13 @@ class BaseClient {
} else {
responseMessage.tokenCount = this.getTokenCountForResponse(responseMessage);
completionTokens = responseMessage.tokenCount;
+ await this.recordTokenUsage({
+ usage,
+ promptTokens,
+ completionTokens,
+ model: responseMessage.model,
+ });
}
-
- await this.recordTokenUsage({ promptTokens, completionTokens, usage });
}
if (userMessagePromise) {
diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.js
index c02e147110..97cf83ee9b 100644
--- a/api/server/controllers/agents/client.js
+++ b/api/server/controllers/agents/client.js
@@ -1135,8 +1135,52 @@ class AgentClient extends BaseClient {
}
}
- /** Silent method, as `recordCollectedUsage` is used instead */
- async recordTokenUsage() {}
+ /**
+ * @param {object} params
+ * @param {number} params.promptTokens
+ * @param {number} params.completionTokens
+ * @param {OpenAIUsageMetadata} [params.usage]
+ * @param {string} [params.model]
+ * @param {string} [params.context='message']
+ * @returns {Promise<void>}
+ */
+ async recordTokenUsage({ model, promptTokens, completionTokens, usage, context = 'message' }) {
+ try {
+ await spendTokens(
+ {
+ model,
+ context,
+ conversationId: this.conversationId,
+ user: this.user ?? this.options.req.user?.id,
+ endpointTokenConfig: this.options.endpointTokenConfig,
+ },
+ { promptTokens, completionTokens },
+ );
+
+ if (
+ usage &&
+ typeof usage === 'object' &&
+ 'reasoning_tokens' in usage &&
+ typeof usage.reasoning_tokens === 'number'
+ ) {
+ await spendTokens(
+ {
+ model,
+ context: 'reasoning',
+ conversationId: this.conversationId,
+ user: this.user ?? this.options.req.user?.id,
+ endpointTokenConfig: this.options.endpointTokenConfig,
+ },
+ { completionTokens: usage.reasoning_tokens },
+ );
+ }
+ } catch (error) {
+ logger.error(
+ '[api/server/controllers/agents/client.js #recordTokenUsage] Error recording token usage',
+ error,
+ );
+ }
+ }
getEncoding() {
return 'o200k_base';