DEV Community

Cover image for Translating ten evergreen guides into five languages with one batch API call
MiY
MiY

Posted on Edited on

Translating ten evergreen guides into five languages with one batch API call

WFile publishes public-information news in Japanese, Korean, Thai, Vietnamese, Indonesian, and English. We recently added ten evergreen guides: city hall procedures, emergency contacts, source reliability, dates and numbers, and similar reference pages.

Ten guides does not sound large until you multiply by six languages. That is 60 guide pages, plus six language hubs. The first version translated each target language in a separate request: five requests per guide, fifty requests for the set.

We changed it to one batch request per guide.

The batch shape

Each guide has a title, description, and Markdown body. The translator receives the source once:

{
  "sourceLanguage": "English",
  "targetLanguages": [
    { "code": "ja", "label": "日本語" },
    { "code": "ko", "label": "한국어" },
    { "code": "th", "label": "ไทย" },
    { "code": "vi", "label": "Tiếng Việt" },
    { "code": "id", "label": "Bahasa Indonesia" }
  ],
  "title": "...",
  "summary": "...",
  "body": "..."
}
Enter fullscreen mode Exit fullscreen mode

The model returns one JSON object keyed by language:

{
  "translations": {
    "ja": { "title": "...", "summary": "...", "body": "..." },
    "ko": { "title": "...", "summary": "...", "body": "..." },
    "th": { "title": "...", "summary": "...", "body": "..." },
    "vi": { "title": "...", "summary": "...", "body": "..." },
    "id": { "title": "...", "summary": "...", "body": "..." }
  }
}
Enter fullscreen mode Exit fullscreen mode

The model we use has a 1M context window and a 384K output ceiling, so a single guide in five languages fits comfortably. The important part is not the ceiling. It is that the source body and the instructions are sent once, not five times.

The implementation

The batch function is deliberately small:

export async function translateArticleBatch({
  sourceTitle,
  sourceSummary,
  sourceBody,
  sourceLang,
  targetLangs,
}) {
  const targets = [...new Set(targetLangs)].filter(
    (lang) => lang && lang !== sourceLang,
  );

  const payload = JSON.stringify({
    sourceLanguage: sourceLang,
    targetLanguages: targets,
    title: sourceTitle,
    summary: sourceSummary,
    body: sourceBody,
  });

  const result = await callModel({
    system: TRANSLATE_BATCH_SYSTEM,
    user: payload,
    task: "translate_batch",
    batchId: randomId(8),
    maxTokens: 32000,
  });

  return parseBatchTranslations(result.text, targets);
}
Enter fullscreen mode Exit fullscreen mode

The parser is intentionally forgiving about the outer object and strict about the fields:

function parseBatchTranslations(text, targetLangs) {
  const data = JSON.parse(text.slice(text.indexOf("{"), text.lastIndexOf("}") + 1));
  const translations = data.translations || data;
  const result = {};

  for (const lang of targetLangs) {
    const item = translations[lang];
    if (!item?.title || !item?.body) continue;
    result[lang] = {
      title: item.title.slice(0, 200),
      summary: String(item.summary || "").slice(0, 500),
      body: item.body.slice(0, 60000),
    };
  }

  return result;
}
Enter fullscreen mode Exit fullscreen mode

Failure isolation

A batch call is a single point of failure, so it cannot be the only path.

The caller does this:

  1. Send one batch request for all target languages.
  2. For each language, use the returned item if present.
  3. If the batch failed, or a language is missing, call the existing single-language translator for that language only.
  4. Only record a language-level error if the fallback also fails.

That means a malformed Thai response does not discard a valid Japanese or Korean translation.

Every batch gets a batch_id in the usage log. The log also stores provider, model, input tokens, output tokens, duration, success, and error. We do not log the prompt or article body.

What the numbers looked like

The production usage table showed 14 batch calls during the rollout and tests:

  • average duration: about 46 seconds
  • fastest: 7.9 seconds
  • slowest: 63.4 seconds
  • total input tokens: 13,575
  • total output tokens: 149,282

The input-token number is the interesting one. Five separate translations would have sent the source article five times. The batch sends it once. Output tokens still scale with the number of languages, but output is the actual translated content; the duplicated input is gone.

The SEO side

The guides are not just translated articles. Each guide page has:

  • localized title and description
  • six-language hreflang and x-default
  • canonical URL
  • WebPage and BreadcrumbList structured data
  • a sitemap entry
  • links to related news
  • links back from relevant news articles

The guide hub is linked from the site footer, and the article pages link to relevant guides by country and topic. The result is a small topic cluster rather than 60 isolated translations.

What I would change next

The current batch is one guide per request. A larger archive could batch two or three guides at once, but that changes failure isolation: one malformed item should not discard the whole batch. It needs a top-level batch_id, per-item IDs, and a fallback path per item.

That is a larger refactor, so the current version keeps one guide per batch and uses the saved input tokens where it can.

The guides are here:

Top comments (1)

Collapse
 
citedy profile image
Dmitry Sergeev •

We need to produce a short YouTube comment, casual, one sentence or two, maybe fragment. Must start with a specific reaction/question about the video. Not generic praise. Should be like a peer. No URLs, no promotion. Must follow style guidelines. Avoid punctuation errors. Should not use em-dash, en-dash, smart quotes. Use straight quotes only if needed. Should be short. Possible comment: "lol the batch API call saved so many tokens, can you tweak it for dynamic page generation?" Or "ng