Low-Cost LLMs in Software Engineering: Principles, Boundaries, and Practice

    This article explains how teams are shifting from comparing LLM parameter counts to evaluating real-world repair effectiveness, cost per call, and manual review effort. It covers software engineering challenges such as long-context codebase understanding, RAG-based retrieval, and automated test-feedback loops; how to interpret SWE-bench scores; a code review example using an LLM API; cost composition and estimation; and techniques such as MoE, low-bit quantization, KV cache optimization, and speculative decoding. It also introduces selection considerations across closed-source APIs, private deployment, and cloud-hosted inference.

    Introduction: From Parameter Competition to Effectiveness and Cost Comparison

    Over the past two years, the use of large language models in software development has gradually moved from demonstrations to daily use, and the questions teams care about have changed accordingly. Previously, people mainly compared model parameter counts. Now they focus more on three things: how many real repository issues the model can fix correctly, how much each call costs, and how much manual review the results require. Benchmarks such as SWE-bench provide a common basis for this comparison, but scores can only serve as a reference and cannot be directly equated with production readiness.

    Core Challenges in Software Engineering Tasks

    Software engineering tasks go far beyond completing a single line of code. They require the model to understand cross-file context, translate requirements into code changes, write tests, and locate and fix defects.

    1. Long Context and Codebase Understanding: Large projects often contain hundreds of thousands of lines of code. Models usually need retrieval-augmented generation (RAG) to first find relevant files, then read function definitions and call relationships. When retrieval results are incomplete, the model may provide seemingly reasonable changes with insufficient information, which is one of the most common causes of failure.
    2. Automated Testing and Verification: Model-generated code needs to pass compilation, unit tests, and integration tests. A more reliable approach is to build a “generate, test, feedback” loop: pass the failed test output back to the model, let it make another round of changes, and limit the number of rounds to a fixed maximum.

    How to Interpret Benchmark Scores

    The pass rate of SWE-bench refers to the proportion of real issues in specified versions of open-source repositories that a model fixes and that pass the preset tests. When comparing two models, several things need to be confirmed: whether the evaluation dataset versions are consistent, whether the same scripts and number of runs are used, and whether the models are configured with the same tools and context length. Without these conditions, score differences may mainly come from the test setup rather than model capability.

    Even when scores are comparable, they only represent performance on specific repositories and test sets. Production codebases, test coverage, and business rules vary, so manual review is still required.

    Code Review Example

    The following example shows how to ask a model to locate a defect based on an error log. The model name and service address are placeholders, and the API key should be injected through environment variables.

    1. import os
    2. from openai import OpenAI # Requires: pip install openai
    3. client = OpenAI(
    4. api_key=os.environ["LLM_API_KEY"], # Read from environment variables; do not hard-code
    5. base_url=os.environ["LLM_BASE_URL"], # Point to an internal or authorized inference service
    6. timeout=60,
    7. )
    8. def review_code_for_bug(file_content: str, error_log: str) -> str:
    9. prompt = f"""You are a senior software engineer. Analyze the following error log and code file,
    10. locate the root cause of the error, and provide the fixed code snippet.
    11. [Error Log]
    12. {error_log}
    13. [Code File]
    14. {file_content}
    15. [Output Requirements]
    16. 1. Point out specific line numbers and logic defects.
    17. 2. Provide fixed code and explain whether it affects existing interface contracts.
    18. 3. If the information is insufficient to determine the cause, explicitly state what information is missing; do not guess.
    19. """
    20. try:
    21. response = client.chat.completions.create(
    22. model=os.environ.get("LLM_MODEL", "your-deployed-model-name"),
    23. messages=[{"role": "user", "content": prompt}],
    24. temperature=0,
    25. )
    26. return response.choices[0].message.content
    27. except Exception as exc:
    28. # Log the error and hand it over for manual handling; do not silently swallow the exception
    29. raise RuntimeError(f"Model call failed: {exc}") from exc

    There are three points to note in this code. First, keys and service addresses must not be written into source code or logs. Second, model-provided fixes must be tested in a sandbox before adoption and must not be merged directly. Third, code snippets sent to the model should be desensitized first and may only be sent to inference services authorized by the enterprise.

    Cost Composition and Estimation

    The cost of a single task consists of several parts, and model call fees are only one of them.

    Variable Meaning Notes
    Input tokens Length of prompts and code context The longer the context, the higher the cost
    Output tokens Model-generated analysis and code Output unit price is usually higher than input unit price
    Number of calls Average rounds per task The test-feedback loop increases the number of rounds
    Cache hit rate Whether repeated context hits the cache When hit, input cost usually decreases
    Manual review time Time engineers spend reviewing results Often ignored, but may be the largest cost

    Model call cost can be estimated with the following formula:

    Per-task call cost = average number of calls × (input tokens × input unit price + output tokens × output unit price)

    Assume a team consumes an average of 20,000 input tokens and 2,000 output tokens per call, with an average of 2 rounds. The input unit price is CNY 1 per million tokens, and the output unit price is CNY 4 per million tokens. The following numbers are for demonstration only; refer to actual quotes.

    Per-task call cost = 2 × (20000 × 1 / 1000000 + 2000 × 4 / 1000000) = 2 × (0.02 + 0.008) = CNY 0.056

    Manual review time needs to be converted into cost separately. If one review takes 10 minutes, its cost is often far higher than the CNY 0.056 call fee. Therefore, when evaluating whether automation is worthwhile, both parts should be calculated together.

    Low cost does not come from a single technique but from the combination of several techniques:

    1. Mixture of Experts (MoE): The model has a large total parameter count, but only some expert networks are activated during each inference. Computation per inference decreases while model capacity remains unchanged.
    2. Low-bit quantization: INT8 or INT4 quantization can reduce memory usage, but it may lose some accuracy and needs to be re-evaluated on the target task.
    3. KV Cache optimization: Caches already computed key-value pairs to avoid recomputing historical tokens in long contexts. Scenarios that repeatedly reference the same codebase benefit more noticeably.
    4. Speculative decoding: A small draft model first generates candidate tokens, and the large model verifies them in parallel. Structured text such as code usually has a high acceptance rate, but the specific value depends on the task and requires measurement.

    Stacking techniques can reduce call costs, but it cannot eliminate testing and manual review.

    Model Selection: Closed-Source APIs, Private Deployment, and Cloud-Hosted Inference

    The selection comparison covers closed-source APIs, private deployment, and cloud-hosted inference. The first dimension is the destination of code data.