Created
October 22, 2024 20:50
-
-
Save boxabirds/ee1d9aed1eb6bc2e3fb9fd2150a03b7c to your computer and use it in GitHub Desktop.
Google Gemini cost estimator (node)
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
const {GoogleGenerativeAI} = require('@google/generative-ai'); | |
/** | |
* Estimates the cost of a Gemini API invocation based on token usage. | |
* | |
* @param {Object} usageMetadata - The usage metadata from the API response. | |
* @param {number} usageMetadata.promptTokenCount - The number of tokens in the input prompt. | |
* @param {number} usageMetadata.candidatesTokenCount - The number of tokens in the output. | |
* @returns {number} The estimated cost of the invocation in USD. | |
* | |
* Pricing information: | |
* - For prompts up to 128k tokens: | |
* - Input: $0.075 per 1 million tokens | |
* - Output: $0.30 per 1 million tokens | |
* - For prompts longer than 128k tokens: | |
* - Input: $0.15 per 1 million tokens | |
* - Output: $0.60 per 1 million tokens | |
* | |
* Source: [Google AI Pricing](https://ai.google.dev/pricing#1_5flash) | |
*/ | |
function estimateInvocationCost(usageMetadata) { | |
const { promptTokenCount, candidatesTokenCount } = usageMetadata; | |
const totalTokenCount = promptTokenCount + candidatesTokenCount; | |
let inputCostPerMillionTokens; | |
let outputCostPerMillionTokens; | |
if (totalTokenCount <= 128000) { | |
inputCostPerMillionTokens = 0.075; | |
outputCostPerMillionTokens = 0.30; | |
} else { | |
inputCostPerMillionTokens = 0.15; | |
outputCostPerMillionTokens = 0.60; | |
} | |
const inputCost = (promptTokenCount / 1e6) * inputCostPerMillionTokens; | |
const outputCost = (candidatesTokenCount / 1e6) * outputCostPerMillionTokens; | |
return inputCost + outputCost; | |
} | |
const model = client.getGenerativeModel({ | |
model: 'gemini-1.5-flash', | |
safetySettings: safetySettings, | |
generation_config:{ | |
"response_mime_type": "application/json", | |
"temperature": 0.0 | |
} | |
}); | |
const result = await model.generateContent(prompt); | |
const usageMetadata = result.response.usageMetadata; | |
const cost = estimateInvocationCost(usageMetadata); | |
console.log(`Estimated cost of parsing stripped ${url}: $${cost.toFixed(6)}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment