Optimize Groq costs through tier selection, sampling, and usage monitoring.
Use when analyzing Groq billing, reducing API costs,
or implementing usage monitoring and budget alerts.
Trigger with phrases like "groq cost", "groq billing",
"reduce groq costs", "groq pricing", "groq expensive", "groq budget".
Optimize Groq inference costs through smart model routing, token minimization, and caching. Groq pricing is already extremely competitive, but at high volume the savings from routing classification to 8B vs 70B are 12x per request.
// COST SAVINGS: Reduce system prompt tokens
// Groq charges for BOTH input and output tokens
// Verbose system prompt: ~200 tokens ($0.012 per 1000 calls on 70B)
const expensive = "You are a highly skilled AI assistant specializing in text classification. When given a piece of text, carefully analyze the sentiment, considering tone, word choice, connotation...";
// Concise system prompt: ~15 tokens ($0.001 per 1000 calls on 70B)
const cheap = "Classify sentiment: positive/negative/neutral. One word.";
// COST SAVINGS: Limit output tokens
async function cheapClassify(text: string): Promise<string> {
const result = await groq.chat.completions.create({
model: "llama-3.1-8b-instant",
messages: [
{ role: "system", content: "Reply with one word: positive, negative, or neutral." },
{ role: "user", content: text },
],
max_tokens: 3, // One word = 1-2 tokens
temperature: 0, // Deterministic = cacheable
});
return result.choices[0].message.content!.trim();
}
Step 3: Batch to Reduce Overhead
// Batch 10 items in one request instead of 10 separate requests
// Saves on per-request overhead and reduces RPM usage
async function batchClassify(items: string[]): Promise<string[]> {
const batchPrompt = items.map((item, i) => `${i + 1}. ${item}`).join("\n");
const result = await groq.chat.completions.create({
model: "llama-3.1-8b-instant",
messages: [
{
role: "system",
content: "Classify each numbered item as positive/negative/neutral. Reply with numbered results only.",
},
{ role: "user", content: batchPrompt },
],
max_tokens: items.length * 10,
temperature: 0,
});
// Parse numbered results
return result.choices[0].message.content!
.split("\n")
.map((line) => line.replace(/^\d+\.\s*/, "").trim())
.filter(Boolean);
}
// 10 items in 1 API call vs 10 API calls = ~90% reduction in overhead
Step 4: Cache Deterministic Requests
import { createHash } from "crypto";
const cache = new Map<string, { result: string; ts: number }>();
const CACHE_TTL = 60 * 60_000; // 1 hour
async function cachedCompletion(
messages: any[],
model: string
): Promise<string> {
const key = createHash("md5")
.update(JSON.stringify({ messages, model }))
.digest("hex");
const cached = cache.get(key);
if (cached && Date.now() - cached.ts < CACHE_TTL) {
return cached.result; // Zero cost, zero latency
}
const response = await groq.chat.completions.create({
model,
messages,
temperature: 0, // Required for cache consistency
});
const result = response.choices[0].message.content!;
cache.set(key, { result, ts: Date.now() });
return result;
}