Lightweight LLMs for Code Generation: Stalls, Instruction Following, and Engineering Mitigations

    Lightweight LLMs are increasingly being integrated into AI coding tools. They offer clear advantages in inference cost, response latency, and local deployment, but stalls during inference and unstable instruction following often hurt developer experience. This article categorizes the symptoms, explains how to diagnose them, proposes measurable metrics for instruction following, and discusses engineering responses.

    1. Distinguish Two Types of Stalls

    In AI coding tools, users typically encounter two types of problems. The first is a long period with no output during inference, followed by a large block of text being emitted at once. The second is more severe: the model keeps reasoning until timeout and never reaches the code-output stage. The two problems require different diagnostic directions and should be handled separately.

    1.1 Pauses During Inference

    The cause of a stall should be diagnosed based on the symptom rather than attributed directly to the model itself. There are three common causes:

    • Reasoning falls into repetition: The model repeatedly expands the same branch in intermediate steps. Tokens continue to be generated, but the reasoning does not advance to a conclusion. To diagnose this, check whether the intermediate text contains many repeated or similar sentences.
    • Instruction decay under long context: As context grows, the model may pay less attention to constraints at the beginning of the prompt, and the reasoning may remain entirely at the analysis level. To diagnose this, move key constraints to the end of the prompt and retry, then observe whether the output improves.
    • Intermediate-layer buffering: Gateways, proxies, or clients parsing an SSE (Server-Sent Events) stream may accumulate a batch of data before forwarding it. To diagnose this, use curl to connect directly to the model service and compare the time to first token between the direct connection and the tool-mediated path. If the direct connection is fast but the tool-mediated path is slow, the problem is likely in the intermediate layer.

    1.2 Excessive Total Time

    The total time for complex tasks may increase significantly, especially for problems with many boundary conditions and branches that require exhaustive exploration. This article does not provide fixed latency numbers, because results depend on model size, hardware, concurrency, and task difficulty. Teams should record the P95 of time to first token and total latency themselves, and use real data to set alert thresholds.

    2. Measure Instruction Following with Countable Metrics

    “Following the prompt” is a vague statement. Teams need to break it down into countable metrics. The following four are recommended:

    • Constraint compliance pass rate: Check the hard constraints in the prompt one by one, and calculate the ratio of satisfied constraints to total constraints.
    • Long-range constraint retention rate: Place constraints at the beginning of the prompt, and measure the proportion of outputs that still satisfy those constraints after long reasoning.
    • First-pass compile rate: The proportion of generated code that compiles or passes static checks without modification.
    • Unit test pass rate: The proportion of generated code that passes predefined test cases.

    Each constraint needs explicit decision rules. For example, for “no third-party dependencies,” the decision method is to parse import statements and allow only the standard library and dependencies already present in the project. For “input parameters must have strict type validation,” the decision method is to check whether the function entry point contains type checks or assertions. Clear decision rules ensure that different reviewers reach the same conclusion for the same output.

    In addition, consistency between reasoning and code can be measured. Compare the implementation steps listed in the reasoning with the final code item by item, and record any steps that are not implemented.