We are building a low-latency image analysis agent that reads an image and returns structured JSON describing objects, visible text, and safety flags. This is useful for teams running automated visual QA or content moderation where response time matters. I chose Oxlo.ai because its per-request pricing keeps costs flat even when large image prompts increase token count, and there are no cold starts on popular models.
What you'll need
- Python 3.10 or higher
- An Oxlo.ai API key from https://portal.oxlo.ai
- The OpenAI SDK:
pip install openai - A sample image file named
sample.jpg
Step 1: Initialize the Oxlo.ai client
First I initialize the OpenAI SDK pointing at Oxlo.ai. This is a drop-in replacement, so the only difference from the standard OpenAI client is the base URL and API key.
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY"
)
Step 2: Encode images for the vision API
Vision models on Oxlo.ai accept base64-encoded images through the standard OpenAI image URL payload. I use a small helper to read a local file and convert it.
import base64
def encode_image(path: str) -> str:
with open(path, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
Step 3: Define the system prompt
To keep latency low, I force structured JSON output and tell the model to be terse. Long prose increases token generation time, so I restrict the response to short lists and booleans.
SYSTEM_PROMPT = """You are a fast visual analysis engine. Inspect the image and return a single JSON object with no markdown formatting. Use exactly these keys: objects (list of strings), visible_text (string), safety_flag (boolean), dominant_colors (list of strings). Keep every value concise."""
Step 4: Build the analysis function with timing
Now I wire the encoded image into the chat completion call. I use kimi-k2.6 because it supports vision and reasoning, and I enable JSON mode so I do not have to parse markdown. I also wrap the call with a timer so I can verify latency in production.
import json
import time
def analyze_image(image_path: str):
b64_image = encode_image(image_path)
data_url = f"data:image/jpeg;base64,{b64_image}"
start = time.perf_counter()
response = client.chat.completions.create(
model="kimi-k2.6",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": [
{"type": "image_url", "image_url": {"url": data_url}},
{"type": "text", "text": "Analyze this image and return JSON only."}
]}
],
response_format={"type": "json_object"},
max_tokens=400
)
latency = time.perf_counter() - start
result = json.loads(response.choices[0].message.content)
return result, latency
Step 5: Add a CLI runner
Finally, I add a small CLI wrapper so I can run this against any image path. This is the exact script I deploy to my inference container.
if __name__ == "__main__":
import sys
image_file = sys.argv[1] if len(sys.argv) > 1 else "sample.jpg"
print(f"Analyzing {image_file}...")
result, latency = analyze_image(image_file)
print(f"\nLatency: {latency:.2f}s")
print(json.dumps(result, indent=2))
Run it
Save the complete script as analyze.py and run it against a test image. Because Oxlo.ai has no cold starts on popular models, the first request returns immediately without a warm-up penalty.
python analyze.py sample.jpg
Example output:
Analyzing sample.jpg...
Latency: 0.82s
{
"objects": ["laptop", "coffee cup", "notebook"],
"visible_text": "Oxlo.ai",
"safety_flag": false,
"dominant_colors": ["black", "silver", "brown"]
}
Wrap-up
From here, you can extend the agent with function calling to route flagged images to a review queue, or parallelize batches with asyncio to increase throughput. If you are processing high volumes, check out Oxlo.ai pricing; request-based pricing means image size does not affect cost, which makes budgeting predictable as you scale.
Top comments (0)