MongoDB

MongoDB holds authoritative data: organizations, users, messages, tasks, simulation settings, and most operational entities. The app manages connections through MongoClient in src/memory/mongo_client.py.

Repository pattern

Route handlers should call repository helpers in src/memory/repositories.py (and specialized repositories) instead of issuing raw collection operations inline. That keeps query logic testable and consistent.

Modeling new data

When you add or extend collections, document:
  • Primary keys — ObjectId vs string, and how APIs expose them (JSON usually uses strings).
  • References — Foreign keys such as organization_id and user_id.
  • Indexes — What you need for uniqueness and hot queries (for example per-org constraints).

Schema changes

The codebase and startup scripts enforce shape and indexes; some changes need manual migration steps. Record operator-facing steps in Production rollout when deployments must run them in order.

AI usage accounting collections

For AI cost visibility and reporting:
  • ai_usage_events stores per-call usage/cost events with organization_id, optional user_id, model/request metadata, token counts, mem0 units, and estimated costs.
  • organization_ai_pricing stores org pricing versions with effective ranges and active status.
See AI usage and cost tracking for endpoint contracts and default pricing behavior.