Optimize Mistral AI costs through model selection, token management, and usage monitoring.
Use when analyzing Mistral billing, reducing API costs,
or implementing usage monitoring and budget alerts.
Trigger with phrases like "mistral cost", "mistral billing",
"reduce mistral costs", "mistral pricing", "mistral expensive", "mistral budget".
Optimize Mistral AI costs through model selection, token management, caching, batch inference, and budget monitoring. Mistral offers the best price-performance in the market with models from $0.1/M tokens (Ministral/Small) to $0.5/M tokens (Large).
// Reduce tokens = reduce cost directly
function optimizeForCost(systemPrompt: string): string {
// Remove filler words
return systemPrompt
.replace(/please\s+/gi, '')
.replace(/I would like you to\s+/gi, '')
.replace(/\s+/g, ' ')
.trim();
}
// Before: "I would like you to please provide a comprehensive and detailed explanation of how REST APIs work." (~25 tokens)
// After: "Explain REST APIs concisely." (~6 tokens, 76% reduction)
// Set maxTokens to prevent runaway output
const response = await client.chat.complete({
model: 'mistral-small-latest',
messages,
maxTokens: 200, // Cap output — prevents 4000-token essays
});
Step 5: Batch API for Bulk Workloads
// Batch API = 50% cost reduction for non-realtime processing
// Instead of 100K individual API calls at $11/month (small)
// Use batch: $5.50/month for the same work
// Supported endpoints:
// /v1/chat/completions, /v1/embeddings, /v1/fim/completions,
// /v1/moderations, /v1/ocr, /v1/classifications
// See mistral-webhooks-events for implementation details
Step 6: Usage Tracking SQL
CREATE TABLE mistral_usage (
id SERIAL PRIMARY KEY,
model VARCHAR(50) NOT NULL,
input_tokens INTEGER NOT NULL,
output_tokens INTEGER NOT NULL,
cost_usd DECIMAL(10, 6) NOT NULL,
is_batch BOOLEAN DEFAULT FALSE,
endpoint VARCHAR(50),
user_id VARCHAR(50),
created_at TIMESTAMP DEFAULT NOW()
);
-- Daily cost report
SELECT
DATE(created_at) AS day,
model,
SUM(input_tokens) AS total_input,
SUM(output_tokens) AS total_output,
SUM(cost_usd) AS total_cost
FROM mistral_usage
WHERE created_at >= NOW() - INTERVAL '30 days'
GROUP BY 1, 2
ORDER BY 1 DESC, 5 DESC;
-- Highest-cost users
SELECT user_id, SUM(cost_usd) AS cost, COUNT(*) AS requests
FROM mistral_usage
WHERE created_at >= DATE_TRUNC('month', NOW())
GROUP BY 1 ORDER BY 2 DESC LIMIT 10;