-
Notifications
You must be signed in to change notification settings - Fork 223
feat: real-time token cost calculation and subscription ROI dashboard #148
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
jackrescuer-gif
wants to merge
6
commits into
matt1398:main
Choose a base branch
from
jackrescuer-gif:feat/subscription-roi
base: main
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
586241d
feat: add token-to-USD pricing model and fix cost calculation
2b51b7f
feat: add subscription payment tracking to Settings
a9fc997
feat: add subscription ROI block to dashboard
3eef28c
test: add tests for pricingModel and subscriptions config validation
c75edf4
fix: resolve all lint errors ahead of PR
403c03a
refactor: address Gemini code review feedback
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,135 @@ | ||
| /** | ||
| * Claude API pricing model. | ||
| * | ||
| * Provides token-to-USD cost calculation for all Claude model families. | ||
| * Prices are in USD per 1,000,000 tokens. | ||
| * | ||
| * Source: https://www.anthropic.com/api | ||
| */ | ||
|
|
||
| export interface ModelPricing { | ||
| /** USD per 1M input tokens */ | ||
| inputPerMillion: number; | ||
| /** USD per 1M output tokens */ | ||
| outputPerMillion: number; | ||
| /** USD per 1M cache creation tokens (prompt caching write) */ | ||
| cacheWritePerMillion: number; | ||
| /** USD per 1M cache read tokens (prompt caching read) */ | ||
| cacheReadPerMillion: number; | ||
| } | ||
|
|
||
| /** | ||
| * Pricing table: [model-string-prefix, pricing]. | ||
| * Entries are ordered from most-specific to least-specific. | ||
| * Matching is done via startsWith or includes on the lowercased model string. | ||
| */ | ||
| const PRICING_TABLE: [string, ModelPricing][] = [ | ||
| // ── Claude 4 ────────────────────────────────────────────────────────── | ||
| [ | ||
| 'claude-opus-4', | ||
| { inputPerMillion: 15.0, outputPerMillion: 75.0, cacheWritePerMillion: 18.75, cacheReadPerMillion: 1.5 }, | ||
| ], | ||
| [ | ||
| 'claude-sonnet-4', | ||
| { inputPerMillion: 3.0, outputPerMillion: 15.0, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.3 }, | ||
| ], | ||
| [ | ||
| 'claude-haiku-4', | ||
| { inputPerMillion: 0.8, outputPerMillion: 4.0, cacheWritePerMillion: 1.0, cacheReadPerMillion: 0.08 }, | ||
| ], | ||
|
|
||
| // ── Claude 3.7 ──────────────────────────────────────────────────────── | ||
| [ | ||
| 'claude-3-7-sonnet', | ||
| { inputPerMillion: 3.0, outputPerMillion: 15.0, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.3 }, | ||
| ], | ||
|
|
||
| // ── Claude 3.5 ──────────────────────────────────────────────────────── | ||
| [ | ||
| 'claude-3-5-sonnet', | ||
| { inputPerMillion: 3.0, outputPerMillion: 15.0, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.3 }, | ||
| ], | ||
| [ | ||
| 'claude-3-5-haiku', | ||
| { inputPerMillion: 0.8, outputPerMillion: 4.0, cacheWritePerMillion: 1.0, cacheReadPerMillion: 0.08 }, | ||
| ], | ||
|
|
||
| // ── Claude 3 ────────────────────────────────────────────────────────── | ||
| [ | ||
| 'claude-3-opus', | ||
| { inputPerMillion: 15.0, outputPerMillion: 75.0, cacheWritePerMillion: 18.75, cacheReadPerMillion: 1.5 }, | ||
| ], | ||
| [ | ||
| 'claude-3-sonnet', | ||
| { inputPerMillion: 3.0, outputPerMillion: 15.0, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.3 }, | ||
| ], | ||
| [ | ||
| 'claude-3-haiku', | ||
| { inputPerMillion: 0.25, outputPerMillion: 1.25, cacheWritePerMillion: 0.3, cacheReadPerMillion: 0.03 }, | ||
| ], | ||
|
|
||
| // ── Claude 2 ────────────────────────────────────────────────────────── | ||
| [ | ||
| 'claude-2', | ||
| { inputPerMillion: 8.0, outputPerMillion: 24.0, cacheWritePerMillion: 8.0, cacheReadPerMillion: 8.0 }, | ||
| ], | ||
| [ | ||
| 'claude-instant', | ||
| { inputPerMillion: 1.63, outputPerMillion: 5.51, cacheWritePerMillion: 1.63, cacheReadPerMillion: 1.63 }, | ||
| ], | ||
| ]; | ||
|
|
||
| /** | ||
| * Fallback pricing used for unknown or future models. | ||
| * Uses Claude Sonnet 4 pricing as a reasonable mid-tier estimate. | ||
| */ | ||
| export const FALLBACK_PRICING: ModelPricing = { | ||
| inputPerMillion: 3.0, | ||
| outputPerMillion: 15.0, | ||
| cacheWritePerMillion: 3.75, | ||
| cacheReadPerMillion: 0.3, | ||
| }; | ||
|
|
||
| /** | ||
| * Returns pricing for a given model string. | ||
| * Matching is prefix-based and case-insensitive. | ||
| * Falls back to Sonnet-tier pricing for unrecognized models. | ||
| */ | ||
| export function getPricingForModel(model: string | null | undefined): ModelPricing { | ||
| if (!model) return FALLBACK_PRICING; | ||
|
|
||
| const lower = model.toLowerCase(); | ||
| for (const [prefix, pricing] of PRICING_TABLE) { | ||
| if (lower.startsWith(prefix) || lower.includes(prefix)) { | ||
| return pricing; | ||
| } | ||
| } | ||
|
|
||
| return FALLBACK_PRICING; | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the USD cost for a single API response given token counts and model. | ||
| * | ||
| * @param model Model string (e.g. "claude-sonnet-4-5-20251022") | ||
| * @param inputTokens Prompt / input tokens | ||
| * @param outputTokens Completion / output tokens | ||
| * @param cacheReadTokens Tokens retrieved from prompt cache | ||
| * @param cacheCreationTokens Tokens written to prompt cache | ||
| * @returns Cost in USD | ||
| */ | ||
| export function calculateTokenCost( | ||
| model: string | null | undefined, | ||
| inputTokens: number, | ||
| outputTokens: number, | ||
| cacheReadTokens = 0, | ||
| cacheCreationTokens = 0 | ||
| ): number { | ||
| const p = getPricingForModel(model); | ||
| return ( | ||
| (inputTokens / 1_000_000) * p.inputPerMillion + | ||
| (outputTokens / 1_000_000) * p.outputPerMillion + | ||
| (cacheReadTokens / 1_000_000) * p.cacheReadPerMillion + | ||
| (cacheCreationTokens / 1_000_000) * p.cacheWritePerMillion | ||
| ); | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
What are the current token pricing rates for Claude API models including Claude 4, Claude 3.5, and Claude 3?💡 Result:
Current Claude API token pricing rates (per million tokens, USD, as of March 2026) from official Anthropic documentation: Claude 4 models (latest flagship family): - Claude Opus 4.6: Input $5, Output $25 - Claude Sonnet 4.6: Input $3, Output $15 - Claude Opus 4.5: Input $5, Output $25 - Claude Sonnet 4.5: Input $3, Output $15 - Claude Haiku 4.5: Input $1, Output $5 Claude 3.5 models: - Claude Haiku 3.5: Input $0.80, Output $4 Claude 3 models (legacy/deprecated): - Claude Opus 3: Input $15, Output $75 - Claude Sonnet 3.7: Input $3, Output $15 (deprecated) - Claude Haiku 3: Input $0.25, Output $1.25 Notes: Prices are for base input/output tokens. Prompt caching, batch processing (50% discount), and long-context may modify rates. Claude 4 represents the current production models; Claude 3/3.5 are older generations still available but some deprecated. No specific "Claude 3.5 Sonnet" or "Claude 3 Opus/Sonnet" listed separately in current docs—likely rolled into 4.x or legacy 3.7/3.
Citations:
Update PRICING_TABLE to reflect current Anthropic API pricing (as of March 2026).
The hardcoded pricing values are outdated and will cause inaccurate cost calculations:
claude-opus-4: Input/Output should be $5/$25 (not $15/$75)claude-haiku-4: Input/Output should be $1/$5 (not $0.8/$4)Reference current pricing from Anthropic documentation.
Regarding Claude 2/Instant cache rates matching input rates—if intentionally supporting legacy models without caching, add an inline comment explaining this is by design.
🤖 Prompt for AI Agents