diff --git a/backend/app/models/cost.py b/backend/app/models/cost.py new file mode 100644 index 0000000..7550274 --- /dev/null +++ b/backend/app/models/cost.py @@ -0,0 +1,42 @@ +"""Cost tracking model - per-call usage logs.""" +from sqlalchemy import Column, String, Integer, Text, ForeignKey, Float +from app.models.base import Base, TimestampMixin, TenantMixin + + +class CostLog(Base, TimestampMixin, TenantMixin): + """Records every LLM call's token usage and cost. + + Enables multi-dimensional cost analysis: + tenant x project x task x worker x model + """ + __tablename__ = "cost_logs" + + id = Column(Integer, primary_key=True, autoincrement=True) + + project_id = Column(Integer, ForeignKey("projects.id"), nullable=True, index=True) + task_id = Column(Integer, ForeignKey("tasks.id"), nullable=True, index=True) + worker_id = Column(Integer, ForeignKey("ai_workers.id"), nullable=True, index=True) + + # Model info + provider = Column(String(100), nullable=False) + model_name = Column(String(200), nullable=False) + + # Token counts + prompt_tokens = Column(Integer, default=0) + completion_tokens = Column(Integer, default=0) + total_tokens = Column(Integer, default=0) + + # Cost in cents + cost_cents = Column(Integer, default=0) + + # Duration + duration_ms = Column(Integer, default=0) + + # Status + status = Column(String(50), default="success") # success, error, timeout + + # Error message if any + error_message = Column(Text, nullable=True) + + def __repr__(self): + return f""