Configure Groq across development, staging, and production environments.
Use when setting up multi-environment deployments, configuring per-environment secrets,
or implementing environment-specific Groq configurations.
Trigger with phrases like "groq environments", "groq staging",
"groq dev prod", "groq environment setup", "groq config by env".
Configure Groq API access across development, staging, and production with the right model, rate limit strategy, and secret management per environment. Key insight: use llama-3.1-8b-instant in development (cheapest, fastest), match production model in staging, and harden production with retries and fallbacks.
Environment Strategy
Environment
API Key Source
Default Model
Retry
Logging
Development
.env.local
llama-3.1-8b-instant
1
Verbose
Staging
CI/CD secrets
llama-3.3-70b-versatile
3
Standard
Production
Secret manager
llama-3.3-70b-versatile
5
Structured
Instructions
Step 1: Configuration Module
// config/groq.ts
import Groq from "groq-sdk";
interface GroqEnvConfig {
apiKey: string;
model: string;
maxTokens: number;
temperature: number;
maxRetries: number;
timeout: number;
logRequests: boolean;
}
const configs: Record<string, GroqEnvConfig> = {
development: {
apiKey: process.env.GROQ_API_KEY || "",
model: "llama-3.1-8b-instant", // Cheapest, fastest for iteration
maxTokens: 512,
temperature: 0.7,
maxRetries: 1,
timeout: 15_000,
logRequests: true, // Verbose in dev
},
staging: {
apiKey: process.env.GROQ_API_KEY_STAGING || process.env.GROQ_API_KEY || "",
model: "llama-3.3-70b-versatile", // Match production model
maxTokens: 2048,
temperature: 0.3,
maxRetries: 3,
timeout: 30_000,
logRequests: false,
},
production: {
apiKey: process.env.GROQ_API_KEY_PROD || process.env.GROQ_API_KEY || "",
model: "llama-3.3-70b-versatile", // Quality model
maxTokens: 2048,
temperature: 0.3,
maxRetries: 5, // More retries in prod
timeout: 30_000,
logRequests: false,
},
};
function getEnv(): string {
return process.env.NODE_ENV || "development";
}
export function getGroqConfig(): GroqEnvConfig {
const env = getEnv();
const config = configs[env] || configs.development;
if (!config.apiKey) {
throw new Error(
`GROQ_API_KEY not set for ${env}. ` +
(env === "development"
? "Copy .env.example to .env.local and add your key from console.groq.com/keys"
: `Set GROQ_API_KEY_${env.toUpperCase()} in your secret manager`)
);
}
return config;
}
let _client: Groq | null = null;
export function getGroqClient(): Groq {
if (!_client) {
const config = getGroqConfig();
_client = new Groq({
apiKey: config.apiKey,
maxRetries: config.maxRetries,
timeout: config.timeout,
});
}
return _client;
}