← Blog

Claude API Translation App: A Build Guide

2026-11-13 · 5 min read · SubToAPI Team

Can you build a translation app with the Claude API?

Yes, and for most use cases it works better than dedicated translation APIs, especially when you need context-aware output, tone control, or translation of domain-specific text like legal contracts, marketing copy, or support tickets. Claude handles idioms, formality levels, and formatting (Markdown, JSON, HTML) far more reliably than classic statistical translation engines, because it's reasoning about meaning rather than doing word-for-word substitution.

This guide walks through the actual build: prompt structure, request format, streaming for long documents, batch translation, and the production details people usually get wrong (language detection, cost control, formatting preservation).

Why use an LLM instead of a traditional translation API

Traditional MT (machine translation) APIs are fast and cheap for short, generic strings, but they struggle with:

Claude handles all of these because you're giving it a prompt, not a fixed API call with a target_lang parameter and nothing else. The tradeoff is latency and cost per request, which is why batching and caching matter once you're past the prototype stage.

Core request structure

A translation request to Claude is a standard messages call. Keep the system prompt narrow and deterministic — you want consistent output, not creative liberty.

curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "system": "You are a professional translator. Translate the user text from English to French. Preserve formatting, line breaks, and placeholders like {name}. Return only the translated text, no explanations.",
    "messages": [
      {"role": "user", "content": "Hi {name}, your order #{order_id} has shipped and will arrive by {date}."}
    ]
  }'

Two details matter here:

  1. "Return only the translated text" — without this, Claude will sometimes add "Here is the translation:" before the output, which breaks programmatic parsing.
  2. Explicit placeholder instructions — if your source strings contain template variables ({name}, %s, {{count}}), tell the model to leave them untouched. Test this thoroughly; it's the most common bug in translation apps built on LLMs.

Handling structured content (JSON, HTML, Markdown)

If you're translating UI strings or CMS content, send the whole structure and ask Claude to preserve it:

{
  "system": "Translate all string values in this JSON from English to German. Keep keys, numbers, booleans, and null values unchanged. Return valid JSON only.",
  "messages": [
    {"role": "user", "content": "{\"welcome\": \"Welcome back\", \"cta\": \"Get started\", \"count\": 3}"}
  ]
}

For larger files, don't send one giant JSON blob per request — split by logical section and run requests in parallel. This keeps individual responses small and reduces the chance of truncated or malformed JSON.

Streaming for long documents

For document translation (contracts, articles, transcripts), streaming matters for perceived performance. Rather than waiting 20+ seconds for a 3,000-word document, stream tokens to the client as they're generated:

const response = await fetch("https://api.anthropic.com/v1/messages", {
  method: "POST",
  headers: {
    "x-api-key": process.env.ANTHROPIC_API_KEY,
    "anthropic-version": "2023-06-01",
    "content-type": "application/json",
  },
  body: JSON.stringify({
    model: "claude-sonnet-4-5",
    max_tokens: 4096,
    stream: true,
    system: "Translate the following document from English to Japanese. Preserve paragraph breaks and headings.",
    messages: [{ role: "user", content: documentText }],
  }),
});

Parse the SSE stream and render tokens as they arrive. This is the same pattern used for chat UIs — translation output just happens to be deterministic text instead of conversational replies.

Language detection without a separate API

You don't need a separate language-detection service for most apps. Ask Claude to detect and translate in one pass:

{
  "system": "Detect the source language automatically. Translate the text to English. If the text is already in English, return it unchanged. Return only the translated text.",
  "messages": [{"role": "user", "content": "Bonjour, comment allez-vous?"}]
}

This removes a dependency and a round trip, at the cost of a slightly less predictable system prompt — test it against your actual input distribution (chat messages, reviews, support tickets) before relying on it in production.

Cost and latency control at scale

Translation apps tend to send a lot of small requests, which is the worst pattern for LLM APIs cost-wise (fixed overhead per call). A few practical fixes:

Exposing this as an internal or external API

If you're building the translation feature behind your own app (a SaaS product, a CMS plugin, a support tool), you'll eventually need to manage API keys, rate limits, and usage visibility for whoever consumes this internally — your frontend team, a mobile app, or external customers.

This is where SubToAPI is useful: it turns your Claude access into a standard HTTPS API with its own sub_live_... keys, so you can issue scoped keys per environment or per customer, track usage per key, and avoid wiring raw Anthropic credentials into client-facing services. The request format is the same /v1/messages shape shown above — see the quickstart and messages docs — so moving from a direct integration to a managed one is mostly a base-URL and key swap, including streaming if your translation UI needs token-by-token output.

questions

Can Claude translate between any two languages, not just to/from English? Yes. Specify the source and target languages explicitly in the system prompt (or let it auto-detect the source). Claude handles non-English-to-non-English pairs, though quality varies by language pair just as with any translation system — test your specific pairs.

How do I stop Claude from adding extra commentary to translations? Add an explicit instruction like "return only the translated text, no explanations or notes" in the system prompt, and keep temperature low. If you need machine-parseable output, ask for a specific format (plain text, JSON) and validate the response before using it.

Is the Claude API cheaper than dedicated translation services for high volume? It depends on string length and volume. For short, repetitive strings (UI labels), dedicated MT APIs are usually cheaper per character. For longer, context-sensitive content, Claude's quality advantage often outweighs the per-token cost, especially once you add caching and batching.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →