ApiGateway
API 토큰 / 사용량 / 단가 — 정� 평가 + 비용 계산
상태
| 항목 | 값 |
|---|---|
| Layer | core |
| Tier | L1 |
| Status | wip |
| Version | 0.9.0 |
| 가격 | Free (free) |
| **카� | |
| �고리** | Infrastructure |
개요
개요
ApiGateway 는 multi-saas-kit 의 Layer 1 Core Plugin � 니다. API 게이트웨이 운영의 표준 비용 계산 엔진과 사용량 측정 어댑터를 제공합니다.
토큰 정�
검증(ApiAccessKeyPolicyEvaluator)은 이미 Core/Base/Security/ApiAccess 에 구현되어 있으므로, Plugin 은 그 위 레이어인 Pricing(가격) + Usage(사용량 측정) 을 표준화합니다.
핵심 컴포넌트
PricingCalculator (Pure)
- 단가(price_usd) + 단위(unit) + 수량(quantity) → USD 비용
calculate($priceUsd, $unit, $quantity): floatcalculateFor(PricingEntryInterface, $quantity): floatcalculateBreakdown([...]): array— � 출력 토큰 등 다중 segment 합산- 표준 단위:
1m_chars,1m_tokens,1k_tokens,minute,second,hour,request - 미등록 단위 →
UnknownPricingUnitException - decimal precision 설정 가능 (기본 6)
UsageQuantityExtractor (Pure)
- 서비스별 quantity 추출 규칙 (config 기반)
tts→char_countstt→audio_duration_ms(ms_to_minute transform 적용)llm→input_tokens(input) +output_tokens(output)- 새 서비스 추가 시 config 만 수정하면 됨
PricingEntryInterface / PricingLookupInterface
프로젝트의 ApiPricing Eloquent Model 이 PricingEntryInterface 를 구현하면 그대로 Plugin 이 소비. 가격 저장소(�
�이블/캐시/외부 API)는 PricingLookupInterface 로 추상화.
설정 (config/api-gateway.php)
return [
'enabled' => env('PLG_API_GATEWAY_ENABLED', true),
'pricing' => [
'unit_divisors' => [
'1m_chars' => 1_000_000,
'1m_tokens' => 1_000_000,
'1k_tokens' => 1_000,
'minute' => 1,
'second' => 1,
'hour' => 1,
'request' => 1,
],
'currency' => 'USD',
'precision' => 6,
],
'usage' => [
'extractors' => [
'tts' => [['field' => 'char_count', 'unit_hint' => '1m_chars']],
'stt' => [['field' => 'audio_duration_ms', 'unit_hint' => 'minute', 'transform' => 'ms_to_minute']],
'llm' => [
['field' => 'input_tokens', 'direction' => 'input', 'unit_hint' => '1m_tokens'],
['field' => 'output_tokens', 'direction' => 'output', 'unit_hint' => '1m_tokens'],
],
],
],
];
사용 예시
use App\Plugins\ApiGateway\Services\PricingCalculator;
use App\Plugins\ApiGateway\Services\UsageQuantityExtractor;
// 1) 단순 비용 계산
$calc = PricingCalculator::fromConfig(config('api-gateway.pricing'));
$cost = $calc->calculate(16.0, '1m_chars', 500_000); // $8.00
// 2) 사용량 → segments → breakdown
$ext = UsageQuantityExtractor::fromConfig(config('api-gateway.usage'));
$segments = $ext->extract('llm', ['input_tokens' => 1000, 'output_tokens' => 500]);
// PricingLookupInterface 구현체로 단가 조회 후 breakdown 계산
$priced = array_map(fn ($seg) => [
'entry' => $lookup->lookup('llm', 'openai', 'gpt-4o', direction: $seg['direction']),
'quantity' => $seg['quantity'],
], $segments);
$breakdown = $calc->calculateBreakdown($priced);
// ['total_usd' => 0.0075, 'currency' => 'USD', 'segments' => [...]]
출처
apis.how 의 ApiPricing::calculateCost() / unitDivisor() 와 ApiUsageLog 페이로드 구조에서 추출. LiteLLMActualCostSynchronizer 의 비용 동기화 흐름은 Phase 3 (Models + Filament) 에서 표준화 예정.
다음 단계 (Phase 3+)
- 표준
ApiToken/ApiUsageLog/ApiPricingModel + Migration (선택 활성) - Filament Resource (Token CRUD + Pricing 편집 + Usage Dashboard)
- ApiUsageAnalytics Plugin (Phase 3 Composite) 와 연동
- LiteLLM 등 비용 sync adapter
의존성
- Core/Base/Security/ApiAccess (
ApiAccessKeyPolicyEvaluator) - Plugin 자체는 모델 의존 없음 (Pure Service + Interface)
라이선스
MIT
데모
- Platform 관리자 패널에서 메타 확인 🔒 로그인 필요
- 사용 예시 (PHP)
🛒 Plugin Store에서 보기: store.codebase.how/plugins/api-gateway