DEV Community

shashank ms
shashank ms

Posted on

Complex Coding with LLMs: Best Practices and Applications

I recently shipped an internal tool that turns a one-paragraph API spec into a working FastAPI service with tests. It runs entirely on Oxlo.ai and cuts our boilerplate time from a few hours to under a minute. In this post I will walk you through the exact agent I built so you can adapt it to your own stack.

What you'll need

Before starting, make sure you have the following:

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

Step 1: Initialize the Oxlo.ai client

Every call routes through Oxlo.ai's OpenAI-compatible endpoint. I keep the client initialization explicit in each snippet so you can see exactly where the request goes. For this agent I use DeepSeek V3.2 because it handles code generation and reasoning well, and Oxlo.ai's flat per-request pricing means I am not penalized for sending long system prompts.

import os
from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key=os.environ["OXLO_API_KEY"])

response = client.chat.completions.create(
    model="deepseek-v3.2",
    messages=[
        {"role": "system", "content": "You are a senior Python engineer."},
        {"role": "user", "content": "Confirm the client is ready."},
    ],
)
print(response.choices[0].message.content)

Step 2: Define the system prompt

The system prompt is the only magic in the pipeline. I keep it versioned in a separate string so non-coders on the team can edit it without touching Python.

SYSTEM_PROMPT = """You are a senior Python engineer. Your job is to turn a natural language API specification into production-ready FastAPI code.

Rules:
- Emit only valid Python 3.10+ code.
- Use Pydantic v2 models for every request and response schema.
- Add type hints everywhere.
- Include a main block that runs uvicorn.
- Do not add markdown fences around the code.
- If the spec is ambiguous, make a reasonable assumption and add a comment."""

Step 3: Decompose the spec into modules

Complex coding fails when you ask for everything at once. I first ask the model to outline the files we need. This gives us a plan and lets us generate each file with full context.

SPEC = """
Build a URL shortener API with these endpoints:
- POST /links to create a short code for a URL.
- GET /{code} to redirect.
- GET /stats/{code} to return click count and original URL.
Use an in-memory dict for storage.
"""

response = client.chat.completions.create(
    model="deepseek-v3.2",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": f"Given this spec, list the Python files needed and a one-sentence description of each. Spec:\n{SPEC}"},
    ],
    temperature=0.1,
)
outline = response.choices[0].message.content
print(outline)

Step 4: Generate the implementation

I send the outline back as context. Because Oxlo.ai charges per request rather than per token, I can include the full outline and spec in every call without worrying about a ballooning bill. This is a major advantage for multi-step agentic workflows.

impl_prompt = f"""Spec:
{SPEC}

File outline:
{outline}

Generate a single file named main.py that implements the entire service. Include Pydantic models, the FastAPI app, and the in-memory store."""

response = client.chat.completions.create(
    model="deepseek-v3.2",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": impl_prompt},
    ],
    temperature=0.2,
)
main_py = response.choices[0].message.content

with open("main.py", "w") as f:
    f.write(main_py)

print("Wrote main.py")

Step 5: Generate tests

I use a different model for this step. Kimi K2.6 on Oxlo.ai handles agentic coding and long context well, so I pass it the generated implementation and ask for pytest coverage.

with open("main.py") as f:
    source = f.read()

test_prompt = f"""Here is the service I just built:

{source}

Write a comprehensive pytest file named test_main.py. Use httpx.AsyncClient for endpoint tests. Cover success cases, 404s, and duplicate code creation."""

response = client.chat.completions.create(
    model="kimi-k2.6",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": test_prompt},
    ],
    temperature=0.2,
)
test_py = response.choices[0].message.content

with open("test_main.py", "w") as f:
    f.write(test_py)

print("Wrote test_main.py")

Step 6: Self-review for bugs

Before I commit anything, I run a review pass. I send both files to the model and ask for a bug list. If it finds issues, I regenerate.

review_prompt = f"""main.py:
{source}

test_main.py:
{test_py}

List any bugs, missing imports, or type errors. Be concise."""

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "system", "content": "You are a staff engineer doing code review."},
        {"role": "user", "content": review_prompt},
    ],
    temperature=0.1,
)
print(response.choices[0].message.content)

Run it

You can now run the generated service locally.

# Terminal
pip install fastapi uvicorn pydantic pytest httpx
python main.py

In another terminal:

pytest test_main.py -v

When I ran this against the URL shortener spec, DeepSeek V3.2 on Oxlo.ai produced a working FastAPI app with Pydantic models and proper redirects. Kimi K2.6 generated async tests that caught a missing 404 handler in my first draft. The review step found an unused import. Example output from the review step looked like this:

1. Unused import: typing.Optional in main.py.
2. GET /stats/{code} should return 404 when code missing.
3. test_duplicate_code expects 409, but implementation returns 200.

Wrap-up

That is the entire pipeline. You can extend it by adding a formatting step with ruff or black, or by swapping in Oxlo.ai's Qwen 3 32B for multilingual specs. If you want to run this on your own CI, Oxlo.ai's flat per-request pricing makes long system prompts and multi-turn reviews predictable. Check the details at https://oxlo.ai/pricing.

Two concrete next steps:

  1. Add a loop that feeds review feedback back into the generator until the review comes back clean.
  2. Replace the in-memory dict with a SQLite schema and have the model generate the migration script.

Top comments (0)