File size: 1,654 Bytes
4ac256d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
API Usage Tracking Model
Tracks all API calls and their costs for admin monitoring
"""
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Text
from sqlalchemy.sql import func
from config.database import Base


class APIUsage(Base):
    """Track API usage and costs"""
    __tablename__ = 'api_usage'

    id = Column(Integer, primary_key=True)

    # API Information
    provider = Column(String(50), nullable=False)  # openai, gemini, elevenlabs, google_speech, etc.
    service = Column(String(100), nullable=False)  # gpt-4o-mini, whisper, tts, etc.
    endpoint = Column(String(200))  # specific endpoint called

    # Usage Details
    tokens_input = Column(Integer, default=0)  # input tokens (LLMs)
    tokens_output = Column(Integer, default=0)  # output tokens (LLMs)
    characters = Column(Integer, default=0)  # characters (TTS/STT)
    audio_seconds = Column(Float, default=0)  # audio duration (STT/TTS)

    # Cost Information (in USD)
    cost_usd = Column(Float, default=0.0)

    # Context
    user_id = Column(Integer, ForeignKey('users.id'), nullable=True)
    session_id = Column(Integer, ForeignKey('practice_sessions.id'), nullable=True)
    practice_type = Column(String(50))  # reading, conversation, articulation

    # Request details
    request_data = Column(Text)  # JSON string of request params
    response_data = Column(Text)  # JSON string of response (truncated)

    # Timestamps
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    def __repr__(self):
        return f"<APIUsage(provider={self.provider}, service={self.service}, cost=${self.cost_usd:.4f})>"