Files
dreamstudio/app/Services/Runware/CostEstimator.php
T

28 lines
971 B
PHP
Raw Normal View History

<?php
namespace App\Services\Runware;
use App\Models\RunwareModel;
/**
* Pre-flight cost estimation used by BudgetGuard before a Runware call is
* made. Deliberately conservative — the real cost is always reconciled
* afterward from Runware's response `cost` field (or, for text, `usage.cost`).
*/
class CostEstimator
{
protected const DEFAULT_VIDEO_DURATION_SECONDS = 5;
protected const DEFAULT_TEXT_TOKEN_BUDGET = 2048;
public function estimateUsd(RunwareModel $model, array $params): float
{
return match ($model->unit) {
'per_image' => (float) $model->price_usd * (int) ($params['numberResults'] ?? 1),
'per_second' => (float) $model->price_usd * (int) ($params['duration'] ?? self::DEFAULT_VIDEO_DURATION_SECONDS),
'per_million_tokens' => (float) $model->price_usd
* (((int) ($params['max_completion_tokens'] ?? self::DEFAULT_TEXT_TOKEN_BUDGET)) / 1_000_000),
};
}
}