DEV Community

shashank ms
shashank ms

Posted on

Leveraging LLMs for Coding Efficiency

We are building a small CLI tool that reviews Python files for bugs and generates pytest tests automatically. It is aimed at developers who want to speed up code review and test coverage without running local GPUs. Every call routes through Oxlo.ai's OpenAI-compatible API, so you can switch between coding models like DeepSeek V3.2, Llama 3.3 70B, or Kimi K2.6 by changing a single string.

What you'll need

  • Python 3.10 or newer
  • An Oxlo.ai API key from https://portal.oxlo.ai
  • The OpenAI SDK installed with pip install openai

Oxlo.ai uses request-based pricing, so reviewing a 500-line module costs the same flat rate as a 10-line script. That makes this tool cheap to run against large files or entire directories. Check https://oxlo.ai/pricing for current plan details.

Step 1: Bootstrap the client

Create a file named code_assistant.py and initialize the OpenAI SDK against Oxlo.ai's base URL. I default to deepseek-v3.2 because it is built for coding and reasoning, and it is available on the Oxlo.ai free tier.

from openai import OpenAI

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

MODEL = "deepseek-v3.2"

Step 2: Define the system prompt

The system prompt acts as a config file for the model's personality. Keeping it in a module-level constant makes it easy to tweak without touching the rest of the logic.

REVIEWER_PROMPT = """You are a senior staff engineer performing a code review.
Analyze the provided Python code and return a JSON object with exactly these keys:
- "bugs": a list of strings describing potential bugs or exceptions
- "style": a list of strings describing style or readability issues
- "suggestions": a list of concrete improvement recommendations
Be concise. Do not include markdown formatting outside the JSON."""

Step 3: Build the review function

This helper reads a file, wraps the source in a user message, and asks the model to return structured JSON. Oxlo.ai supports JSON mode on compatible models, so we can parse the result with the standard library.

import json

def review_file(path: str) -> dict:
    with open(path, "r") as f:
        source = f.read()

    user_msg = f"Review this Python file:\n\n{source}"

    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": REVIEWER_PROMPT},
            {"role": "user", "content": user_msg},
        ],
        response_format={"type": "json_object"},
        temperature=0.2,
    )

    raw = response.choices[0].message.content
    return json.loads(raw)

Step 4: Add test generation

We add a second prompt and function that writes pytest cases. Because Oxlo.ai charges a flat rate per request, sending the same large file twice does not require any token arithmetic. You pay the same cost per call whether the input is fifty lines or five hundred.

TEST_PROMPT = """You are a senior staff engineer writing unit tests.
Given the provided Python code, generate a complete pytest test file.
Include edge cases, error conditions, and docstrings.
Output only valid Python code. Do not wrap it in markdown."""

def generate_tests(path: str) -> str:
    with open(path, "r") as f:
        source = f.read()

    user_msg = f"Write pytest tests for this code:\n\n{source}"

    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": TEST_PROMPT},
            {"role": "user", "content": user_msg},
        ],
        temperature=0.2,
    )

    return response.choices[0].message.content

Step 5: Wire the CLI

We use argparse to expose two subcommands, review and test. The entrypoint dispatches to the correct helper and prints the result.

import argparse

def main():
    parser = argparse.ArgumentParser(
        description="CLI coding assistant powered by Oxlo.ai"
    )
    subparsers = parser.add_subparsers(dest="command", required=True)

    review_parser = subparsers.add_parser("review", help="Review a Python file")
    review_parser.add_argument("file")

    test_parser = subparsers.add_parser("test", help="Generate pytest tests")
    test_parser.add_argument("file")

    args = parser.parse_args()

    if args.command == "review":
        result = review_file(args.file)
        print(json.dumps(result, indent=2))
    elif args.command == "test":
        tests = generate_tests(args.file)
        print(tests)

if __name__ == "__main__":
    main()

Run it

Create a sample calculator.py with a deliberate bug:

def divide(a, b):
    return a / b

def average(numbers):
    total = sum(numbers)
    return total / len(numbers)

Run the review subcommand:

export OXLO_API_KEY="YOUR_OXLO_API_KEY"
python code_assistant.py review calculator.py

Example output:

{
  "bugs": [
    "divide() does not handle ZeroDivisionError when b is 0.",
    "average() does not handle empty lists, which raises ZeroDivisionError."
  ],
  "style": [
    "Missing docstrings for both functions.",
    "No type hints provided."
  ],
  "suggestions": [
    "Add input validation or try/except blocks for division by zero.",
    "Consider adding type hints and docstrings for clarity."
  ]
}

Generate tests:

python code_assistant.py test calculator.py

Example output:

import pytest
from calculator import divide, average

def test_divide_normal():
    assert divide(10, 2) == 5.0

def test_divide_by_zero():
    with pytest.raises(ZeroDivisionError):
        divide(10, 0)

def test_average_normal():
    assert average([1, 2, 3]) == 2.0

def test_average_empty_list():
    with pytest.raises(ZeroDivisionError):
        average([])

Next steps

Add a --fix flag that sends the review JSON back to the model and requests a corrected version of the source file, then writes it to disk. Alternatively, wire the review command into a Git pre-commit hook so every changed Python file is checked automatically before it reaches your main branch.

Top comments (0)