- 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.
19 lines
430 B
Go
19 lines
430 B
Go
package cost
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"strings"
|
|
)
|
|
|
|
// KeyFingerprint returns a stable short hash for grouping usage by API key.
|
|
func KeyFingerprint(providerID, apiKey string) string {
|
|
providerID = strings.TrimSpace(providerID)
|
|
apiKey = strings.TrimSpace(apiKey)
|
|
if apiKey == "" {
|
|
return providerID + ":none"
|
|
}
|
|
sum := sha256.Sum256([]byte(providerID + ":" + apiKey))
|
|
return hex.EncodeToString(sum[:8])
|
|
}
|