Skip to content

Instantly share code, notes, and snippets.

@ruvnet
Last active April 22, 2024 22:28
Show Gist options
  • Save ruvnet/395ca573b635b18be386bea1b6898d4d to your computer and use it in GitHub Desktop.
Save ruvnet/395ca573b635b18be386bea1b6898d4d to your computer and use it in GitHub Desktop.
MindStudio Cost Estimator

MindStudio Cost Estimator

Welcome to MindStudio's cost estimation tool for various AI models. Our tool allows you to calculate the cost based on token usage for different AI models, with a clear emphasis on transparency and predictability of pricing.

Overview

Each AI model in our suite has distinct capabilities and price points. We bill these models based on the number of tokens used during operation. Think of tokens as pieces of words, where 1,000 tokens are approximately equivalent to 750 words.

Configuration

Our tool is configured to handle different AI models and their associated costs. Here's a snapshot of the configuration we use:

const config = {
  thumbnailUrl: 'https://youai.imgix.net/images/..._1702453515478.png',
  blockStyle: {
    backgroundImageUrl: 'https://youai.imgix.net/images/..._1710391440552.webp?width=1024',
    foregroundColor: '#ffffff',
    label: ' ',
  },
  configurationSections: [
    // Model Configuration Section
    // ...
  ],
}

In the configuration sections, you will find settings for model type, token counts, and other parameters to calculate the cost. We assume text input for each parameter, with defaults provided for a quick start.

Usage

To estimate costs, you need to select your AI model and provide token usage details. Our intuitive configuration allows for easy input and adjustments.

Token Cost Update

The updateTokenCosts function is at the core of our tool. It dynamically updates the token costs based on the model selection:

async function updateTokenCosts() {
  // ...
  // Model Pricing
  // ...
  await calculate();
}

Calculation

The calculate function takes input and output tokens, along with other operational parameters, to calculate total and deployment costs:

async function calculate() {
  // ...
}

Initialization

To kick things off, the initializeCostEstimation function sets up default values and begins the cost estimation process:

async function initializeCostEstimation() {
  // ...
}

Environment

Lastly, our environment object is where all the variables are stored:

environment = {
  vars: {
    modelSelector: 'Claude 3 Sonnet', // Placeholder for model selection
    inputTokens: '100000', // Placeholder for the number of input tokens
    // ...
  },
  config: {
    totalTokenCostVar: 'totalTokenCost', // Variable for total token cost
    deploymentCostVar: 'deploymentCost', // Variable for deployment cost
  },
}

Credits

The tool's functionality was developed by the innovative team at MindStudio, with special contributions by @rUv.

For more detailed information on each function and the complete configuration setup, please refer to the inline documentation within the code.

Thank you for choosing MindStudio for your AI model cost estimation needs!

Testing in Functions UI.

Executing function...
 
Logs
------------------------------------
"Initializing cost estimation with the following settings:"
"Model Selector: Claude 3 Sonnet"
"Input Tokens: 1000000"
"Output Tokens: 1000000"
"Requests per Day: 10000"
"Days per Month: 30"
"Total Token Cost for Claude 3 Sonnet: $22.50"
"Deployment Cost for Claude 3 Sonnet: $3.37"
 
Variable updates
------------------------------------
costPerTokenInput: 0.00000375
costPerTokenOutput: 0.00001875
costPerToken: 0.000011249999999999999
totalTokenCost: "22.50"
deploymentCost: "3.37"
 
✓ Finished execution in 8ms
  Memory used: 1.08mb
// Pricing per 1 million tokens, converted to per-token cost
const modelPricing = {
'GPT-4': { inputCost: 30.00 / 1e6, outputCost: 60.00 / 1e6 },
'GPT-4 32K': { inputCost: 60.00 / 1e6, outputCost: 120.00 / 1e6 },
'GPT-4 Turbo': { inputCost: 10.00 / 1e6, outputCost: 30.00 / 1e6 },
'GPT-3.5 Instruct': { inputCost: 1.50 / 1e6, outputCost: 2.00 / 1e6 },
'GPT-3.5': { inputCost: 0.50 / 1e6, outputCost: 1.50 / 1e6 },
'Claude 3 Opus': { inputCost: 15.00 / 1e6, outputCost: 75.00 / 1e6 },
'Claude 2': { inputCost: 8.00 / 1e6, outputCost: 24.00 / 1e6 },
'Claude 3 Sonnet': { inputCost: 3.00 / 1e6, outputCost: 15.00 / 1e6 },
'Claude Instant': { inputCost: 0.80 / 1e6, outputCost: 2.40 / 1e6 },
'Claude 3 Haiku': { inputCost: 0.25 / 1e6, outputCost: 1.25 / 1e6 },
'PaLM 2': { inputCost: 0.33 / 1e6, outputCost: 0.65 / 1e6 },
'Gemini Pro': { inputCost: 0.17 / 1e6, outputCost: 0.49 / 1e6 },
'Gemini Pro Vision': { inputCost: 0.17 / 1e6, outputCost: 0.49 / 1e6 },
'Mistral 8x7B Instruct': { inputCost: 0.36 / 1e6, outputCost: 0.36 / 1e6 },
'Mistral 7B Instruct': { inputCost: 0.17 / 1e6, outputCost: 0.17 / 1e6 },
'Llama-2 70B Chat': { inputCost: 0.91 / 1e6, outputCost: 1.17 / 1e6 },
'Code Llama': { inputCost: 0.78 / 1e6, outputCost: 0.78 / 1e6 },
'Llama-2 13B Chat': { inputCost: 0.29 / 1e6, outputCost: 0.29 / 1e6 }
};
config = {
thumbnailUrl: 'https://youai.imgix.net/images/9e603bae-0732-4f04-8136-2eeec1f0a9fe_1702453515478.png',
blockStyle: {
backgroundImageUrl: 'https://youai.imgix.net/images/f24957c5-cbbb-4a9b-a1f9-0e0f47745bf5_1710391440552.webp?width=1024',
foregroundColor: '#ffffff',
label: ' ',
},
configurationSections: [
{
title: 'Model Configuration',
items: [
{
label: 'Model Type',
type: 'text', // Assumes text input is required
variable: 'modelSelector',
helpText: 'Enter the model type (e.g., GPT3.5, GPT4-8K, GPT4-32K).',
defaultValue: 'GPT4-8K',
},
{
label: 'Input Tokens',
type: 'text', // Assumes text input for token counts
variable: 'inputTokens',
helpText: 'Enter the number of input tokens for cost calculation.',
defaultValue: '1000',
},
{
label: 'Output Tokens',
type: 'text', // Assumes text input for token counts
variable: 'outputTokens',
helpText: 'Enter the number of output tokens for cost calculation.',
defaultValue: '1000',
},
{
label: 'Requests per Day',
type: 'text', // Assumes text input for request counts
variable: 'requestsPerDay',
helpText: 'Average number of API requests per day.',
defaultValue: '1000',
},
{
label: 'Days per Month',
type: 'text', // Assumes text input for day counts
variable: 'daysPerMonth',
helpText: 'Number of operational days per month.',
defaultValue: '30',
},
{
label: 'Total Token Cost Variable',
type: 'text', // Variable name as text
variable: 'totalTokenCostVar',
helpText: 'Variable name to store the total token cost calculation.',
defaultValue: 'totalTokenCost',
},
{
label: 'Deployment Cost Variable',
type: 'text', // Variable name as text
variable: 'deploymentCostVar',
helpText: 'Variable name to store the deployment cost calculation.',
defaultValue: 'deploymentCost',
},
],
},
],
}
// Update Token Costs based on model selection
// created by @rUv, cause i could.
async function updateTokenCosts() {
const selectedModel = ai.vars.modelSelector || 'GPT-4'; // Default to GPT-4
let inputCost, outputCost;
// Pricing per 1 million tokens, converted to per-token cost
const modelPricing = {
'GPT-4': { inputCost: 36.00 / 1e6, outputCost: 72.00 / 1e6 },
'GPT-4 Turbo': { inputCost: 12.50 / 1e6, outputCost: 37.50 / 1e6 },
'GPT-3.5 Instruct': { inputCost: 1.95 / 1e6, outputCost: 2.60 / 1e6 },
'GPT-3.5': { inputCost: 1.30 / 1e6, outputCost: 2.60 / 1e6 },
'Claude 3 Opus': { inputCost: 18.00 / 1e6, outputCost: 90.00 / 1e6 },
'Claude 2': { inputCost: 10.00 / 1e6, outputCost: 30.00 / 1e6 },
'Claude 3 Sonnet': { inputCost: 3.75 / 1e6, outputCost: 18.75 / 1e6 },
'Claude Instant': { inputCost: 1.04 / 1e6, outputCost: 3.12 / 1e6 },
'Claude 3 Haiku': { inputCost: 0.33 / 1e6, outputCost: 1.63 / 1e6 },
'PaLM 2': { inputCost: 0.33 / 1e6, outputCost: 0.65 / 1e6 },
'Gemini Pro': { inputCost: 0.17 / 1e6, outputCost: 0.49 / 1e6 },
'Gemini Pro Vision': { inputCost: 0.17 / 1e6, outputCost: 0.49 / 1e6 },
'Mistral 8x7B Instruct': { inputCost: 0.36 / 1e6, outputCost: 0.36 / 1e6 },
'Mistral 7B Instruct': { inputCost: 0.17 / 1e6, outputCost: 0.17 / 1e6 },
'Llama-2 70B Chat': { inputCost: 0.91 / 1e6, outputCost: 1.17 / 1e6 },
'Code Llama': { inputCost: 0.78 / 1e6, outputCost: 0.78 / 1e6 },
'Llama-2 13B Chat': { inputCost: 0.29 / 1e6, outputCost: 0.29 / 1e6 }
};
if (modelPricing[selectedModel]) {
inputCost = modelPricing[selectedModel].inputCost;
outputCost = modelPricing[selectedModel].outputCost;
} else {
console.log(`Unknown model selected: ${selectedModel}`);
ai.log(`Unknown model selected: ${selectedModel}`);
return;
}
ai.vars.costPerTokenInput = inputCost;
ai.vars.costPerTokenOutput = outputCost;
ai.vars.costPerToken = (inputCost + outputCost) / 2;
await calculate();
}
// Calculate Token and Operational Costs
async function calculate() {
const inputTokens = parseFloat(ai.vars.inputTokens);
const outputTokens = parseFloat(ai.vars.outputTokens);
const costPerToken = ai.vars.costPerToken;
const totalTokenCost = (inputTokens + outputTokens) * costPerToken;
ai.vars.totalTokenCost = totalTokenCost.toFixed(2);
console.log(`Total Token Cost for ${ai.vars.modelSelector}: $${ai.vars.totalTokenCost}`);
ai.log(`Total Token Cost for ${ai.vars.modelSelector}: $${ai.vars.totalTokenCost}`);
const requestsPerDay = parseFloat(ai.vars.requestsPerDay);
const daysPerMonth = parseFloat(ai.vars.daysPerMonth);
const deploymentCost = requestsPerDay * daysPerMonth * costPerToken;
ai.vars.deploymentCost = deploymentCost.toFixed(2);
console.log(`Deployment Cost for ${ai.vars.modelSelector}: $${ai.vars.deploymentCost}`);
ai.log(`Deployment Cost for ${ai.vars.modelSelector}: $${ai.vars.deploymentCost}`);
}
// Function to initialize the cost estimation process
async function initializeCostEstimation() {
ai.vars.modelSelector = ai.vars.modelSelector || 'GPT-4';
ai.vars.inputTokens = ai.vars.inputTokens || '1000';
ai.vars.outputTokens = ai.vars.outputTokens || '1000';
ai.vars.requestsPerDay = ai.vars.requestsPerDay || '1000';
ai.vars.daysPerMonth = ai.vars.daysPerMonth || '30';
console.log('Initializing cost estimation with the following settings:');
console.log(`Model Selector: ${ai.vars.modelSelector}`);
console.log(`Input Tokens: ${ai.vars.inputTokens}`);
console.log(`Output Tokens: ${ai.vars.outputTokens}`);
console.log(`Requests per Day: ${ai.vars.requestsPerDay}`);
console.log(`Days per Month: ${ai.vars.daysPerMonth}`);
await updateTokenCosts();
}
// Start the initialization process
initializeCostEstimation();
environment = {
vars: {
modelSelector: 'Claude 3 Sonnet', // Setting 'modelSelector' to 'GPT-4' for testing
inputTokens: '100000', // Ensure these are string values if that's what your system expects
outputTokens: '10000',
requestsPerDay: '100000',
daysPerMonth: '30',
},
config: {
totalTokenCostVar: 'totalTokenCost',
deploymentCostVar: 'deploymentCost',
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment