Intermediate LevelAI, LLMs, Embeddings & MCP
How do you calculate exact token counts and API costs using tiktoken or model tokenizers?
❓ Interview Prompt: Explain token-based API billing models, prompt vs completion token pricing, and pre-request budget estimation.
LLM Cloud APIs (OpenAI, Anthropic, Gemini) bill based on Input (Prompt) Tokens and Output (Completion) Tokens, with output tokens typically priced 3x to 4x higher than input tokens.
Cost Calculation Pipeline:
1
Count exact prompt tokens locally before calling API using
tiktoken.2
Estimate expected completion max tokens.
3
Apply provider price rates per 1,000,000 (1M) tokens.
Code Implementation Example
typescript
function estimateApiCost(promptTokens: number, completionTokens: number, inputRatePerM: number = 2.50, outputRatePerM: number = 10.00): number {
const inputCost = (promptTokens / 1_000_000) * inputRatePerM;
const outputCost = (completionTokens / 1_000_000) * outputRatePerM;
return Number((inputCost + outputCost).toFixed(5));
}
console.log('Estimated Cost:', estimateApiCost(50000, 1000), 'USD'); // ~$0.135💡Calculating exact LLM API invocation cost based on token counts.
🎯 Interview Key Takeaway
Count tokens locally with tiktoken prior to API execution to estimate costs and enforce application budget limits.
#Cost Estimation#Tiktoken#Tokens#API Billing#Intermediate
Back to all AI, LLMs, Embeddings & MCP Questions→