DEV Community

shashank ms
shashank ms

Posted on

Using LLMs for Code Generation: Best Practices and Examples

I built a small code generator that takes a plain-english task, writes Python, reviews itself, and saves the file. It runs entirely on Oxlo.ai and costs one flat request per generation loop, which keeps pricing predictable even when I paste in long stack traces or documentation as context.

What you'll need

Before starting, grab an Oxlo.ai API key from https://portal.oxlo.ai. You will also need Python 3.10 or newer and the OpenAI SDK installed.

pip install openai

Step 1: Verify the client and generate a function

First, I verify the connection to Oxlo.ai by generating a simple utility function. I use deepseek-v3.2 because it is optimized for coding and reasoning, and it is available on the free tier.

from openai import OpenAI

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

response = client.chat.completions.create(
    model="deepseek-v3.2",
    messages=[
        {"role": "system", "content": "You are an expert Python developer."},
        {"role": "user", "content": "Write a function that validates an email address with a regex and returns True or False."},
    ],
)

print(response.choices[0].message.content)

Step 2: Lock down the output format with a system prompt

Raw markdown is annoying to parse, so I force the model to return JSON with separate fields for the code, dependencies, and notes. I also embed style rules directly in the system prompt so I do not have to repeat them every request.

SYSTEM_PROMPT = """You are a senior software engineer. When given a coding task, respond with a JSON object containing exactly these keys:
- summary: a one-sentence description of what the code does
- code: the full source code as a single string, including necessary imports
- dependencies: a list of required third-party packages, or an empty list if none are needed
- notes: any caveats or usage hints

Rules for the code:
- Include type hints
- Handle edge cases with explicit exceptions
- Add docstrings
- Do not include markdown formatting inside the code string"""

Step 3: Build the generator function

Next, I wrap the API call in a typed helper that parses the JSON automatically. This keeps the rest of the script clean and makes error handling easier.

import json
from typing import Dict

def generate_code(task: str) -> Dict[str, str]:
    response = client.chat.completions.create(
        model="deepseek-v3.2",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": task},
        ],
        response_format={"type": "json_object"},
    )
    
    raw = response.choices[0].message.content
    parsed = json.loads(raw)
    return parsed

result = generate_code(
    "Write a FastAPI endpoint that accepts a JSON payload, validates it with Pydantic, and returns a 201 status."
)
print(result["code"])

Step 4: Add a self-review step

Generated code often looks correct but contains subtle bugs. I send the draft to llama-3.3-70b with a review prompt that checks for null pointers, injection risks, and missing validations. If the verdict is needs_fix, I print the issues and stop rather than writing broken code to disk.

REVIEW_PROMPT = """You are a senior code reviewer. Analyze the provided Python code and respond with JSON containing exactly these keys:
- issues: a list of specific bugs or risks found, or an empty list
- fixes: a list of concise fixes, or an empty list
- verdict: either "pass" or "needs_fix"

Focus on: null pointer exceptions, injection risks, missing input validation, and incorrect error handling."""

def review_code(code: str) -> Dict:
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": REVIEW_PROMPT},
            {"role": "user", "content": code},
        ],
        response_format={"type": "json_object"},
    )
    return json.loads(response.choices[0].message.content)

review = review_code(result["code"])
print(json.dumps(review, indent=2))

Step 5: Assemble the CLI tool

Finally, I combine the pieces into a single script that reads a task from the command line, runs the generation loop, and writes the output to generated_code.py.

import json
import sys
from pathlib import Path
from typing import Dict, Any
from openai import OpenAI

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

SYSTEM_PROMPT = """You are a senior software engineer. When given a coding task, respond with a JSON object containing exactly these keys:
- summary: a one-sentence description of what the code does
- code: the full source code as a single string, including necessary imports
- dependencies: a list of required third-party packages, or an empty list if none are needed
- notes: any caveats or usage hints

Rules for the code:
- Include type hints
- Handle edge cases with explicit exceptions
- Add docstrings
- Do not include markdown formatting inside the code string"""

REVIEW_PROMPT = """You are a senior code reviewer. Analyze the provided Python code and respond with JSON containing exactly these keys:
- issues: a list of specific bugs or risks found, or an empty list
- fixes: a list of concise fixes, or an empty list
- verdict: either "pass" or "needs_fix"

Focus on: null pointer exceptions, injection risks, missing input validation, and incorrect error handling."""

def generate_code(task: str) -> Dict[str, str]:
    response = client.chat.completions.create(
        model="deepseek-v3.2",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": task},
        ],
        response_format={"type": "json_object"},
    )
    return json.loads(response.choices[0].message.content)

def review_code(code: str) -> Dict[str, Any]:
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": REVIEW_PROMPT},
            {"role": "user", "content": code},
        ],
        response_format={"type": "json_object"},
    )
    return json.loads(response.choices[0].message.content)

def main():
    if len(sys.argv) < 2:
        print("Usage: python codegen.py 'your task here'")
        sys.exit(1)
    
    task = sys.argv[1]
    print(f"Task: {task}")
    
    draft = generate_code(task)
    print(f"Generated: {draft['summary']}")
    print(f"Dependencies: {draft['dependencies']}")
    
    review = review_code(draft["code"])
    if review["verdict"] == "needs_fix":
        print("Review flagged issues:")
        for issue in review["issues"]:
            print(f"  - {issue}")
        sys.exit(1)
    
    print("Review passed.")
    out_path = Path("generated_code.py")
    out_path.write_text(draft["code"], encoding="utf-8")
    print(f"Saved to {out_path}")

if __name__ == "__main__":
    main()

Run it

Save the script as codegen.py and run it with a real task. Here is what the output looks like when I ask for a SQLite helper.

$ python codegen.py "Write a thread-safe SQLite helper class that executes parameterized queries and returns results as dictionaries."

Task: Write a thread-safe SQLite helper class that executes parameterized queries and returns results as dictionaries.
Generated: A thread-safe SQLite helper class that executes parameterized queries and returns results as dictionaries.
Dependencies: []
Review passed.
Saved to generated_code.py

The generated file contains a complete class with type hints, docstrings, and a threading.Lock around the connection. Because Oxlo.ai uses flat per-request pricing, I could have pasted a hundred lines of existing schema code into the task as context without worrying about token costs.

Next steps

Try swapping deepseek-v3.2 for qwen-3-32b or kimi-k2.6 when you need advanced reasoning or vision capabilities, such as generating a React component from a screenshot. If you are generating code in a CI pipeline, the Oxlo.ai request-based pricing makes long-context diffs cheap compared to token-based providers. Check the latest plans at https://oxlo.ai/pricing.

Top comments (0)