Skip to content

Add uid field to llm_usage Firestore documents for BigQuery cross-user queries #4616

@beastoin

Description

@beastoin

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.


Filed by @Chen on behalf of @mon

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs-toolingLayer: Documentation, examples, dev toolsmaintainerLane: High-risk, cross-system changesp2Priority: Important (score 14-21)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions