-
Notifications
You must be signed in to change notification settings - Fork 65
feat: user cost #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wjiayis
wants to merge
13
commits into
staging
Choose a base branch
from
feat/user-cost
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: user cost #126
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d93462e
feat: LLM usage tracking, backend
wjiayis 3a73435
feat: LLM usage tracking, frontend
wjiayis c882e6e
feat: simplify frontend display
wjiayis baeeaa7
feat: refresh handling
wjiayis 27092ae
chore: update comments
wjiayis 3c6a7f2
chore: clarify comments
wjiayis 4660a29
feat: add usage_test.go
wjiayis 92a99ad
feat: improve llm_sessions compound index
wjiayis 736871b
chore: improve race condition error handling
wjiayis ea8782f
feat: break down to per-model usage
wjiayis 18402b2
feat: display USD token prices
wjiayis 219396f
feat: improve display of usage costs
wjiayis dc78d5b
fix: only track usage for non-BYOK
wjiayis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package usage | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "paperdebugger/internal/libs/contextutil" | ||
| "paperdebugger/internal/models" | ||
| usagev1 "paperdebugger/pkg/gen/api/usage/v1" | ||
|
|
||
| "google.golang.org/protobuf/types/known/timestamppb" | ||
| ) | ||
|
|
||
| func (s *UsageServer) GetSessionUsage( | ||
| ctx context.Context, | ||
| req *usagev1.GetSessionUsageRequest, | ||
| ) (*usagev1.GetSessionUsageResponse, error) { | ||
| actor, err := contextutil.GetActor(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| session, err := s.usageService.GetActiveSession(ctx, actor.ID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if session == nil { | ||
| return &usagev1.GetSessionUsageResponse{ | ||
| Session: nil, | ||
| }, nil | ||
| } | ||
|
|
||
| // Get pricing map for cost calculation | ||
| pricingMap, err := s.pricingService.GetPricingMap(ctx) | ||
| if err != nil { | ||
| s.logger.Warn("Failed to get pricing map", "error", err) | ||
| pricingMap = make(map[string]*models.ModelPricing) | ||
| } | ||
|
|
||
| // Convert models map to proto format and calculate costs | ||
| protoModels := make(map[string]*usagev1.ModelTokens) | ||
| var totalCostUSD float64 | ||
| for modelName, tokens := range session.Models { | ||
| var costUSD float64 | ||
| if pricing, ok := pricingMap[modelName]; ok && pricing != nil { | ||
| costUSD = float64(tokens.PromptTokens)*pricing.PromptPrice + | ||
| float64(tokens.CompletionTokens)*pricing.CompletionPrice | ||
| totalCostUSD += costUSD | ||
| } | ||
| protoModels[modelName] = &usagev1.ModelTokens{ | ||
| PromptTokens: tokens.PromptTokens, | ||
| CompletionTokens: tokens.CompletionTokens, | ||
| TotalTokens: tokens.TotalTokens, | ||
| RequestCount: tokens.RequestCount, | ||
| CostUsd: costUSD, | ||
| } | ||
| } | ||
|
|
||
| return &usagev1.GetSessionUsageResponse{ | ||
| Session: &usagev1.SessionUsage{ | ||
| SessionExpiry: timestamppb.New(session.SessionExpiry.Time()), | ||
| Models: protoModels, | ||
| TotalCostUsd: totalCostUSD, | ||
| }, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package usage | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "paperdebugger/internal/libs/contextutil" | ||
| "paperdebugger/internal/models" | ||
| usagev1 "paperdebugger/pkg/gen/api/usage/v1" | ||
| ) | ||
|
|
||
| func (s *UsageServer) GetWeeklyUsage( | ||
| ctx context.Context, | ||
| req *usagev1.GetWeeklyUsageRequest, | ||
| ) (*usagev1.GetWeeklyUsageResponse, error) { | ||
| actor, err := contextutil.GetActor(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| stats, err := s.usageService.GetWeeklyUsage(ctx, actor.ID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Get pricing map for cost calculation | ||
| pricingMap, err := s.pricingService.GetPricingMap(ctx) | ||
| if err != nil { | ||
| s.logger.Warn("Failed to get pricing map", "error", err) | ||
| pricingMap = make(map[string]*models.ModelPricing) | ||
| } | ||
|
|
||
| // Convert models map to proto format and calculate costs | ||
| protoModels := make(map[string]*usagev1.ModelTokens) | ||
| var totalCostUSD float64 | ||
| for modelName, tokens := range stats.Models { | ||
| var costUSD float64 | ||
| if pricing, ok := pricingMap[modelName]; ok && pricing != nil { | ||
| costUSD = float64(tokens.PromptTokens)*pricing.PromptPrice + | ||
| float64(tokens.CompletionTokens)*pricing.CompletionPrice | ||
| totalCostUSD += costUSD | ||
| } | ||
| protoModels[modelName] = &usagev1.ModelTokens{ | ||
| PromptTokens: tokens.PromptTokens, | ||
| CompletionTokens: tokens.CompletionTokens, | ||
| TotalTokens: tokens.TotalTokens, | ||
| RequestCount: tokens.RequestCount, | ||
| CostUsd: costUSD, | ||
| } | ||
| } | ||
|
|
||
| return &usagev1.GetWeeklyUsageResponse{ | ||
| Usage: &usagev1.WeeklyUsage{ | ||
| Models: protoModels, | ||
| SessionCount: stats.SessionCount, | ||
| TotalCostUsd: totalCostUSD, | ||
| }, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package usage | ||
|
|
||
| import ( | ||
| "paperdebugger/internal/libs/logger" | ||
| "paperdebugger/internal/services" | ||
| usagev1 "paperdebugger/pkg/gen/api/usage/v1" | ||
| ) | ||
|
|
||
| type UsageServer struct { | ||
| usagev1.UnimplementedUsageServiceServer | ||
|
|
||
| usageService *services.UsageService | ||
| pricingService *services.PricingService | ||
| logger *logger.Logger | ||
| } | ||
|
|
||
| func NewUsageServer( | ||
| usageService *services.UsageService, | ||
| pricingService *services.PricingService, | ||
| logger *logger.Logger, | ||
| ) usagev1.UsageServiceServer { | ||
| return &UsageServer{ | ||
| usageService: usageService, | ||
| pricingService: pricingService, | ||
| logger: logger, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package models | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "go.mongodb.org/mongo-driver/v2/bson" | ||
| ) | ||
|
|
||
| // ModelPricing stores the pricing information for an LLM model. | ||
| // Prices are in USD per token. | ||
| type ModelPricing struct { | ||
| ID bson.ObjectID `bson:"_id"` | ||
| ModelID string `bson:"model_id"` // e.g., "openai/gpt-4" | ||
| ModelSlug string `bson:"model_slug"` // e.g., "gpt-4" (short name used in our app) | ||
| Name string `bson:"name"` // e.g., "OpenAI: GPT-4" | ||
| PromptPrice float64 `bson:"prompt_price"` // USD per token | ||
| CompletionPrice float64 `bson:"completion_price"` // USD per token | ||
| UpdatedAt time.Time `bson:"updated_at"` | ||
| } | ||
|
|
||
| func (m ModelPricing) CollectionName() string { | ||
| return "model_pricing" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package models | ||
|
|
||
| import "go.mongodb.org/mongo-driver/v2/bson" | ||
|
|
||
| // ModelTokens stores token counts for a specific model. | ||
| type ModelTokens struct { | ||
| PromptTokens int64 `bson:"prompt_tokens"` | ||
| CompletionTokens int64 `bson:"completion_tokens"` | ||
| TotalTokens int64 `bson:"total_tokens"` | ||
| RequestCount int64 `bson:"request_count"` | ||
| } | ||
|
|
||
| // LLMSession represents a user's session for tracking LLM usage and token counts. | ||
| // Tokens are stored per model in the Models map. | ||
| type LLMSession struct { | ||
| ID bson.ObjectID `bson:"_id"` | ||
| UserID bson.ObjectID `bson:"user_id"` | ||
| SessionStart bson.DateTime `bson:"session_start"` | ||
| SessionExpiry bson.DateTime `bson:"session_expiry"` | ||
| Models map[string]*ModelTokens `bson:"models"` | ||
| } | ||
|
|
||
| func (s LLMSession) CollectionName() string { | ||
| return "llm_sessions" | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.