-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Labels
docs-toolingLayer: Documentation, examples, dev toolsLayer: Documentation, examples, dev toolsmaintainerLane: High-risk, cross-system changesLane: High-risk, cross-system changesp2Priority: Important (score 14-21)Priority: Important (score 14-21)
Description
Context
The backend recently added LLM token usage tracking via backend/utils/llm/usage_tracker.py and backend/database/llm_usage.py. It records per-user token consumption by feature and model into Firestore at users/{uid}/llm_usage/{YYYY-MM-DD}.
The uid is in the document path but not stored as a field inside the document. This makes cross-user BigQuery queries impossible without parsing document paths.
What needs to change
In backend/database/llm_usage.py, function record_llm_usage() around line 60, add "uid": uid to the update_data dict:
update_data = {
f"{feature}.{safe_model}.input_tokens": firestore.Increment(input_tokens),
f"{feature}.{safe_model}.output_tokens": firestore.Increment(output_tokens),
f"{feature}.{safe_model}.call_count": firestore.Increment(1),
"uid": uid, # <-- ADD THIS
"date": doc_id,
"last_updated": datetime.now(timezone.utc),
}Why
- Enables BigQuery queries like
SELECT uid, SUM(tokens) FROM llm_usage GROUP BY uid ORDER BY 2 DESC - Current mentor notification analysis shows top user burned $36.56 in 3 days on agent calls alone
- 251 daily active users generating ~109K+ LLM calls/day just from mentor notifications
- Need uid field to do cost-per-user analysis after Firestore-to-BigQuery export
Scope
One-line change in backend/database/llm_usage.py. Existing tests in backend/tests/unit/test_llm_usage_db.py may need a minor update to assert the uid field.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
docs-toolingLayer: Documentation, examples, dev toolsLayer: Documentation, examples, dev toolsmaintainerLane: High-risk, cross-system changesLane: High-risk, cross-system changesp2Priority: Important (score 14-21)Priority: Important (score 14-21)