I built a lightweight CLI translator that turns any text into a target language via a single LLM call. It is useful for developers who need to embed translation into data pipelines, internal tools, or subtitle workflows without managing token math. Because Oxlo.ai charges per request rather than per token, I can feed it long paragraphs or batch files and the cost stays flat.
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: Set up the Oxlo.ai client
Create translator.py and initialize the client. Oxlo.ai is fully OpenAI SDK compatible, so the only change is the base URL.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key=os.environ["OXLO_API_KEY"]
)
Step 2: Write the translation system prompt
I keep the system prompt strict. This prevents the model from adding fluff like "Here is the translation" before the output.
SYSTEM_PROMPT = """You are an expert translator. Follow these rules:
1. Translate the user's text into the requested target language.
2. Preserve the original tone, formatting, and line breaks.
3. Do not add explanations, preambles, or notes.
4. If the text is already in the target language, return it unchanged.
5. Output only the translated text."""
MODEL = "llama-3.3-70b"
Step 3: Build the translation function
Add the core function that packages the user text with the target language and sends it to Oxlo.ai.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key=os.environ["OXLO_API_KEY"]
)
SYSTEM_PROMPT = """You are an expert translator. Follow these rules:
1. Translate the user's text into the requested target language.
2. Preserve the original tone, formatting, and line breaks.
3. Do not add explanations, preambles, or notes.
4. If the text is already in the target language, return it unchanged.
5. Output only the translated text."""
MODEL = "llama-3.3-70b"
def translate(text: str, target_language: str) -> str:
user_prompt = f"Translate the following text into {target_language}.\n\n{text}"
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
temperature=0.2,
)
return response.choices[0].message.content
Step 4: Wire up the CLI
I use argparse so the script feels like a real tool. This lets me pipe text in or call it from Makefiles and CI jobs.
import argparse
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key=os.environ["OXLO_API_KEY"]
)
SYSTEM_PROMPT = """You are an expert translator. Follow these rules:
1. Translate the user's text into the requested target language.
2. Preserve the original tone, formatting, and line breaks.
3. Do not add explanations, preambles, or notes.
4. If the text is already in the target language, return it unchanged.
5. Output only the translated text."""
MODEL = "llama-3.3-70b"
def translate(text: str, target_language: str) -> str:
user_prompt = f"Translate the following text into {target_language}.\n\n{text}"
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
temperature=0.2,
)
return response.choices[0].message.content
def main():
parser = argparse.ArgumentParser(description="CLI translator powered by Oxlo.ai")
parser.add_argument("text", help="Text to translate")
parser.add_argument("--to", default="English", help="Target language")
args = parser.parse_args()
result = translate(args.text, args.to)
print(result)
if __name__ == "__main__":
main()
Run it
Export your key and translate a few lines.
$ export OXLO_API_KEY=oxlo_xxxxxxxx
$ python translator.py "Bonjour le monde" --to English
Hello world
$ python translator.py "The quick brown fox jumps over the lazy dog." --to Spanish
El rápido zorro marrón salta sobre el perro perezoso.
Next steps
Add a --file argument that reads JSON or subtitle files and translates each entry in a loop. Because Oxlo.ai uses request-based pricing, you can process large documents without watching token meters tick up. For non-English language pairs or long-context documents, try swapping MODEL to qwen-3-32b or kimi-k2.6. See the latest model list and plan details at https://oxlo.ai/pricing.
Top comments (0)