feat:AI 토큰 사용량 추적 기능 추가

- ai_token_usages 테이블 마이그레이션 생성
- AiTokenUsage 모델 생성
- AiReportService에 usageMetadata 추출 및 저장 로직 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
김보곤
2026-02-07 09:57:12 +09:00
parent 78851ec04a
commit f45f91967f
3 changed files with 122 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('ai_token_usages', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->constrained()->comment('테넌트 ID');
$table->string('model', 100)->comment('AI 모델명 (gemini-2.0-flash 등)');
$table->string('menu_name', 100)->comment('호출 메뉴/기능명 (AI리포트, 명함OCR 등)');
$table->unsignedInteger('prompt_tokens')->default(0)->comment('입력 토큰 수');
$table->unsignedInteger('completion_tokens')->default(0)->comment('출력 토큰 수');
$table->unsignedInteger('total_tokens')->default(0)->comment('전체 토큰 수');
$table->decimal('cost_usd', 10, 6)->default(0)->comment('예상 비용 (USD)');
$table->decimal('cost_krw', 12, 2)->default(0)->comment('예상 비용 (KRW)');
$table->string('request_id', 100)->nullable()->comment('요청 추적 ID');
$table->foreignId('created_by')->nullable()->constrained('users')->comment('생성자 ID');
$table->timestamps();
$table->index(['tenant_id', 'created_at'], 'idx_ai_token_tenant_date');
$table->index(['tenant_id', 'menu_name'], 'idx_ai_token_tenant_menu');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('ai_token_usages');
}
};