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...
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.
GROQ_API_KEY environment variable — the groq-sdk client reads it automatically (new Groq()).groq-sdk package installed (npm install groq-sdk).| Model | Input | Output |
|---|---|---|
llama-3.1-8b-instant |
~$0.05 | ~$0.08 |
llama-3.3-70b-versatile |
~$0.59 | ~$0.79 |
llama-3.3-70b-specdec |
~$0.59 | ~$0.99 |
meta-llama/llama-4-scout-17b-16e-instruct |
~$0.11 | ~$0.34 |
whisper-large-v3-turbo |
~$0.04/hr | — |
Check current pricing at groq.com/pricing.
Apply these six levers in order. Each compounds on the last — routing alone is the biggest win (~12x), and caching plus batching halve the remainder. The lean skeleton below shows the routing core; the full code for every step lives in references/implementation.md.
llama-3.1-8b-instant; reasoning/code review/chat → llama-3.3-70b-versatile; vision → llama-4-scout).max_tokens so a one-word answer never bills for a paragraph.temperature: 0, hash identical prompts into a cache for zero-cost, zero-latency repeat hits.import Groq from "groq-sdk";
const groq = new Groq(); // reads GROQ_API_KEY
const ROUTING = {
classification: "llama-3.1-8b-instant", // ~$0.05/M
reasoning: "llama-3.3-70b-versatile", // ~$0.59/M
};
const getModel = (useCase: string) =>
ROUTING[useCase] || "llama-3.1-8b-instant";
// Classification on 8B vs 70B = 12x savings
See references/implementation.md for the complete routing table, token-minimization, batching, caching, usage-tracking, and console-limit code.
Applying the workflow produces:
getModel(useCase)) that resolves every call to the cheapest fit model.UsageRecord rows (timestamp, model, prompt/completion tokens, estimated cost) accumulated per call.dailyCostReport() returning { totalCost, byModel }, e.g. { totalCost: "$2.0000", byModel: { "llama-3.1-8b-instant": "$2.0000" } }.Batch three items in a single call using the batchClassify helper from
references/implementation.md:
const labels = await batchClassify([
"Loved it, five stars",
"Broke on day one",
"It was fine, nothing special",
]);
// -> ["positive", "negative", "neutral"] (1 API call instead of 3)
For the full 100,000-message cost walkthrough and a stacked routing + caching + tracking pipeline, see references/examples.md.
| Issue | Cause | Solution |
|---|---|---|
| Costs higher than expected | 70B for simple tasks | Route classification/extraction to 8B |
| Spending cap hit | Budget exhausted | Increase cap or reduce volume |
| Cache not effective | Unique prompts | Normalize prompts before caching |
| Rate limits causing retries | RPM cap hit | Batch requests, spread across time |
groq-reference-architecture skill.