Files
project.linkedcorp.net/backend/database/migrations/032_ai_usage.sql
T
2026-10-23 17:48:49 -05:00

16 lines
781 B
SQL

-- Consumo de la IA por llamada (para el resumen por usuario en Configuracion -> IA). El costo es
-- una estimacion con los precios por millon de tokens vigentes al registrar. Idempotente.
CREATE TABLE IF NOT EXISTS ai_usage (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NULL,
action VARCHAR(40) NOT NULL,
model VARCHAR(60) NOT NULL,
input_tokens INT UNSIGNED NOT NULL DEFAULT 0,
output_tokens INT UNSIGNED NOT NULL DEFAULT 0,
cost_usd DECIMAL(12,6) NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
KEY idx_ai_usage_created (created_at),
KEY idx_ai_usage_user (user_id),
CONSTRAINT fk_ai_usage_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;