Skip to content

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 ​

text
/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/failed

Submitting a batch ​

http
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"
}
FieldRule
topicsRequired array; 1–10 items
focusOptional angle inserted into the dynamic prompt
promptContentOptional 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:

  1. The stable guide prompt (or the editor's promptContent), marked with cache_control: ephemeral.
  2. 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:

  1. Loads the GenerationBatch.
  2. Returns existing posts if the batch is already completed or partial.
  3. Asks Claude for the batch status. If it is not ended, returns the current status and no posts.
  4. Streams results and parses each successful text block as JSON.
  5. Creates a draft post per success with canonicalUrl = "/<slug>", no category/country/city, and the batch id stored in meta.batchId.
  6. Sets the batch to completed, partial or failed and records failed custom_ids in error.

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:

  1. JSON.parse on the raw text.
  2. Stripping a markdown code fence.
  3. Slicing from the first { to the last }.
  4. 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 ​

prisma
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.

MethodEndpointPermission
GET/api/promptsprompts
POST/api/promptsprompts
PATCH/api/prompts/:nameprompts
DELETE/api/prompts/:nameprompts

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 ​

VariablePurpose
CLAUDE_API_KEY or ANTHROPIC_API_KEYAPI key; either is accepted
CLAUDE_MODELModel 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.

Built with VitePress