AI Generation
The CMS can generate up to ten long-form Nepal travel articles in one Claude Messages Batch request. Each result becomes a draft post for an editor to review; nothing is published automatically.
Flow
/admin/generate
│ topics + focus + optional prompt template
▼
POST /api/generate ──▶ Claude Batch API ──▶ GenerationBatch (processing)
│
▼
poll GET /api/generate/:id until Claude reports "ended"
│
▼
POST /api/generate/:id/process ──▶ draft Posts + batch completed/partial/failedSubmitting a batch
POST /api/v1/../api/generate
Content-Type: application/json
{
"topics": ["Everest Base Camp in December", "Annapurna Circuit permit guide"],
"focus": "budget travellers",
"promptContent": "optional override for the system prompt"
}| Field | Rule |
|---|---|
topics | Required array; 1–10 items |
focus | Optional angle inserted into the dynamic prompt |
promptContent | Optional full system-prompt override |
Each topic becomes one batch request with custom_id equal to topic-<index>. The response is created with status = "processing" and a batchId returned by Anthropic. If the initial submit fails, the route returns 500; no batch row is written.
Prompt construction
The system prompt is split into two cacheable blocks:
- The stable guide prompt (or the editor's
promptContent), marked withcache_control: ephemeral. - A per-topic prompt containing the focus, the internal link bank, and strict JSON output instructions.
The built-in prompt is opinionated about Nepalese travel content:
- First-person editorial "we" voice, no fabricated personal memories.
- Permit and fee accuracy (TIMS, ACAP, MCAP, Sagarmatha, restricted-area permits).
- Current operational details: Ramechhap flights, guide ratios, altitude and insurance guidance, cash and connectivity realities.
- A mandatory FAQ section and a markdown table where the topic supports one.
- Aggressive bolding of prices, elevations, durations and permit names.
- A marketplace call to action at the end.
- Banned filler words such as "delve", "tapestry" and "in conclusion".
Internal links are restricted to the link bank fetched from GET /api/links/bank. The model is told to use only those relative URLs and to return the links it actually used.
Processing
POST /api/generate/:id/process is idempotent for finished batches:
- Loads the
GenerationBatch. - Returns existing posts if the batch is already
completedorpartial. - Asks Claude for the batch status. If it is not
ended, returns the current status and no posts. - Streams results and parses each successful text block as JSON.
- Creates a draft post per success with
canonicalUrl = "/<slug>", no category/country/city, and the batch id stored inmeta.batchId. - Sets the batch to
completed,partialorfailedand records failedcustom_ids inerror.
Generated posts start with the slug <slugified-title>-<counter>, where the counter is process-local. They have no canonical path scoping until an editor adds a category and destination, so review the URL before publishing.
JSON parsing
Claude is asked for raw JSON, but the parser is defensive. It tries, in order:
JSON.parseon the raw text.- Stripping a markdown code fence.
- Slicing from the first
{to the last}. - Repairing truncated JSON by closing an unterminated string and balancing brackets/braces.
If all fail, that topic is marked failed and the batch becomes partial (or failed when every topic fails).
Rate limits
Batch submission and status calls retry HTTP 429 up to five times with exponential backoff and jitter. Other errors surface immediately.
Batch model
model GenerationBatch {
id String @id @default(cuid())
batchId String @unique
topics String[]
focus String?
status String @default("pending")
error String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}Observed statuses: pending, processing, completed, partial, failed. error holds the failed custom_ids, not a stack trace.
Prompt templates
Prompt templates are .md files on disk, not database rows.
| Method | Endpoint | Permission |
|---|---|---|
| GET | /api/prompts | prompts |
| POST | /api/prompts | prompts |
| PATCH | /api/prompts/:name | prompts |
| DELETE | /api/prompts/:name | prompts |
Filenames are restricted to letters, numbers, hyphens and underscores plus .md. Submitting a duplicate name returns 400. Because templates live on disk, they are per-instance and should be treated as deployment artifacts if more than one CMS instance runs.
Configuration
| Variable | Purpose |
|---|---|
CLAUDE_API_KEY or ANTHROPIC_API_KEY | API key; either is accepted |
CLAUDE_MODEL | Model id used for every request |
The CMS code falls back to claude-opus-4-7 when CLAUDE_MODEL is unset. The API's .env.example still lists an older Sonnet id for its own unrelated Claude integration, so set CLAUDE_MODEL explicitly rather than relying on either default.
Operational notes
- Batch generation is asynchronous; the admin must poll status and then call
process. There is no background cron that ingests finished batches. - A failed topic does not block the successful ones.
- Generated content is untrusted input: review claims, fees and links before publishing. The prompt reduces invented facts but does not prove them.
- Every post created by processing goes through the normal editor save path afterwards, which sanitises HTML, strips invalid links and builds the TOC.