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.
You need both ai: true in features.config.ts and a valid AI_API_KEY. Either missing → AI silently skips.
Supported providers
| Provider | Where to get a key |
|---|---|
| OpenAI | platform.openai.com → API keys |
| Anthropic | console.anthropic.com → API keys |
Setup
1. Turn on the feature flag
export const featuresConfig = {
// ...
ai: true,
};2. Add env vars
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
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,
};| Field | Default | What it does |
|---|---|---|
provider | openai | Provider. Must match your key. |
model | gpt-4o-mini | Model ID. See provider's docs for available models. |
apiKey | — | Pulled from AI_API_KEY. |
maxTokens | 1000 | Max response length. |
temperature | 0.7 | 0 = deterministic, 1 = creative. Good middle ground for descriptions. |
Recommended models
For directory descriptions (short, factual), cheap fast models win:
| Provider | Model | Input / Output cost |
|---|---|---|
| OpenAI | gpt-4o-mini | cheapest GPT-4-class |
| Anthropic | claude-haiku-4-5 | cheap and fast |
| OpenAI | gpt-4o | better quality, more expensive |
| Anthropic | claude-sonnet-4-5 | best 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.tsto 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-miniat current pricing: roughly $0.04 per generation → $5/monthclaude-haiku-4-5: similargpt-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.0to "make it creative". For directory descriptions, you want consistent voice across listings.0.6–0.8is 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=openaiwithAI_MODEL=claude-haiku-4-5silently 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
- AI Features — where AI is used in the app
- Features Config — toggling AI on/off
- API Reference Overview — guarding the AI route