We are building a context-aware translation agent that handles nuanced, domain-specific language, helping teams localize content without worrying about input length driving up costs. Because Oxlo.ai uses flat per-request pricing, you can pass long source documents in a single API call. See https://oxlo.ai/pricing for current plans.
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
- A few sample sentences to translate
Step 1: Initialize the Oxlo.ai client
First, instantiate the OpenAI SDK pointing at Oxlo.ai and run a quick smoke test to confirm connectivity. I use qwen-3-32b here because its multilingual reasoning handles translation nuances well.
import os
from openai import OpenAI
OXLO_API_KEY = os.getenv("OXLO_API_KEY", "YOUR_OXLO_API_KEY")
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key=OXLO_API_KEY)
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Translate 'Hello world' to Spanish."},
],
)
print(response.choices[0].message.content)
Step 2: Lock the system prompt
A strict system prompt keeps the model from adding explanations or hallucinating formatting. I keep this in a constant so every request behaves the same way.
SYSTEM_PROMPT = (
"You are an expert translator. Translate the text the user provides into the requested target language. "
"Preserve original tone, markdown formatting, and line breaks. "
"If a glossary is provided, use the exact mapped terms. "
"Output only the translation, with no extra commentary."
)
Step 3: Build the translation function
This helper accepts a glossary dictionary so you can enforce domain terms, like mapping OAuth token to token OAuth in Spanish. The glossary is injected into the user message, not the system prompt, so you can swap it per request.
def translate(text: str, target_lang: str, glossary: dict | None = None) -> str:
user_msg = f"Target language: {target_lang}\n\nText to translate:\n{text}"
if glossary:
terms = "\n".join(f"{k} -> {v}" for k, v in glossary.items())
user_msg = f"Glossary:\n{terms}\n\n{user_msg}"
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_msg},
],
)
return response.choices[0].message.content.strip()
Step 4: Add back-translation for quality control
When you do not have bilingual reviewers, back-translation is a lightweight ML trick to spot semantic drift. Translate to the target language, then translate the result back to English and compare.
def back_translate(text: str, source_lang: str) -> str:
prompt = f"Translate this text back to {source_lang}. Output only the translation.\n\n{text}"
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
],
)
return response.choices[0].message.content.strip()
source = "The user authentication flow failed due to an invalid OAuth token."
glossary = {"OAuth token": "token OAuth", "authentication flow": "flujo de autenticación"}
spanish = translate(source, "Spanish", glossary=glossary)
english_back = back_translate(spanish, "English")
print("Original:", source)
print("Spanish:", spanish)
print("Back:", english_back)
Step 5: Batch process a file
For production use, you will likely translate a dataset or a list of UI strings. This script reads a JSONL file, translates each line, and writes the results without loading everything into memory.
import json
def batch_translate(input_path: str, output_path: str, target_lang: str):
with open(input_path, "r", encoding="utf-8") as fin, \
open(output_path, "w", encoding="utf-8") as fout:
for line in fin:
record = json.loads(line)
glossary = record.get("glossary")
translated = translate(record["text"], target_lang, glossary=glossary)
out = {"id": record["id"], "translation": translated}
fout.write(json.dumps(out, ensure_ascii=False) + "\n")
# Example input.jsonl:
# {"id": 1, "text": "Retry connection", "glossary": {"Retry": "Reintentar"}}
# {"id": 2, "text": "Invalid credentials", "glossary": {}}
batch_translate("input.jsonl", "output.jsonl", "Spanish")
Run it
Here is a complete script that wires everything together. Save it as translate_agent.py, set your OXLO_API_KEY, and run python translate_agent.py.
import os
import json
from openai import OpenAI
OXLO_API_KEY = os.getenv("OXLO_API_KEY", "YOUR_OXLO_API_KEY")
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key=OXLO_API_KEY)
SYSTEM_PROMPT = (
"You are an expert translator. Translate the text the user provides into the requested target language. "
"Preserve original tone, markdown formatting, and line breaks. "
"If a glossary is provided, use the exact mapped terms. "
"Output only the translation, with no extra commentary."
)
def translate(text: str, target_lang: str, glossary: dict | None = None) -> str:
user_msg = f"Target language: {target_lang}\n\nText to translate:\n{text}"
if glossary:
terms = "\n".join(f"{k} -> {v}" for k, v in glossary.items())
user_msg = f"Glossary:\n{terms}\n\n{user_msg}"
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_msg},
],
)
return response.choices[0].message.content.strip()
def back_translate(text: str, source_lang: str) -> str:
prompt = f"Translate this text back to {source_lang}. Output only the translation.\n\n{text}"
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
],
)
return response.choices[0].message.content.strip()
if __name__ == "__main__":
source = "The user authentication flow failed due to an invalid OAuth token."
glossary = {"OAuth token": "token OAuth", "authentication flow": "flujo de autenticación"}
spanish = translate(source, "Spanish", glossary=glossary)
english_back = back_translate(spanish, "English")
print("Original :", source)
print("Spanish :", spanish)
print("Back :", english_back)
Example output:
Original : The user authentication flow failed due to an invalid OAuth token.
Spanish : El flujo de autenticación del usuario falló debido a un token OAuth no válido.
Back : The user authentication flow failed due to an invalid OAuth token.
Wrap-up and next steps
You now have a working translation agent that enforces terminology, checks its own work with back-translation, and scales to batch jobs. A concrete next step is to persist corrections in a local SQLite cache and feed them back into the glossary so the agent improves over time. Another is to wire this script into a CI pipeline to automatically localize documentation on every release, leveraging Oxlo.ai flat per-request pricing to keep long document batches predictable.
Top comments (0)