From 8d0fddc5c848c596c88be21b818e4e86b0b1f8e9 Mon Sep 17 00:00:00 2001 From: nickviljoen Date: Tue, 24 Feb 2026 09:36:51 +0200 Subject: [PATCH] added token usage tracking from LibreChat transactions collection Co-Authored-By: Claude Opus 4.6 --- export_shared_agents.js | 63 ++++++++++++++++++++++++++++++++++++++++- register_agents.py | 5 ++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/export_shared_agents.js b/export_shared_agents.js index 7121225..6b6fc0c 100755 --- a/export_shared_agents.js +++ b/export_shared_agents.js @@ -104,6 +104,61 @@ const pipeline = [ }, { $unwind: { path: "$usageSummary", preserveNullAndEmptyArrays: true } }, + // NEW: Get token usage from transactions collection + { + $lookup: { + from: "conversations", + let: { agentId: "$agentDetails.id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$agent_id", "$$agentId"] }, + endpoint: "agents" + } + }, + // Lookup transactions for each conversation + { + $lookup: { + from: "transactions", + localField: "conversationId", + foreignField: "conversationId", + as: "txns" + } + }, + { $unwind: "$txns" }, + // Aggregate token totals across all conversations + { + $group: { + _id: null, + prompt_tokens: { + $sum: { + $cond: [ + { $eq: ["$txns.tokenType", "prompt"] }, + { $abs: "$txns.rawAmount" }, + 0 + ] + } + }, + completion_tokens: { + $sum: { + $cond: [ + { $eq: ["$txns.tokenType", "completion"] }, + { $abs: "$txns.rawAmount" }, + 0 + ] + } + }, + total_tokens: { + $sum: { $abs: "$txns.rawAmount" } + } + } + } + ], + as: "tokenUsage" + } + }, + { $unwind: { path: "$tokenUsage", preserveNullAndEmptyArrays: true } }, + // Add usage data to agentDetails { $set: { @@ -132,6 +187,11 @@ const pipeline = [ else: null } } + }, + "agentDetails.token_usage": { + prompt_tokens: { $ifNull: ["$tokenUsage.prompt_tokens", 0] }, + completion_tokens: { $ifNull: ["$tokenUsage.completion_tokens", 0] }, + total_tokens: { $ifNull: ["$tokenUsage.total_tokens", 0] } } } }, @@ -141,7 +201,8 @@ const pipeline = [ "agentDetails.instructions": 0, "authorDetails": 0, "usageTimeline": 0, - "usageSummary": 0 + "usageSummary": 0, + "tokenUsage": 0 } } ]; diff --git a/register_agents.py b/register_agents.py index 12166d2..5e741bb 100755 --- a/register_agents.py +++ b/register_agents.py @@ -144,6 +144,7 @@ def build_payload(agent: Dict[str, Any]) -> Dict[str, Any]: # Extract usage data usage_timeline = agent.get("usage_timeline", []) usage_summary = agent.get("usage_summary", {}) + token_usage = agent.get("token_usage", {}) # Construct agent URL for LibreChat agent_url = None @@ -174,6 +175,10 @@ def build_payload(agent: Dict[str, Any]) -> Dict[str, Any]: "total_messages": usage_summary.get("total_messages", 0), "first_used": parse_iso(usage_summary.get("first_used")), "last_used": parse_iso(usage_summary.get("last_used")), + # Token usage: + "prompt_tokens": token_usage.get("prompt_tokens", 0), + "completion_tokens": token_usage.get("completion_tokens", 0), + "total_tokens": token_usage.get("total_tokens", 0), } # Final prune of empties so the API only sees meaningful data for k in list(payload.keys()):