- Refactored AI gateway to utilize new cost management structures for usage tracking. - Replaced deprecated token extraction methods with a unified cost parsing approach. - Enhanced usage fallback mechanisms and introduced detailed usage metrics in responses. - Added new metering functionality to record AI usage and costs effectively. - Updated tests to reflect changes in usage parsing and cost calculations. - Introduced new API endpoints for retrieving AI usage summaries and pricing information.
35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/ultisuite/ulti-backend/internal/ai/cost"
|
|
"github.com/ultisuite/ulti-backend/internal/llm"
|
|
)
|
|
|
|
// RecordFeatureUsage meters an LLM call from a non-gateway feature.
|
|
func RecordFeatureUsage(ctx context.Context, db *pgxpool.Pool, externalUserID, feature, modelID string, provider llm.Provider, usage llm.UsageDetail) {
|
|
if db == nil || externalUserID == "" {
|
|
return
|
|
}
|
|
q := NewQuotaService(db)
|
|
scope := ResolveBillingScope(ctx, db, externalUserID, provider, false)
|
|
_ = q.RecordUsage(ctx, cost.RecordInput{
|
|
ExternalUserID: externalUserID,
|
|
Feature: feature,
|
|
ModelID: modelID,
|
|
ProviderID: provider.ID,
|
|
BillingScope: scope,
|
|
ProviderKeyFingerprint: cost.KeyFingerprint(provider.ID, provider.APIKey),
|
|
Usage: cost.UsageDetail{
|
|
PromptTokens: usage.PromptTokens,
|
|
CompletionTokens: usage.CompletionTokens,
|
|
CachedInputTokens: usage.CachedInputTokens,
|
|
ReasoningTokens: usage.ReasoningTokens,
|
|
TotalTokens: usage.TotalTokens,
|
|
},
|
|
})
|
|
}
|