Nyk commited on
Commit
a744127
·
1 Parent(s): 200c64b

fix: correct token costing and add provider subscription detection

Browse files
src/app/api/status/route.ts CHANGED
@@ -1,6 +1,6 @@
1
  import { NextRequest, NextResponse } from 'next/server'
2
  import net from 'node:net'
3
- import { existsSync, readFileSync, statSync } from 'node:fs'
4
  import path from 'node:path'
5
  import { runCommand, runOpenClaw, runClawdbot } from '@/lib/command'
6
  import { config } from '@/lib/config'
@@ -9,6 +9,7 @@ import { getAllGatewaySessions, getAgentLiveStatuses } from '@/lib/sessions'
9
  import { requireRole } from '@/lib/auth'
10
  import { MODEL_CATALOG } from '@/lib/models'
11
  import { logger } from '@/lib/logger'
 
12
 
13
  export async function GET(request: NextRequest) {
14
  const auth = requireRole(request, 'viewer')
@@ -494,25 +495,14 @@ async function getCapabilities() {
494
  // claude_sessions table may not exist
495
  }
496
 
497
- // Detect Claude subscription type from credentials
498
- let subscription: { type: string; rateLimitTier?: string } | null = null
499
- try {
500
- const credsPath = path.join(config.claudeHome, '.credentials.json')
501
- if (existsSync(credsPath)) {
502
- const creds = JSON.parse(readFileSync(credsPath, 'utf-8'))
503
- const oauth = creds.claudeAiOauth
504
- if (oauth?.subscriptionType) {
505
- subscription = {
506
- type: oauth.subscriptionType,
507
- rateLimitTier: oauth.rateLimitTier || undefined,
508
- }
509
- }
510
- }
511
- } catch {
512
- // credentials file may not exist or be unreadable
513
- }
514
 
515
- return { gateway, openclawHome, claudeHome, claudeSessions, subscription }
516
  }
517
 
518
  function isPortOpen(host: string, port: number): Promise<boolean> {
 
1
  import { NextRequest, NextResponse } from 'next/server'
2
  import net from 'node:net'
3
+ import { existsSync, statSync } from 'node:fs'
4
  import path from 'node:path'
5
  import { runCommand, runOpenClaw, runClawdbot } from '@/lib/command'
6
  import { config } from '@/lib/config'
 
9
  import { requireRole } from '@/lib/auth'
10
  import { MODEL_CATALOG } from '@/lib/models'
11
  import { logger } from '@/lib/logger'
12
+ import { detectProviderSubscriptions, getPrimarySubscription } from '@/lib/provider-subscriptions'
13
 
14
  export async function GET(request: NextRequest) {
15
  const auth = requireRole(request, 'viewer')
 
495
  // claude_sessions table may not exist
496
  }
497
 
498
+ const subscriptions = detectProviderSubscriptions().active
499
+ const primary = getPrimarySubscription()
500
+ const subscription = primary ? {
501
+ type: primary.type,
502
+ provider: primary.provider,
503
+ } : null
 
 
 
 
 
 
 
 
 
 
 
504
 
505
+ return { gateway, openclawHome, claudeHome, claudeSessions, subscription, subscriptions }
506
  }
507
 
