We are building a technical document translator that preserves Markdown, code blocks, and domain terminology across languages. It helps developer-relations and localization teams turn long English docs into polished Spanish, Chinese, or Japanese versions without managing token budgets per paragraph.
What you'll need
- Python 3.10 or newer
- The OpenAI SDK:
pip install openai - An Oxlo.ai API key from https://portal.oxlo.ai
Step 1: Initialize the Oxlo.ai client
I always start with a minimal connectivity check. Oxlo.ai exposes an OpenAI-compatible endpoint, so the only difference from a standard OpenAI setup is the base URL and your Oxlo.ai key.
from openai import OpenAI
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")
# sanity check
response = client.chat.completions.create(
model="llama-3.3-70b",
messages=[{"role": "user", "content": "Reply OK"}],
max_tokens=5,
)
print(response.choices[0].message.content)
Step 2: Lock in the system prompt
The system prompt is the contract. It tells the model to act as a translation agent, not a chatbot. I keep it strict: no explanations, preserve formatting, respect the glossary.
SYSTEM_PROMPT = """You are a technical translation agent.
Rules:
1. Preserve all Markdown formatting, code blocks, inline code, and URLs exactly.
2. Match the original tone. Formal stays formal, casual stays casual.
3. Apply the glossary terms strictly. If no glossary is provided, use consistent technical translations.
4. Output only the translated text. Do not add introductions, summaries, or notes."""
Step 3: Build the translate function
Next I wrap the API call in a clean Python function. I use qwen-3-32b because Oxlo.ai lists it as a strong multilingual model for reasoning and agent workflows. A low temperature keeps the output deterministic.
def translate(text: str, target_lang: str, glossary: dict | None = None) -> str:
glossary_block = ""
if glossary:
glossary_block = "Glossary:\n" + "\n".join(
f"{en} -> {target}" for en, target in glossary.items()
)
user_msg = f"Translate the following text to {target_lang}.\n{glossary_block}\n\nText:\n{text}"
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_msg},
],
temperature=0.3,
)
return response.choices[0].message.content.strip()
Step 4: Chunk long documents without token anxiety
Real docs are not single paragraphs. I split on double newlines and translate each chunk separately. On token-based providers, long inputs inflate costs linearly. Oxlo.ai uses flat per-request pricing, so each chunk costs the same whether it is fifty words or five hundred. For long-context workloads, that predictability matters. See https://oxlo.ai/pricing for plan details.
def translate_document(paragraphs: list[str], target_lang: str, glossary: dict | None = None) -> list[str]:
results = []
for i, para in enumerate(paragraphs, 1):
if not para.strip():
results.append("")
continue
out = translate(para, target_lang, glossary)
results.append(out)
print(f"Chunk {i}/{len(paragraphs)} complete")
return results
Step 5: Wire up the CLI test
I feed the agent two short technical paragraphs and a Spanish glossary. This is the moment of truth.
if __name__ == "__main__":
paragraphs = [
"The inference engine leverages pipeline parallelism to shard layers across multiple GPUs.",
"Ensure your `config.yaml` specifies the correct `batch_size` before deployment.",
]
glossary = {
"pipeline parallelism": "paralelismo de tubería",
"batch_size": "tamaño de lote",
"inference engine": "motor de inferencia",
}
translated = translate_document(paragraphs, "Spanish", glossary)
for original, trans in zip(paragraphs, translated):
print(f"Original:\n{original}\n\nTranslated:\n{trans}\n{'-' * 40}")
Run it
Save everything to translator.py, export your key, and execute.
export OXLO_API_KEY="sk-oxlo.ai-..."
python translator.py
Expected output:
Chunk 1/2 complete
Chunk 2/2 complete
Original:
The inference engine leverages pipeline parallelism to shard layers across multiple GPUs.
Translated:
El motor de inferencia aprovecha el paralelismo de tubería para dividir las capas entre varias GPUs.
----------------------------------------
Original:
Ensure your `config.yaml` specifies the correct `batch_size` before deployment.
Translated:
Asegúrese de que su `config.yaml` especifique el `tamaño de lote` correcto antes del despliegue.
----------------------------------------
Next steps
First, add an automatic back-translation check. Send the Spanish output through the same agent with a reverse glossary and verify semantic similarity against the English source. Second, hook this script into a GitHub Action so every pull request that touches your docs folder triggers a preview translation job on Oxlo.ai.
Top comments (0)