Skip to main content

AI Config

OpenAI or Anthropic for auto-generating descriptions — provider choice, model selection, cost ceilings and caching strategy.

Why this matters

This is the smallest config file in the project, but it controls the most expensive runtime behaviour: every LLM call costs real money, and a misconfigured retry loop can drain $100/day before you notice. The good news is that the right defaults — gpt-4o-mini or claude-haiku-4-5, capped tokens, modest temperature — keep AI generation under $0.05 per project description even at production scale.

This page documents the toggles, recommends models for different niches, and lays out the cost-control patterns that prevent surprises. If you're enabling AI for the first time, read Common pitfalls before deploying — three of the four issues are budget killers.

File: config/ai.config.ts

When AI is on, users get "Generate with AI" buttons in the submit flow — the LLM writes descriptions, suggests categories, and fills SEO metadata from a URL.

Two switches to flip

You need both ai: true in features.config.ts and a valid AI_API_KEY. Either missing → AI silently skips.

Supported providers

ProviderWhere to get a key
OpenAIplatform.openai.com → API keys
Anthropicconsole.anthropic.com → API keys

Setup

1. Turn on the feature flag

config/features.config.ts
export const featuresConfig = {
  // ...
  ai: true,
};

2. Add env vars

.env.local
AI_PROVIDER=openai          # or 'anthropic'
AI_MODEL=gpt-4o-mini        # or 'claude-haiku-4-5' etc.
AI_API_KEY=sk-...

Restart the dev server.

3. Verify

Go to /submit → fill in a project URL → click "Generate description with AI". If nothing happens, check the dev console / network tab for errors.

The config file

config/ai.config.ts
export const aiConfig = {
  provider: (process.env.AI_PROVIDER as 'openai' | 'anthropic') || 'openai',
  model: process.env.AI_MODEL || 'gpt-4o-mini',
  apiKey: process.env.AI_API_KEY || '',
  maxTokens: 1000,
  temperature: 0.7,
};
FieldDefaultWhat it does
provideropenaiProvider. Must match your key.
modelgpt-4o-miniModel ID. See provider's docs for available models.
apiKeyPulled from AI_API_KEY.
maxTokens1000Max response length.
temperature0.70 = deterministic, 1 = creative. Good middle ground for descriptions.

For directory descriptions (short, factual), cheap fast models win:

ProviderModelInput / Output cost
OpenAIgpt-4o-minicheapest GPT-4-class
Anthropicclaude-haiku-4-5cheap and fast
OpenAIgpt-4obetter quality, more expensive
Anthropicclaude-sonnet-4-5best quality Anthropic

Start with the cheap one. Only upgrade if outputs are noticeably worse.

Cost control

  • Each description generation is ~300-500 tokens → fractions of a cent per request.
  • Rate-limit AI endpoints in lib/rate-limit.ts to prevent abuse.
  • Consider caching generated descriptions by URL so re-submissions don't re-pay.

A real-world walkthrough: choosing a model for a 200-listing AI directory

You're operating an AI-tools directory and expect to publish 30–50 new listings per week. Each listing needs a description (~80 words) and category suggestions. Math:

  • 30 listings/week × 4 weeks = ~120 generations/month
  • Each generation: ~600 input tokens (URL fetched), ~200 output tokens
  • gpt-4o-mini at current pricing: roughly $0.04 per generation → $5/month
  • claude-haiku-4-5: similar
  • gpt-4o (premium): roughly $0.40 per generation → $50/month

Start with gpt-4o-mini. Generate 10 sample descriptions, manually review for tone and accuracy. If quality is acceptable (it usually is for short marketing copy), stay there. If you find consistent issues — tone too generic, missing nuance for a specific niche — try claude-haiku-4-5 next. Only escalate to gpt-4o or claude-sonnet-4-6 if both small models struggle.

Set a $50 monthly cap in your provider dashboard regardless. That's a 10× safety margin against runaway loops, and it triggers an email alert before any meaningful damage.

Common pitfalls

  • Setting temperature: 1.0 to "make it creative". For directory descriptions, you want consistent voice across listings. 0.6–0.8 is the right range. 1.0+ produces variability that reads as inconsistency at scale.
  • Forgetting the provider's spend cap. Both OpenAI and Anthropic let you set hard monthly spend limits in the dashboard. Set one. A misconfigured loop in your generation code can run unbounded calls until the limit kicks in.
  • Hardcoding the API key in the config file. Always pull from env. The config file gets committed; the env file shouldn't. If you accidentally commit a key, rotate it immediately at the provider — it's already scraped by GitHub bots.
  • Mixing providers in one deploy. AI_PROVIDER=openai with AI_MODEL=claude-haiku-4-5 silently fails — the OpenAI client doesn't know about Anthropic models. Make sure both env vars are set together, and test locally before pushing.
  • Caching too aggressively. Caching by URL alone means a project that updates its tagline keeps showing the stale AI description for months. Cache TTL of 7 days is a reasonable default; expire on user-triggered regenerate.

FAQ

Can I run a local model (Ollama, vLLM) for free generations?

Yes — Ollama exposes an OpenAI-compatible API on :11434/v1. Set AI_PROVIDER=openai and AI_BASE_URL=http://localhost:11434/v1, plus a model like llama3.1:8b. Quality of small open-source models is noticeably worse for short marketing copy than the hosted commercial models — usable for prototyping, not great for production.

How do I switch from OpenAI to Anthropic without code changes?

Update three env vars: AI_PROVIDER=anthropic, AI_MODEL=claude-haiku-4-5, AI_API_KEY=<anthropic-key>. Restart the server. The internal lib/ai.ts adapter handles both providers transparently — no other code changes needed.

Should I let users see the AI was used?

Optional, but transparency is increasingly the norm. Add a small "Generated with AI — review before submitting" notice next to the Generate button. This sets expectations and reduces support tickets when output isn't perfect.

See also