508
  function isPortOpen(host: string, port: number): Promise<boolean> {
src/app/api/tokens/route.ts CHANGED
@@ -6,6 +6,8 @@ import { requireRole } from '@/lib/auth'
6
  import { getAllGatewaySessions } from '@/lib/sessions'
7
  import { logger } from '@/lib/logger'
8
  import { getDatabase } from '@/lib/db'
 
 
9
 
10
  const DATA_PATH = config.tokensPath
11
 
@@ -38,23 +40,6 @@ interface ExportData {
38
  sessions: Record<string, TokenStats>
39
  }
40
 
41
- // Model pricing (cost per 1K tokens)
42
- const MODEL_PRICING: Record<string, number> = {
43
- 'anthropic/claude-3-5-haiku-latest': 0.25,
44
- 'claude-3-5-haiku': 0.25,
45
- 'anthropic/claude-sonnet-4-20250514': 3.0,
46
- 'claude-sonnet-4': 3.0,
47
- 'anthropic/claude-opus-4-5': 15.0,
48
- 'claude-opus-4-5': 15.0,
49
- 'groq/llama-3.1-8b-instant': 0.05,
50
- 'groq/llama-3.3-70b-versatile': 0.59,
51
- 'moonshot/kimi-k2.5': 1.0,
52
- 'minimax/minimax-m2.1': 0.3,
53
- 'ollama/deepseek-r1:14b': 0.0,
54
- 'ollama/qwen2.5-coder:7b': 0.0,
55
- 'ollama/qwen2.5-coder:14b': 0.0,
56
- }
57
-
58
  function extractAgentName(sessionId: string): string {
59
  const trimmed = sessionId.trim()
60
  if (!trimmed) return 'unknown'
@@ -62,14 +47,6 @@ function extractAgentName(sessionId: string): string {
62
  return agent?.trim() || 'unknown'
63
  }
64
 
65
- function getModelCost(modelName: string): number {
66
- if (MODEL_PRICING[modelName] !== undefined) return MODEL_PRICING[modelName]
67
- for (const [model, cost] of Object.entries(MODEL_PRICING)) {
68
- if (modelName.includes(model.split('/').pop() || '')) return cost
69
- }
70
- return 1.0
71
- }
72
-
73
  interface DbTokenUsageRow {
74
  id: number
75
  model: string
@@ -79,7 +56,7 @@ interface DbTokenUsageRow {
79
  created_at: number
80
  }
81
 
82
- function loadTokenDataFromDb(): TokenUsageRecord[] {
83
  try {
84
  const db = getDatabase()
85
  const rows = db.prepare(`
@@ -91,7 +68,6 @@ function loadTokenDataFromDb(): TokenUsageRecord[] {
91
 
92
  return rows.map((row) => {
93
  const totalTokens = row.input_tokens + row.output_tokens
94
- const costPer1k = getModelCost(row.model)
95
  return {
96
  id: `db-${row.id}`,
97
  model: row.model,
@@ -101,7 +77,7 @@ function loadTokenDataFromDb(): TokenUsageRecord[] {
101
  inputTokens: row.input_tokens,
102
  outputTokens: row.output_tokens,
103
  totalTokens,
104
- cost: (totalTokens / 1000) * costPer1k,
105
  operation: 'heartbeat',
106
  }
107
  })
@@ -111,7 +87,10 @@ function loadTokenDataFromDb(): TokenUsageRecord[] {
111
  }
112
  }
113
 
114
- function normalizeTokenRecord(record: Partial<TokenUsageRecord>): TokenUsageRecord | null {
 
 
 
115
  if (!record.model || !record.sessionId) return null
116
  const inputTokens = Number(record.inputTokens ?? 0)
117
  const outputTokens = Number(record.outputTokens ?? 0)
@@ -126,7 +105,7 @@ function normalizeTokenRecord(record: Partial<TokenUsageRecord>): TokenUsageReco
126
  inputTokens,
127
  outputTokens,
128
  totalTokens,
129
- cost: Number(record.cost ?? (totalTokens / 1000) * getModelCost(model)),
130
  operation: String(record.operation ?? 'chat_completion'),
131
  duration: record.duration,
132
  }
@@ -155,7 +134,7 @@ function dedupeTokenRecords(records: TokenUsageRecord[]): TokenUsageRecord[] {
155
  return deduped
156
  }
157
 
158
- async function loadTokenDataFromFile(): Promise<TokenUsageRecord[]> {
159
  try {
160
  ensureDirExists(dirname(DATA_PATH))
161
  await access(DATA_PATH)
@@ -164,7 +143,7 @@ async function loadTokenDataFromFile(): Promise<TokenUsageRecord[]> {
164
  if (!Array.isArray(parsed)) return []
165
 
166
  return parsed
167
- .map((record: Partial<TokenUsageRecord>) => normalizeTokenRecord(record))
168
  .filter((record): record is TokenUsageRecord => record !== null)
169
  } catch {
170
  return []
@@ -175,33 +154,32 @@ async function loadTokenDataFromFile(): Promise<TokenUsageRecord[]> {
175
  * Load token data from persistent file, falling back to deriving from session stores.
176
  */
177
  async function loadTokenData(): Promise<TokenUsageRecord[]> {
178
- const dbRecords = loadTokenDataFromDb()
179
- const fileRecords = await loadTokenDataFromFile()
 
180
  const combined = dedupeTokenRecords([...dbRecords, ...fileRecords]).sort((a, b) => b.timestamp - a.timestamp)
181
  if (combined.length > 0) {
182
  return combined
183
  }
184
 
185
  // Final fallback: derive from in-memory sessions
186
- return deriveFromSessions()
187
  }
188
 
189
  /**
190
  * Derive token usage records from OpenClaw session stores.
191
  * Each session has totalTokens, inputTokens, outputTokens, model, etc.
192
  */
193
- function deriveFromSessions(): TokenUsageRecord[] {
194
  const sessions = getAllGatewaySessions(Infinity) // Get ALL sessions regardless of age
195
  const records: TokenUsageRecord[] = []
196
 
197
  for (const session of sessions) {
198
- if (!session.totalTokens && !session.model) continue // Skip empty sessions
199
-
200
- const totalTokens = session.totalTokens || 0
201
- const inputTokens = session.inputTokens || Math.round(totalTokens * 0.7)
202
- const outputTokens = session.outputTokens || totalTokens - inputTokens
203
- const costPer1k = getModelCost(session.model || '')
204
- const cost = (totalTokens / 1000) * costPer1k
205
 
206
  records.push({
207
  id: `session-${session.agent}-${session.key}`,
@@ -510,8 +488,8 @@ export async function POST(request: NextRequest) {
510
  }
511
 
512
  const totalTokens = inputTokens + outputTokens
513
- const costPer1k = getModelCost(model)
514
- const cost = (totalTokens / 1000) * costPer1k
515
 
516
  const record: TokenUsageRecord = {
517
  id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
@@ -528,7 +506,7 @@ export async function POST(request: NextRequest) {
528
  }
529
 
530
  // Persist only manually posted usage records in the JSON file.
531
- const existingData = await loadTokenDataFromFile()
532
  existingData.unshift(record)
533
 
534
  if (existingData.length > 10000) {
 
6
  import { getAllGatewaySessions } from '@/lib/sessions'
7
  import { logger } from '@/lib/logger'
8
  import { getDatabase } from '@/lib/db'
9
+ import { calculateTokenCost } from '@/lib/token-pricing'
10
+ import { getProviderSubscriptionFlags } from '@/lib/provider-subscriptions'
11
 
12
  const DATA_PATH = config.tokensPath
13
 
 
40
  sessions: Record<string, TokenStats>
41
  }
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  function extractAgentName(sessionId: string): string {
44
  const trimmed = sessionId.trim()
45
  if (!trimmed) return 'unknown'
 
47
  return agent?.trim() || 'unknown'
48
  }
49
 
 
 
 
 
 
 
 
 
50
  interface DbTokenUsageRow {
51
  id: number
52
  model: string
 
56
  created_at: number
57
  }
58
 
59
+ function loadTokenDataFromDb(providerSubscriptions: Record<string, boolean>): TokenUsageRecord[] {
60
  try {
61
  const db = getDatabase()
62
  const rows = db.prepare(`
 
68
 
69
  return rows.map((row) => {
70
  const totalTokens = row.input_tokens + row.output_tokens
 
71
  return {
72
  id: `db-${row.id}`,
73
  model: row.model,
 
77
  inputTokens: row.input_tokens,
78
  outputTokens: row.output_tokens,
79
  totalTokens,
80
+ cost: calculateTokenCost(row.model, row.input_tokens, row.output_tokens, { providerSubscriptions }),
81
  operation: 'heartbeat',
82
  }
83
  })
 
87
  }
88
  }
89
 
90
+ function normalizeTokenRecord(
91
+ record: Partial<TokenUsageRecord>,
92
+ providerSubscriptions: Record<string, boolean>,
93
+ ): TokenUsageRecord | null {
94
  if (!record.model || !record.sessionId) return null
95
  const inputTokens = Number(record.inputTokens ?? 0)
96
  const outputTokens = Number(record.outputTokens ?? 0)
 
105
  inputTokens,
106
  outputTokens,
107
  totalTokens,
108
+ cost: Number(record.cost ?? calculateTokenCost(model, inputTokens, outputTokens, { providerSubscriptions })),
109
  operation: String(record.operation ?? 'chat_completion'),
110
  duration: record.duration,
111
  }
 
134
  return deduped
135
  }
136
 
137
+ async function loadTokenDataFromFile(providerSubscriptions: Record<string, boolean>): Promise<TokenUsageRecord[]> {
138
  try {
139
  ensureDirExists(dirname(DATA_PATH))
140
  await access(DATA_PATH)
 
143
  if (!Array.isArray(parsed)) return []
144
 
145
  return parsed
146
+ .map((record: Partial<TokenUsageRecord>) => normalizeTokenRecord(record, providerSubscriptions))
147
  .filter((record): record is TokenUsageRecord => record !== null)
148
  } catch {
149
  return []
 
154
  * Load token data from persistent file, falling back to deriving from session stores.
155
  */
156
  async function loadTokenData(): Promise<TokenUsageRecord[]> {
157
+ const providerSubscriptions = getProviderSubscriptionFlags()
158
+ const dbRecords = loadTokenDataFromDb(providerSubscriptions)
159
+ const fileRecords = await loadTokenDataFromFile(providerSubscriptions)
160
  const combined = dedupeTokenRecords([...dbRecords, ...fileRecords]).sort((a, b) => b.timestamp - a.timestamp)
161
  if (combined.length > 0) {
162
  return combined
163
  }
164
 
165
  // Final fallback: derive from in-memory sessions
166
+ return deriveFromSessions(providerSubscriptions)
167
  }
168
 
169
  /**
170
  * Derive token usage records from OpenClaw session stores.
171
  * Each session has totalTokens, inputTokens, outputTokens, model, etc.
172
  */
173
+ function deriveFromSessions(providerSubscriptions: Record<string, boolean>): TokenUsageRecord[] {
174
  const sessions = getAllGatewaySessions(Infinity) // Get ALL sessions regardless of age
175
  const records: TokenUsageRecord[] = []
176
 
177
  for (const session of sessions) {
178
+ const inputTokens = session.inputTokens || 0
179
+ const outputTokens = session.outputTokens || 0
180
+ const totalTokens = inputTokens + outputTokens
181
+ if (totalTokens <= 0 && !session.model) continue // Skip empty sessions
182
+ const cost = calculateTokenCost(session.model || '', inputTokens, outputTokens, { providerSubscriptions })
 
 
183
 
184
  records.push({
185
  id: `session-${session.agent}-${session.key}`,
 
488
  }
489
 
490
  const totalTokens = inputTokens + outputTokens
491
+ const providerSubscriptions = getProviderSubscriptionFlags()
492
+ const cost = calculateTokenCost(model, inputTokens, outputTokens, { providerSubscriptions })
493
 
494
  const record: TokenUsageRecord = {
495
  id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
 
506
  }
507
 
508
  // Persist only manually posted usage records in the JSON file.
509
+ const existingData = await loadTokenDataFromFile(providerSubscriptions)
510
  existingData.unshift(record)
511
 
512
  if (existingData.length > 10000) {
src/lib/__tests__/token-pricing.test.ts ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from 'vitest'
2
+ import { calculateTokenCost, getModelPricing } from '@/lib/token-pricing'
3
+ import { getProviderFromModel } from '@/lib/provider-subscriptions'
4
+
5
+ describe('token pricing', () => {
6
+ it('uses separate input/output rates for Claude Sonnet 4.5', () => {
7
+ const cost = calculateTokenCost('anthropic/claude-sonnet-4-5', 10, 185)
8
+ expect(cost).toBeCloseTo(0.002805, 9)
9
+ })
10
+
11
+ it('matches model aliases by short model name', () => {
12
+ const pricing = getModelPricing('gateway::claude-opus-4-6')
13
+ expect(pricing.inputPerMTok).toBe(15.0)
14
+ expect(pricing.outputPerMTok).toBe(75.0)
15
+ })
16
+
17
+ it('falls back to conservative default pricing for unknown models', () => {
18
+ const cost = calculateTokenCost('unknown/model', 1_000_000, 1_000_000)
19
+ expect(cost).toBe(18)
20
+ })
21
+
22
+ it('keeps local models at zero cost', () => {
23
+ const cost = calculateTokenCost('ollama/qwen2.5-coder:14b', 50_000, 50_000)
24
+ expect(cost).toBe(0)
25
+ })
26
+
27
+ it('returns zero cost for subscribed providers', () => {
28
+ const cost = calculateTokenCost('anthropic/claude-sonnet-4-5', 2000, 2000, {
29
+ providerSubscriptions: { anthropic: true },
30
+ })
31
+ expect(cost).toBe(0)
32
+ })
33
+
34
+ it('maps providers from model prefixes and names', () => {
35
+ expect(getProviderFromModel('openai/gpt-4.1')).toBe('openai')
36
+ expect(getProviderFromModel('anthropic/claude-sonnet-4-5')).toBe('anthropic')
37
+ expect(getProviderFromModel('gateway::codex-mini')).toBe('openai')
38
+ })
39
+ })
src/lib/provider-subscriptions.ts ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { existsSync, readFileSync } from 'node:fs'
2
+ import path from 'node:path'
3
+ import os from 'node:os'
4
+ import { config } from '@/lib/config'
5
+
6
+ interface ProviderSubscription {
7
+ provider: string
8
+ type: string
9
+ source: 'env' | 'file'
10
+ }
11
+
12
+ interface SubscriptionDetectionResult {
13
+ active: Record<string, ProviderSubscription>
14
+ }
15
+
16
+ const NEGATIVE_TYPES = new Set(['none', 'no', 'false', 'free', 'unknown', 'api_key', 'apikey'])
17
+
18
+ const OPENAI_CREDENTIAL_PATHS = [
19
+ path.join(os.homedir(), '.config', 'openai', 'auth.json'),
20
+ path.join(os.homedir(), '.openai', 'auth.json'),
21
+ path.join(os.homedir(), '.codex', 'auth.json'),
22
+ ]
23
+
24
+ let detectionCache: { ts: number; value: SubscriptionDetectionResult } | null = null
25
+ const CACHE_TTL_MS = 30_000
26
+
27
+ function normalizeProvider(provider: string): string {
28
+ return provider.trim().toLowerCase()
29
+ }
30
+
31
+ function normalizeType(value: string): string {
32
+ return value.trim().toLowerCase()
33
+ }
34
+
35
+ function isPositiveSubscription(type: string): boolean {
36
+ if (!type) return false
37
+ return !NEGATIVE_TYPES.has(normalizeType(type))
38
+ }
39
+
40
+ function parseJsonFile(filePath: string): unknown | null {
41
+ try {
42
+ if (!existsSync(filePath)) return null
43
+ return JSON.parse(readFileSync(filePath, 'utf-8'))
44
+ } catch {
45
+ return null
46
+ }
47
+ }
48
+
49
+ function findNestedString(root: unknown, keys: string[]): string | null {
50
+ const queue: unknown[] = [root]
51
+ const wanted = new Set(keys.map(k => k.toLowerCase()))
52
+
53
+ while (queue.length > 0) {
54
+ const current = queue.shift()
55
+ if (!current || typeof current !== 'object') continue
56
+ if (Array.isArray(current)) {
57
+ for (const item of current) queue.push(item)
58
+ continue
59
+ }
60
+
61
+ for (const [rawKey, value] of Object.entries(current)) {
62
+ const key = rawKey.toLowerCase()
63
+ if (wanted.has(key) && typeof value === 'string' && value.trim()) {
64
+ return value.trim()
65
+ }
66
+ if (value && typeof value === 'object') queue.push(value)
67
+ }
68
+ }
69
+
70
+ return null
71
+ }
72
+
73
+ function detectAnthropicFromFile(): ProviderSubscription | null {
74
+ const credsPath = path.join(config.claudeHome, '.credentials.json')
75
+ const creds = parseJsonFile(credsPath) as Record<string, unknown> | null
76
+ if (!creds || typeof creds !== 'object') return null
77
+
78
+ const oauth = creds.claudeAiOauth as Record<string, unknown> | undefined
79
+ const subscriptionType = typeof oauth?.subscriptionType === 'string' ? oauth.subscriptionType : ''
80
+ if (!isPositiveSubscription(subscriptionType)) return null
81
+
82
+ return {
83
+ provider: 'anthropic',
84
+ type: normalizeType(subscriptionType),
85
+ source: 'file',
86
+ }
87
+ }
88
+
89
+ function detectOpenAIFromFile(): ProviderSubscription | null {
90
+ for (const credsPath of OPENAI_CREDENTIAL_PATHS) {
91
+ const creds = parseJsonFile(credsPath)
92
+ if (!creds) continue
93
+ const plan = findNestedString(creds, [
94
+ 'subscriptionType',
95
+ 'subscription_type',
96
+ 'accountPlan',
97
+ 'account_plan',
98
+ 'plan',
99
+ 'tier',
100
+ ])
101
+ if (!plan || !isPositiveSubscription(plan)) continue
102
+ return {
103
+ provider: 'openai',
104
+ type: normalizeType(plan),
105
+ source: 'file',
106
+ }
107
+ }
108
+ return null
109
+ }
110
+
111
+ function detectFromEnv(): Record<string, ProviderSubscription> {
112
+ const active: Record<string, ProviderSubscription> = {}
113
+
114
+ const allProvidersRaw = process.env.MC_SUBSCRIBED_PROVIDERS || ''
115
+ if (allProvidersRaw.trim()) {
116
+ for (const raw of allProvidersRaw.split(',')) {
117
+ const provider = normalizeProvider(raw)
118
+ if (!provider) continue
119
+ active[provider] = {
120
+ provider,
121
+ type: 'subscription',
122
+ source: 'env',
123
+ }
124
+ }
125
+ }
126
+
127
+ for (const [key, value] of Object.entries(process.env)) {
128
+ if (!value) continue
129
+
130
+ const explicitMatch = key.match(/^MC_([A-Z0-9_]+)_SUBSCRIPTION(?:_TYPE)?$/)
131
+ if (explicitMatch) {
132
+ const provider = normalizeProvider(explicitMatch[1].replace(/_/g, '-'))
133
+ const type = normalizeType(value)
134
+ if (isPositiveSubscription(type)) {
135
+ active[provider] = { provider, type, source: 'env' }
136
+ } else {
137
+ delete active[provider]
138
+ }
139
+ continue
140
+ }
141
+
142
+ const providerMatch = key.match(/^([A-Z0-9_]+)_SUBSCRIPTION_TYPE$/)
143
+ if (providerMatch) {
144
+ const provider = normalizeProvider(providerMatch[1].replace(/_/g, '-'))
145
+ const type = normalizeType(value)
146
+ if (isPositiveSubscription(type)) {
147
+ active[provider] = { provider, type, source: 'env' }
148
+ } else {
149
+ delete active[provider]
150
+ }
151
+ }
152
+ }
153
+
154
+ return active
155
+ }
156
+
157
+ export function detectProviderSubscriptions(forceRefresh = false): SubscriptionDetectionResult {
158
+ const now = Date.now()
159
+ if (!forceRefresh && detectionCache && (now - detectionCache.ts) < CACHE_TTL_MS) {
160
+ return detectionCache.value
161
+ }
162
+
163
+ const active = detectFromEnv()
164
+
165
+ const anthropic = detectAnthropicFromFile()
166
+ if (anthropic) active.anthropic = anthropic
167
+
168
+ const openai = detectOpenAIFromFile()
169
+ if (openai) active.openai = openai
170
+
171
+ const value = { active }
172
+ detectionCache = { ts: now, value }
173
+ return value
174
+ }
175
+
176
+ export function getProviderSubscriptionFlags(forceRefresh = false): Record<string, boolean> {
177
+ const detected = detectProviderSubscriptions(forceRefresh)
178
+ return Object.fromEntries(
179
+ Object.keys(detected.active).map((provider) => [provider, true])
180
+ )
181
+ }
182
+
183
+ export function getPrimarySubscription(forceRefresh = false): ProviderSubscription | null {
184
+ const detected = detectProviderSubscriptions(forceRefresh).active
185
+ return detected.anthropic || detected.openai || Object.values(detected)[0] || null
186
+ }
187
+
188
+ export function getProviderFromModel(modelName: string): string {
189
+ const normalized = modelName.trim().toLowerCase()
190
+ if (!normalized) return 'unknown'
191
+
192
+ const [prefix] = normalized.split('/')
193
+ if (prefix && !prefix.includes(':')) {
194
+ // Most models are provider-prefixed, e.g., "anthropic/claude-sonnet-4-5".
195
+ if (prefix === 'claude') return 'anthropic'
196
+ if (prefix === 'gpt' || prefix === 'o1' || prefix === 'o3') return 'openai'
197
+ return prefix
198
+ }
199
+
200
+ if (normalized.includes('claude')) return 'anthropic'
201
+ if (normalized.includes('gpt') || normalized.includes('codex') || normalized.includes('o1') || normalized.includes('o3')) return 'openai'
202
+ return 'unknown'
203
+ }
204
+
src/lib/token-pricing.ts ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { getProviderFromModel } from '@/lib/provider-subscriptions'
2
+
3
+ interface ModelPricing {
4
+ inputPerMTok: number
5
+ outputPerMTok: number
6
+ }
7
+
8
+ const DEFAULT_MODEL_PRICING: ModelPricing = {
9
+ inputPerMTok: 3.0,
10
+ outputPerMTok: 15.0,
11
+ }
12
+
13
+ const MODEL_PRICING: Record<string, ModelPricing> = {
14
+ 'anthropic/claude-3-5-haiku-latest': { inputPerMTok: 0.8, outputPerMTok: 4.0 },
15
+ 'claude-3-5-haiku': { inputPerMTok: 0.8, outputPerMTok: 4.0 },
16
+ 'anthropic/claude-haiku-4-5': { inputPerMTok: 0.8, outputPerMTok: 4.0 },
17
+ 'claude-haiku-4-5': { inputPerMTok: 0.8, outputPerMTok: 4.0 },
18
+
19
+ 'anthropic/claude-sonnet-4-20250514': { inputPerMTok: 3.0, outputPerMTok: 15.0 },
20
+ 'claude-sonnet-4': { inputPerMTok: 3.0, outputPerMTok: 15.0 },
21
+ 'anthropic/claude-sonnet-4-5': { inputPerMTok: 3.0, outputPerMTok: 15.0 },
22
+ 'claude-sonnet-4-5': { inputPerMTok: 3.0, outputPerMTok: 15.0 },
23
+ 'anthropic/claude-sonnet-4-6': { inputPerMTok: 3.0, outputPerMTok: 15.0 },
24
+ 'claude-sonnet-4-6': { inputPerMTok: 3.0, outputPerMTok: 15.0 },
25
+
26
+ 'anthropic/claude-opus-4-5': { inputPerMTok: 15.0, outputPerMTok: 75.0 },
27
+ 'claude-opus-4-5': { inputPerMTok: 15.0, outputPerMTok: 75.0 },
28
+ 'anthropic/claude-opus-4-6': { inputPerMTok: 15.0, outputPerMTok: 75.0 },
29
+ 'claude-opus-4-6': { inputPerMTok: 15.0, outputPerMTok: 75.0 },
30
+
31
+ // For non-Anthropic models where we only have one published blended estimate,
32
+ // apply the same rate for both input and output.
33
+ 'groq/llama-3.1-8b-instant': { inputPerMTok: 0.05, outputPerMTok: 0.05 },
34
+ 'groq/llama-3.3-70b-versatile': { inputPerMTok: 0.59, outputPerMTok: 0.59 },
35
+ 'moonshot/kimi-k2.5': { inputPerMTok: 1.0, outputPerMTok: 1.0 },
36
+ 'minimax/minimax-m2.1': { inputPerMTok: 0.3, outputPerMTok: 0.3 },
37
+ 'ollama/deepseek-r1:14b': { inputPerMTok: 0.0, outputPerMTok: 0.0 },
38
+ 'ollama/qwen2.5-coder:7b': { inputPerMTok: 0.0, outputPerMTok: 0.0 },
39
+ 'ollama/qwen2.5-coder:14b': { inputPerMTok: 0.0, outputPerMTok: 0.0 },
40
+ }
41
+
42
+ function normalizedModelName(modelName: string): string {
43
+ return modelName.trim().toLowerCase()
44
+ }
45
+
46
+ export function getModelPricing(modelName: string): ModelPricing {
47
+ const normalized = normalizedModelName(modelName)
48
+ if (MODEL_PRICING[normalized] !== undefined) return MODEL_PRICING[normalized]
49
+
50
+ for (const [model, pricing] of Object.entries(MODEL_PRICING)) {
51
+ const shortName = model.split('/').pop() || model
52
+ if (normalized.includes(shortName)) return pricing
53
+ }
54
+
55
+ return DEFAULT_MODEL_PRICING
56
+ }
57
+
58
+ interface CostOptions {
59
+ providerSubscriptions?: Record<string, boolean>
60
+ }
61
+
62
+ export function calculateTokenCost(
63
+ modelName: string,
64
+ inputTokens: number,
65
+ outputTokens: number,
66
+ options?: CostOptions,
67
+ ): number {
68
+ const provider = getProviderFromModel(modelName)
69
+ if (provider !== 'unknown' && options?.providerSubscriptions?.[provider]) {
70
+ return 0
71
+ }
72
+
73
+ const pricing = getModelPricing(modelName)
74
+ return ((inputTokens * pricing.inputPerMTok) + (outputTokens * pricing.outputPerMTok)) / 1_000_000
75
+ }