@@ -29,6 +29,7 @@ const fireworksAgent = new Agent({
2929/** Map from OpenRouter model IDs to Fireworks standard API model IDs */
3030const FIREWORKS_MODEL_MAP : Record < string , string > = {
3131 'minimax/minimax-m2.5' : 'accounts/fireworks/models/minimax-m2p5' ,
32+ 'z-ai/glm-5.1' : 'accounts/fireworks/models/glm-5p1' ,
3233}
3334
3435/** Flag to enable custom Fireworks deployments (set to false to use global API only) */
@@ -137,12 +138,31 @@ function createFireworksRequest(params: {
137138 } )
138139}
139140
140- // Fireworks per-token pricing (dollars per token)
141- const FIREWORKS_INPUT_COST_PER_TOKEN = 0.30 / 1_000_000
142- const FIREWORKS_CACHED_INPUT_COST_PER_TOKEN = 0.03 / 1_000_000
143- const FIREWORKS_OUTPUT_COST_PER_TOKEN = 1.20 / 1_000_000
141+ // Fireworks per-token pricing (dollars per token), keyed by OpenRouter model ID
142+ interface FireworksPricing {
143+ inputCostPerToken : number
144+ cachedInputCostPerToken : number
145+ outputCostPerToken : number
146+ }
147+
148+ const FIREWORKS_PRICING_MAP : Record < string , FireworksPricing > = {
149+ 'minimax/minimax-m2.5' : {
150+ inputCostPerToken : 0.30 / 1_000_000 ,
151+ cachedInputCostPerToken : 0.03 / 1_000_000 ,
152+ outputCostPerToken : 1.20 / 1_000_000 ,
153+ } ,
154+ 'z-ai/glm-5.1' : {
155+ inputCostPerToken : 1.40 / 1_000_000 ,
156+ cachedInputCostPerToken : 0.26 / 1_000_000 ,
157+ outputCostPerToken : 4.40 / 1_000_000 ,
158+ } ,
159+ }
160+
161+ function getFireworksPricing ( model : string ) : FireworksPricing {
162+ return FIREWORKS_PRICING_MAP [ model ] ?? FIREWORKS_MODEL_MAP [ 'z-ai/glm-5.1' ]
163+ }
144164
145- function extractUsageAndCost ( usage : Record < string , unknown > | undefined | null ) : UsageData {
165+ function extractUsageAndCost ( usage : Record < string , unknown > | undefined | null , model : string ) : UsageData {
146166 if ( ! usage ) return { inputTokens : 0 , outputTokens : 0 , cacheReadInputTokens : 0 , reasoningTokens : 0 , cost : 0 }
147167 const promptDetails = usage . prompt_tokens_details as Record < string , unknown > | undefined | null
148168 const completionDetails = usage . completion_tokens_details as Record < string , unknown > | undefined | null
@@ -153,11 +173,12 @@ function extractUsageAndCost(usage: Record<string, unknown> | undefined | null):
153173 const reasoningTokens = typeof completionDetails ?. reasoning_tokens === 'number' ? completionDetails . reasoning_tokens : 0
154174
155175 // Fireworks doesn't return cost — compute from token counts and known pricing
176+ const pricing = getFireworksPricing ( model )
156177 const nonCachedInputTokens = Math . max ( 0 , inputTokens - cacheReadInputTokens )
157178 const cost =
158- nonCachedInputTokens * FIREWORKS_INPUT_COST_PER_TOKEN +
159- cacheReadInputTokens * FIREWORKS_CACHED_INPUT_COST_PER_TOKEN +
160- outputTokens * FIREWORKS_OUTPUT_COST_PER_TOKEN
179+ nonCachedInputTokens * pricing . inputCostPerToken +
180+ cacheReadInputTokens * pricing . cachedInputCostPerToken +
181+ outputTokens * pricing . outputCostPerToken
161182
162183 return { inputTokens, outputTokens, cacheReadInputTokens, reasoningTokens, cost }
163184}
@@ -192,7 +213,7 @@ export async function handleFireworksNonStream({
192213 const data = await response . json ( )
193214 const content = data . choices ?. [ 0 ] ?. message ?. content ?? ''
194215 const reasoningText = data . choices ?. [ 0 ] ?. message ?. reasoning_content ?? data . choices ?. [ 0 ] ?. message ?. reasoning ?? ''
195- const usageData = extractUsageAndCost ( data . usage )
216+ const usageData = extractUsageAndCost ( data . usage , originalModel )
196217
197218 insertMessageToBigQuery ( {
198219 messageId : data . id ,
@@ -493,7 +514,7 @@ async function handleResponse({
493514 return { state }
494515 }
495516
496- const usageData = extractUsageAndCost ( data . usage as Record < string , unknown > )
517+ const usageData = extractUsageAndCost ( data . usage as Record < string , unknown > , originalModel )
497518 const messageId = typeof data . id === 'string' ? data . id : 'unknown'
498519
499520 insertMessageToBigQuery ( {
0 commit comments