We are going to build a context-aware translation agent that runs in the terminal and handles terminology, tone, and long documents. It is useful for engineering teams automating localization pipelines or content ops translating large volumes of text without surprise token bills.
What you'll need
- Python 3.10 or newer.
- An Oxlo.ai API key from https://portal.oxlo.ai. We will use the OpenAI-compatible client, so install it with
pip install openai. - A few minutes to wire up the script.
I am using Oxlo.ai because its request-based pricing keeps costs flat even when I feed it entire pages of text in a single call. That makes batch translation jobs predictable.
Step 1: Initialize the Oxlo.ai client
Create a file named translate.py and set up the client. I use llama-3.3-70b as the workhorse because it handles multilingual tasks reliably. If you need deeper reasoning for ambiguous technical passages, qwen-3-32b and kimi-k2.6 are also available on Oxlo.ai.
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY"
)
MODEL = "llama-3.3-70b"
Step 2: Craft the system prompt
The system prompt is the core of the translation model. It enforces formatting fidelity, glossary compliance, and tone control.
SYSTEM_PROMPT = """You are a professional translation engine.
Rules:
1. Translate the user's text from the specified source language to the target language.
2. Preserve all markdown, code blocks, and formatting exactly.
3. Honor the provided glossary strictly. Do not substitute synonyms for glossary terms.
4. Match the requested tone: formal, casual, or technical.
5. If a phrase is ambiguous, choose the most likely meaning for a software documentation context.
6. Output only the translated text. Do not add explanations or quotation marks around the result."""
Step 3: Build the translation function
This function assembles the user payload as JSON so the model can reference metadata cleanly, then calls Oxlo.ai.
import json
def translate(text: str, source_lang: str, target_lang: str,
tone: str = "formal", glossary: dict | None = None) -> str:
glossary = glossary or {}
user_message = json.dumps({
"source_language": source_lang,
"target_language": target_lang,
"tone": tone,
"glossary": glossary,
"text": text
}, ensure_ascii=False)
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message},
],
)
return response.choices[0].message.content.strip()
Step 4: Chunk long documents
Long files can degrade at the edges or exceed context windows. I split by double newline to keep paragraphs intact, then translate each chunk. Because Oxlo.ai bills per request, splitting a long document into several calls still gives me a flat per-request rate that is easy to forecast.
def chunk_text(text: str, max_chars: int = 4000) -> list[str]:
paragraphs = text.split("\n\n")
chunks = []
current = ""
for p in paragraphs:
if len(current) + len(p) < max_chars:
current += "\n\n" + p if current else p
else:
if current:
chunks.append(current)
current = p
if current:
chunks.append(current)
return chunks
def translate_document(text: str, source_lang: str, target_lang: str,
tone: str = "formal", glossary: dict | None = None) -> str:
chunks = chunk_text(text)
translated = []
for i, chunk in enumerate(chunks, 1):
print(f"Translating chunk {i}/{len(chunks)}...")
result = translate(chunk, source_lang, target_lang, tone, glossary)
translated.append(result)
return "\n\n".join(translated)
Step 5: Wire up the CLI
I use argparse so the script accepts files, languages, and an optional glossary JSON. Reading from stdin makes it easy to pipe content from other tools.
import argparse
def main():
parser = argparse.ArgumentParser(description="Context-aware translation agent")
parser.add_argument("--source", default="English", help="Source language")
parser.add_argument("--target", required=True, help="Target language")
parser.add_argument("--tone", default="formal", choices=["formal", "casual", "technical"])
parser.add_argument("--glossary", default=None, help="Path to JSON glossary file")
parser.add_argument("infile", nargs="?", type=argparse.FileType("r"), default="-")
args = parser.parse_args()
text = args.infile.read()
glossary = None
if args.glossary:
with open(args.glossary, "r", encoding="utf-8") as f:
glossary = json.load(f)
output = translate_document(text, args.source, args.target, args.tone, glossary)
print(output)
if __name__ == "__main__":
main()
Run it
Save a glossary file named glossary.json and a source file named docs.txt, then invoke the script.
# glossary.json
{"database shard": "base de datos fragmentada", "latency": "latencia"}
# docs.txt
To reduce latency, split the database shard across two regions.
Run the command:
export OXLO_API_KEY="YOUR_OXLO_API_KEY"
python translate.py --source English --target Spanish --tone technical --glossary glossary.json docs.txt
Expected output:
Para reducir la latencia, divida la base de datos fragmentada entre dos regiones.
Next steps
Add an evaluation harness that scores Oxlo.ai translations against reference texts using COMET or chrF++ to track regression when you change prompts or models. If you move to production, switch to Oxlo.ai's JSON mode to return aligned sentence pairs for downstream diffing. The flat per-request pricing makes running thousands of evaluation calls cheap and predictable.
Top comments (0)