Guaranteed 15% off your current AI inference bill for team spending up to $20000 / month.

Book a call →
Back to Blogs
Engineering

Unlocking the Potential of Oxlo.ai for Code Analysis and Generation

I built a lightweight code review agent that reads Python files, flags bugs and performance issues, and emits a rewritten version. It runs on Oxlo.ai, and...

Unlocking the Potential of Oxlo.ai for Code Analysis and Generation

I built a lightweight code review agent that reads Python files, flags bugs and performance issues, and emits a rewritten version. It runs on Oxlo.ai, and because the platform charges a flat rate per API request rather than per token, analyzing a 500-line module costs the same as reviewing a 10-line snippet. If you are working with large codebases or long context windows, this pricing model keeps costs predictable and removes the temptation to truncate files to save money.

What you'll need

I also recommend setting your API key as an environment variable named OXLO_API_KEY so you do not hardcode secrets in your scripts. If you are on the Oxlo.ai free tier, you get 60 requests per day, which is plenty for testing this agent.

Step 1: Initialize the Oxlo.ai client

Create a file named code_agent.py. Start by importing the OpenAI SDK and pointing it at Oxlo.ai. The platform is fully OpenAI-compatible, so the only change needed is the base_url. I am using deepseek-v3.2 here because it handles code and reasoning well, and it is available on the free tier. If you need deeper reasoning for complex architectural reviews, swap in kimi-k2.6 or qwen-3-32b without changing any other code. Oxlo.ai carries over 45 models, so you can experiment with different reasoning styles without managing multiple provider accounts.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key=os.getenv("OXLO_API_KEY", "YOUR_OXLO_API_KEY"),
)

MODEL = "deepseek-v3.2"

Step 2: Define the system prompt

The system prompt is the only behavioral constraint the agent receives. I keep it explicit and structured so the output is machine-parseable and consistent across runs. This matters because we will extract the rewrite automatically in a later step.

SYSTEM_PROMPT = """You are a senior staff engineer performing code review.
When given Python code:
1. Identify bugs, security issues, and performance anti-patterns.
2. Rate the code 1-10 on readability, performance, and safety.
3. Provide a rewritten version that fixes all issues.

Use exactly these delimiters:
ANALYSIS: 
RATING: Readability: X/10, Performance: Y/10, Safety: Z/10
REWRITE: 
"""

Step 3: Build the analysis function

Next, wrap the API call in a function that takes raw source code and returns the model response. I wrap the user code in triple backticks so the model does not confuse instructions with input. Because Oxlo.ai bills per request rather than per token, I can pass the entire file contents without worrying about input length. The platform also has no cold starts on popular models, so the first request after idle time returns immediately. I set temperature to 0.2 to keep the output deterministic while still allowing the model enough freedom to restructure code when necessary.

def analyze_code(source_code: str) -> str:
    user_message = f"Review the following Python code:\n\n```python\n{source_code}\n```"
    
    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ],
        temperature=0.2,
    )
    return response.choices[0].message.content

Step 4: Parse output and handle files

Raw text is not enough for automation. I add a lightweight parser that extracts the three sections using regular expressions, plus a helper to read Python files from disk. The regexes are intentionally simple. They look for the exact delimiters the system prompt requested, which keeps the parser small and avoids pulling in heavy dependencies. If the model ever omits a section, the parser falls back to a safe default string so the script does not crash.

import re
from pathlib import Path

def parse_review(text: str) -> dict:
    analysis_match = re.search(r'ANALYSIS:\s*(.*?)(?=RATING:|REWRITE:|$)', text, re.DOTALL)
    rating_match = re.search(r'RATING:\s*(.*?)(?=REWRITE:|$)', text, re.DOTALL)
    rewrite_match = re.search(r'REWRITE:\s*```python\s*(.*?)```', text, re.DOTALL)
    
    return {
        "analysis": analysis_match.group(1).strip() if analysis_match else "No analysis found.",
        "rating": rating_match.group(1).strip() if rating_match else "No rating found.",
        "rewrite": rewrite_match.group(1).strip() if rewrite_match else "No rewrite found.",
    }

def read_file(path: str) -> str:
    return Path(path).read_text(encoding="utf-8")

Step 5: Wrap it in a CLI

Finally, add a __main__ block that accepts a file path, prints the structured review to stdout, and writes the rewritten code to a new file with a .fixed.py suffix. This gives you a clean artifact you can diff against the original.

if __name__ == "__main__":
    import sys
    
    if len(sys.argv) != 2:
        print("Usage: python code_agent.py ")
        sys.exit(1)
    
    file_path = sys.argv[1]
    source = read_file(file_path)
    raw_review = analyze_code(source)
    result = parse_review(raw_review)
    
    print("=== ANALYSIS ===")
    print(result["analysis"])
    print("\n=== RATING ===")
    print(result["rating"])
    print("\n=== REWRITE ===")
    print(result["rewrite"])
    
    out_path = file_path.replace(".py", ".fixed.py")
    Path(out_path).write_text(result["rewrite"], encoding="utf-8")
    print(f"\nSaved rewrite to {out_path}")

Run it

I tested this on a small file containing two classic Python gotchas: a mutable default argument and an unclosed file handle. Save this as example.py in the same directory as your agent:

def get_data(filename, cache=[]):
    f = open(filename, "r")
    data = f.read()
    return data

Run the agent from your terminal:

python code_agent.py example.py

With deepseek-v3.2, I got the following output in under a second. The model correctly identified both the mutable default argument and the resource leak, then rewrote the function to use a with statement and a None guard. That level of precision is why I default to this model for Python refactoring.

=== ANALYSIS ===
The function uses a mutable default argument `cache=[]`, which causes the list to persist across calls. The file handle opened with `open(filename, "r")` is never closed, leading to a resource leak. There is also no error handling for a missing file.

=== RATING ===
Readability: 6/10, Performance: 5/10, Safety: 4/10

=== REWRITE ===
def get_data(filename, cache=None):
    if cache is None:
        cache = []
    with open(filename, "r", encoding="utf-8") as f:
        data = f.read()
    return data

Saved rewrite to example.fixed.py

Next steps

The agent works, but it is still a single-shot script. Two directions I plan to take it next:

  1. Pre-commit hook: Wire the script into Git hooks so every commit gets an automatic readability and safety check before it reaches review. Because Oxlo.ai has no cold starts, the hook runs fast enough to stay out of your way.
  2. Multi-language support: Oxlo.ai hosts models like qwen-3-32b and kimi-k2.6 that handle multilingual codebases. Parameterize the system prompt by language and route to the appropriate model for TypeScript, Go, or Rust analysis.

Because Oxlo.ai uses request-based pricing, running this on a 2,000-line legacy file costs the same as a 50-line utility. For teams doing heavy code review automation, that predictability matters. You are not penalized for sending full context, which means you can feed entire modules instead of carving out snippets. You can check the details at https://oxlo.ai/pricing.

Ready to build with Oxlo.ai?

Get started building high-performance AI inference applications today.

Get started
Ox Assistant
Online
OxBot
OxBot

Hi there! Try our cost calculator to see what you'd save with Oxlo.ai.