added token usage tracking from LibreChat transactions collection

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
nickviljoen 2026-02-24 09:36:51 +02:00
parent 21ecbd2e8f
commit 8d0fddc5c8
2 changed files with 67 additions and 1 deletions

View file

@ -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
}
}
];

View file

@ -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()):