usage

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InputTokenOverhead  = 0.10
	OutputTokenOverhead = 0.20
)

Variables

View Source
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 CalculateBudgetRemaining

func CalculateBudgetRemaining(budgetLimit, budgetUsed float64) float64

func CalculateCost

func CalculateCost(provider, model string, inputTokens, outputTokens int, isCustomConfig bool) float64

func DefaultAPIKeyConfig

func DefaultAPIKeyConfig() models.APIKeyConfig

func ExtractKeyPrefix

func ExtractKeyPrefix(key string) string

func GenerateAPIKey

func GenerateAPIKey() (string, error)

func HashAPIKey

func HashAPIKey(key string) string

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 (*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) GetByHash

func (s *APIKeyService) GetByHash(ctx context.Context, keyHash string) (*models.APIKey, 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 (s *APIKeyService) UpdateAPIKey(ctx context.Context, id uint, updates map[string]any) error

func (*APIKeyService) ValidateAPIKey

func (s *APIKeyService) ValidateAPIKey(ctx context.Context, key string) (*models.APIKey, error)

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

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

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 ModelPricing struct {
	InputTokenCost  float64
	OutputTokenCost float64
}

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

func (rl *RateLimiter) CheckRateLimit(ctx context.Context, apiKeyID uint, limitRpm int) (bool, error)

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 (s *Service) AutoMigrate() error

func (*Service) CheckBudgetLimit

func (s *Service) CheckBudgetLimit(ctx context.Context, apiKeyID uint) (bool, *models.APIKey, error)

func (*Service) GetRecentUsage

func (s *Service) GetRecentUsage(ctx context.Context, apiKeyID uint, limit int) ([]models.APIKeyUsage, error)

func (*Service) GetUsageByAPIKey

func (s *Service) GetUsageByAPIKey(ctx context.Context, apiKeyID uint, limit, offset int) ([]models.APIKeyUsage, error)

func (*Service) GetUsageByEndpoint

func (s *Service) GetUsageByEndpoint(ctx context.Context, apiKeyID uint, startTime, endTime time.Time) (map[string]*models.UsageStats, error)

func (*Service) GetUsageByPeriod

func (s *Service) GetUsageByPeriod(ctx context.Context, apiKeyID uint, startDate, endDate time.Time, groupBy string) ([]map[string]any, error)

func (*Service) GetUsageStats

func (s *Service) GetUsageStats(ctx context.Context, apiKeyID uint, startDate, endDate time.Time) (*models.UsageStats, error)

func (*Service) IncrementBudgetUsed

func (s *Service) IncrementBudgetUsed(ctx context.Context, apiKeyID uint, cost float64) error

func (*Service) ProcessScheduledBudgetResets

func (s *Service) ProcessScheduledBudgetResets(ctx context.Context) error

func (*Service) RecordUsage

func (s *Service) RecordUsage(ctx context.Context, params models.RecordUsageParams) (*models.APIKeyUsage, error)

func (*Service) ResetBudget

func (s *Service) ResetBudget(ctx context.Context, apiKeyID uint) error

func (*Service) TrackUsage

func (s *Service) TrackUsage(ctx context.Context, usage *models.APIKeyUsage) error

type StripeConfig

type StripeConfig struct {
	SecretKey     string
	WebhookSecret string
}

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

func (s *StripeService) HandleWebhook(ctx context.Context, payload []byte, signature string) error

HandleWebhook processes Stripe webhook events

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

Worker represents a usage recording worker that processes recording tasks

func NewWorker

func NewWorker(service *Service, poolSize, bufferSize int) *Worker

NewWorker creates a new usage recording worker with the specified pool size

func (*Worker) Stop

func (w *Worker) Stop()

Stop gracefully stops the worker pool

func (*Worker) Submit

func (w *Worker) Submit(params models.RecordUsageParams, requestID string)

Submit submits a usage recording task to the worker pool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL