Documentation
¶
Index ¶
- Constants
- Variables
- func CalculateBudgetRemaining(budgetLimit, budgetUsed float64) float64
- func CalculateCost(provider, model string, inputTokens, outputTokens int, isCustomConfig bool) float64
- func DefaultAPIKeyConfig() models.APIKeyConfig
- func ExtractKeyPrefix(key string) string
- func GenerateAPIKey() (string, error)
- func HashAPIKey(key string) string
- type APIKeyService
- func (s *APIKeyService) AutoMigrate() error
- func (s *APIKeyService) CreateAPIKey(ctx context.Context, req *models.APIKeyCreateRequest) (*models.APIKeyResponse, error)
- func (s *APIKeyService) DeleteAPIKey(ctx context.Context, id uint) error
- func (s *APIKeyService) GetAPIKey(ctx context.Context, id uint) (*models.APIKeyResponse, error)
- func (s *APIKeyService) GetByHash(ctx context.Context, keyHash string) (*models.APIKey, error)
- func (s *APIKeyService) ListAPIKeys(ctx context.Context, limit, offset int) ([]models.APIKeyResponse, int64, error)
- func (s *APIKeyService) ListAPIKeysByProjectID(ctx context.Context, projectID uint, limit, offset int) ([]models.APIKeyResponse, int64, error)
- func (s *APIKeyService) ListAPIKeysByUserID(ctx context.Context, userID string, limit, offset int) ([]models.APIKeyResponse, int64, error)
- func (s *APIKeyService) RevokeAPIKey(ctx context.Context, id uint) error
- func (s *APIKeyService) UpdateAPIKey(ctx context.Context, id uint, updates map[string]any) error
- func (s *APIKeyService) ValidateAPIKey(ctx context.Context, key string) (*models.APIKey, error)
- type BudgetResetScheduler
- type CreateCheckoutParams
- type CreditsService
- func (s *CreditsService) AddCredits(ctx context.Context, params models.AddCreditsParams) (*models.CreditTransaction, error)
- func (s *CreditsService) AutoMigrate() error
- func (s *CreditsService) CheckSufficientCredits(ctx context.Context, organizationID string, amount float64) error
- func (s *CreditsService) DeductCredits(ctx context.Context, params models.DeductCreditsParams) (*models.CreditTransaction, error)
- func (s *CreditsService) GetOrganizationCredit(ctx context.Context, organizationID string) (*models.OrganizationCredit, error)
- func (s *CreditsService) GetTransactionHistory(ctx context.Context, organizationID string, limit, offset int) ([]models.CreditTransaction, error)
- type ModelPricing
- type ProviderPricing
- type RateLimiter
- type RecordTask
- type Service
- func (s *Service) AutoMigrate() error
- func (s *Service) CheckBudgetLimit(ctx context.Context, apiKeyID uint) (bool, *models.APIKey, error)
- func (s *Service) GetRecentUsage(ctx context.Context, apiKeyID uint, limit int) ([]models.APIKeyUsage, error)
- func (s *Service) GetUsageByAPIKey(ctx context.Context, apiKeyID uint, limit, offset int) ([]models.APIKeyUsage, error)
- func (s *Service) GetUsageByEndpoint(ctx context.Context, apiKeyID uint, startTime, endTime time.Time) (map[string]*models.UsageStats, error)
- func (s *Service) GetUsageByPeriod(ctx context.Context, apiKeyID uint, startDate, endDate time.Time, ...) ([]map[string]any, error)
- func (s *Service) GetUsageStats(ctx context.Context, apiKeyID uint, startDate, endDate time.Time) (*models.UsageStats, error)
- func (s *Service) IncrementBudgetUsed(ctx context.Context, apiKeyID uint, cost float64) error
- func (s *Service) ProcessScheduledBudgetResets(ctx context.Context) error
- func (s *Service) RecordUsage(ctx context.Context, params models.RecordUsageParams) (*models.APIKeyUsage, error)
- func (s *Service) ResetBudget(ctx context.Context, apiKeyID uint) error
- func (s *Service) TrackUsage(ctx context.Context, usage *models.APIKeyUsage) error
- type StripeConfig
- type StripeService
- type Worker
Constants ¶
const ( InputTokenOverhead = 0.10 OutputTokenOverhead = 0.20 )
Variables ¶
var GlobalPricing = map[string]ProviderPricing{
"openai": {
"gpt-5": {
InputTokenCost: 1.25,
OutputTokenCost: 10.0,
},
"gpt-5-pro": {
InputTokenCost: 15.0,
OutputTokenCost: 75.0,
},
"gpt-5-mini": {
InputTokenCost: 0.25,
OutputTokenCost: 2.0,
},
"gpt-5-nano": {
InputTokenCost: 0.05,
OutputTokenCost: 0.4,
},
"gpt-4.1": {
InputTokenCost: 30.0,
OutputTokenCost: 60.0,
},
"gpt-4.1-mini": {
InputTokenCost: 5.0,
OutputTokenCost: 10.0,
},
"gpt-4.1-nano": {
InputTokenCost: 0.5,
OutputTokenCost: 1.0,
},
"gpt-4o": {
InputTokenCost: 2.5,
OutputTokenCost: 10.0,
},
"gpt-4o-mini": {
InputTokenCost: 0.15,
OutputTokenCost: 0.6,
},
"o3": {
InputTokenCost: 60.0,
OutputTokenCost: 240.0,
},
"o3-pro": {
InputTokenCost: 120.0,
OutputTokenCost: 480.0,
},
"o4-mini": {
InputTokenCost: 10.0,
OutputTokenCost: 40.0,
},
},
"anthropic": {
"claude-opus-4.1": {
InputTokenCost: 15.0,
OutputTokenCost: 75.0,
},
"claude-opus-4": {
InputTokenCost: 15.0,
OutputTokenCost: 75.0,
},
"claude-sonnet-4-5-20250929": {
InputTokenCost: 3.0,
OutputTokenCost: 15.0,
},
"claude-sonnet-3.7": {
InputTokenCost: 3.0,
OutputTokenCost: 15.0,
},
"claude-3-5-sonnet-20241022": {
InputTokenCost: 3.0,
OutputTokenCost: 15.0,
},
"claude-3-5-haiku-20241022": {
InputTokenCost: 0.8,
OutputTokenCost: 4.0,
},
},
"gemini": {
"gemini-2.5-pro": {
InputTokenCost: 1.25,
OutputTokenCost: 10.0,
},
"gemini-2.5-flash": {
InputTokenCost: 0.3,
OutputTokenCost: 1.2,
},
"gemini-2.5-flash-lite": {
InputTokenCost: 0.1,
OutputTokenCost: 0.4,
},
"gemini-2.0-flash": {
InputTokenCost: 0.1,
OutputTokenCost: 0.4,
},
"gemini-2.0-flash-live": {
InputTokenCost: 0.15,
OutputTokenCost: 0.6,
},
},
"deepseek": {
"deepseek-chat": {
InputTokenCost: 0.27,
OutputTokenCost: 1.1,
},
"deepseek-reasoner": {
InputTokenCost: 0.55,
OutputTokenCost: 2.19,
},
"deepseek-v3-0324": {
InputTokenCost: 0.27,
OutputTokenCost: 1.1,
},
"deepseek-r1": {
InputTokenCost: 0.55,
OutputTokenCost: 2.19,
},
"deepseek-r1-0528": {
InputTokenCost: 0.75,
OutputTokenCost: 2.99,
},
"deepseek-coder-v2": {
InputTokenCost: 0.27,
OutputTokenCost: 1.1,
},
},
"groq": {
"llama-3.3-70b-versatile": {
InputTokenCost: 0.59,
OutputTokenCost: 0.79,
},
"llama-3.1-8b-instant": {
InputTokenCost: 0.05,
OutputTokenCost: 0.08,
},
"deepseek-r1-distill-llama-70b": {
InputTokenCost: 0.75,
OutputTokenCost: 0.99,
},
"llama-3-groq-70b-tool-use": {
InputTokenCost: 0.59,
OutputTokenCost: 0.79,
},
"llama-3-groq-8b-tool-use": {
InputTokenCost: 0.05,
OutputTokenCost: 0.08,
},
"llama-guard-4-12b": {
InputTokenCost: 0.2,
OutputTokenCost: 0.2,
},
},
"grok": {
"grok-4": {
InputTokenCost: 15.0,
OutputTokenCost: 75.0,
},
"grok-4-heavy": {
InputTokenCost: 25.0,
OutputTokenCost: 125.0,
},
"grok-4-fast": {
InputTokenCost: 5.0,
OutputTokenCost: 25.0,
},
"grok-code-fast-1": {
InputTokenCost: 3.0,
OutputTokenCost: 15.0,
},
"grok-3": {
InputTokenCost: 3.0,
OutputTokenCost: 15.0,
},
"grok-3-mini": {
InputTokenCost: 0.3,
OutputTokenCost: 0.5,
},
},
"huggingface": {
"meta-llama/Llama-3.3-70B-Instruct": {
InputTokenCost: 0.05,
OutputTokenCost: 0.08,
},
"meta-llama/Llama-3.1-8B-Instruct": {
InputTokenCost: 0.01,
OutputTokenCost: 0.02,
},
"deepseek-ai/DeepSeek-R1-Distill-Qwen-14B": {
InputTokenCost: 0.02,
OutputTokenCost: 0.04,
},
"deepseek-ai/DeepSeek-R1-Distill-Llama-8B": {
InputTokenCost: 0.01,
OutputTokenCost: 0.02,
},
"Qwen/Qwen3-235B-A22B": {
InputTokenCost: 0.1,
OutputTokenCost: 0.2,
},
"Qwen/Qwen3-30B-A3B": {
InputTokenCost: 0.02,
OutputTokenCost: 0.04,
},
},
"zai": {
"glm-4.6": {
InputTokenCost: 0.6,
OutputTokenCost: 2.2,
},
"glm-4.5": {
InputTokenCost: 0.6,
OutputTokenCost: 2.2,
},
"glm-4.5v": {
InputTokenCost: 0.6,
OutputTokenCost: 1.8,
},
"glm-4.5-x": {
InputTokenCost: 2.2,
OutputTokenCost: 8.9,
},
"glm-4.5-air": {
InputTokenCost: 0.2,
OutputTokenCost: 1.1,
},
"glm-4.5-airx": {
InputTokenCost: 1.1,
OutputTokenCost: 4.5,
},
"glm-4-32b-0414-128k": {
InputTokenCost: 0.1,
OutputTokenCost: 0.1,
},
"glm-4.5-flash": {
InputTokenCost: 0.0,
OutputTokenCost: 0.0,
},
},
}
Functions ¶
func CalculateCost ¶
func DefaultAPIKeyConfig ¶
func DefaultAPIKeyConfig() models.APIKeyConfig
func ExtractKeyPrefix ¶
func GenerateAPIKey ¶
func HashAPIKey ¶
Types ¶
type APIKeyService ¶
type APIKeyService struct {
// contains filtered or unexported fields
}
func NewAPIKeyService ¶
func NewAPIKeyService(db *gorm.DB) *APIKeyService
func (*APIKeyService) AutoMigrate ¶
func (s *APIKeyService) AutoMigrate() error
func (*APIKeyService) CreateAPIKey ¶
func (s *APIKeyService) CreateAPIKey(ctx context.Context, req *models.APIKeyCreateRequest) (*models.APIKeyResponse, error)
func (*APIKeyService) DeleteAPIKey ¶
func (s *APIKeyService) DeleteAPIKey(ctx context.Context, id uint) error
func (*APIKeyService) GetAPIKey ¶
func (s *APIKeyService) GetAPIKey(ctx context.Context, id uint) (*models.APIKeyResponse, error)
func (*APIKeyService) ListAPIKeys ¶
func (s *APIKeyService) ListAPIKeys(ctx context.Context, limit, offset int) ([]models.APIKeyResponse, int64, error)
func (*APIKeyService) ListAPIKeysByProjectID ¶
func (s *APIKeyService) ListAPIKeysByProjectID(ctx context.Context, projectID uint, limit, offset int) ([]models.APIKeyResponse, int64, error)
func (*APIKeyService) ListAPIKeysByUserID ¶
func (s *APIKeyService) ListAPIKeysByUserID(ctx context.Context, userID string, limit, offset int) ([]models.APIKeyResponse, int64, error)
func (*APIKeyService) RevokeAPIKey ¶
func (s *APIKeyService) RevokeAPIKey(ctx context.Context, id uint) error
func (*APIKeyService) UpdateAPIKey ¶
func (*APIKeyService) ValidateAPIKey ¶
type BudgetResetScheduler ¶
type BudgetResetScheduler struct {
// contains filtered or unexported fields
}
func NewBudgetResetScheduler ¶
func NewBudgetResetScheduler(usageService *Service, interval time.Duration) *BudgetResetScheduler
func (*BudgetResetScheduler) Start ¶
func (s *BudgetResetScheduler) Start(ctx context.Context)
func (*BudgetResetScheduler) Stop ¶
func (s *BudgetResetScheduler) Stop()
type CreateCheckoutParams ¶
type CreateCheckoutParams struct {
OrganizationID string
UserID string
StripePriceID string
CreditAmount float64
SuccessURL string
CancelURL string
CustomerEmail string
}
CreateCheckoutParams contains parameters for creating a checkout session
type CreditsService ¶
type CreditsService struct {
// contains filtered or unexported fields
}
func NewCreditsService ¶
func NewCreditsService(db *gorm.DB) *CreditsService
func (*CreditsService) AddCredits ¶
func (s *CreditsService) AddCredits(ctx context.Context, params models.AddCreditsParams) (*models.CreditTransaction, error)
AddCredits adds credits to an organization's balance
func (*CreditsService) AutoMigrate ¶
func (s *CreditsService) AutoMigrate() error
AutoMigrate runs database migrations for credit tables
func (*CreditsService) CheckSufficientCredits ¶
func (s *CreditsService) CheckSufficientCredits(ctx context.Context, organizationID string, amount float64) error
CheckSufficientCredits verifies if an organization has enough credits
func (*CreditsService) DeductCredits ¶
func (s *CreditsService) DeductCredits(ctx context.Context, params models.DeductCreditsParams) (*models.CreditTransaction, error)
DeductCredits deducts credits from an organization's balance Creates a transaction record and updates the balance atomically
func (*CreditsService) GetOrganizationCredit ¶
func (s *CreditsService) GetOrganizationCredit(ctx context.Context, organizationID string) (*models.OrganizationCredit, error)
GetOrganizationCredit retrieves the credit record for an organization Creates one if it doesn't exist
func (*CreditsService) GetTransactionHistory ¶
func (s *CreditsService) GetTransactionHistory(ctx context.Context, organizationID string, limit, offset int) ([]models.CreditTransaction, error)
GetTransactionHistory retrieves transaction history for an organization
type ModelPricing ¶
type ProviderPricing ¶
type ProviderPricing map[string]ModelPricing
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
func NewRateLimiter ¶
func NewRateLimiter() *RateLimiter
func (*RateLimiter) CheckRateLimit ¶
type RecordTask ¶
type RecordTask struct {
Params models.RecordUsageParams
RequestID string
}
RecordTask represents a usage recording task
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
func NewService ¶
func NewService(db *gorm.DB, creditsService *CreditsService) *Service
func (*Service) AutoMigrate ¶
func (*Service) CheckBudgetLimit ¶
func (*Service) GetRecentUsage ¶
func (*Service) GetUsageByAPIKey ¶
func (*Service) GetUsageByEndpoint ¶
func (*Service) GetUsageByPeriod ¶
func (*Service) GetUsageStats ¶
func (*Service) IncrementBudgetUsed ¶
func (*Service) ProcessScheduledBudgetResets ¶
func (*Service) RecordUsage ¶
func (s *Service) RecordUsage(ctx context.Context, params models.RecordUsageParams) (*models.APIKeyUsage, error)
func (*Service) ResetBudget ¶
func (*Service) TrackUsage ¶
type StripeConfig ¶
type StripeService ¶
type StripeService struct {
// contains filtered or unexported fields
}
func NewStripeService ¶
func NewStripeService(cfg StripeConfig, creditsService *CreditsService) *StripeService
func (*StripeService) CreateCheckoutSession ¶
func (s *StripeService) CreateCheckoutSession(ctx context.Context, params CreateCheckoutParams) (*stripe.CheckoutSession, error)
CreateCheckoutSession creates a Stripe checkout session for purchasing credits
func (*StripeService) HandleWebhook ¶
HandleWebhook processes Stripe webhook events