LoRA Fine-Tuning for 27B Models: A 16-Point Gain on Terminal Tasks

    This article explains how a LoRA adapter can improve the behavior of a 27B model on multi-step terminal tasks without retraining the base model. It covers why strong single-turn code generation does not translate into reliable agentic execution, how to reshape tool-use patterns with low-rank adaptation, an asynchronous reinforcement learning training loop, reward design, data and sandbox environment requirements, observed efficiency and accuracy gains, a failure-to-success case comparison, deployment formats, and practical recommendations with risk notes for teams building coding or terminal agents.

    1. Background: Why “Writing Code” Is Not the Same as “Getting Work Done”

    As AI coding tools move into real development workflows, a clear gap has emerged: models perform well on single-turn code generation benchmarks, but their success rate drops sharply once they face terminal tasks that require multiple steps and multiple tools working together.

    The reason is not mysterious. Terminal tasks are essentially long-chain decision making. The model has to read files, search for symbols, run commands, parse errors, make another edit, and verify the result. A single wrong judgment at any step propagates forward, showing up as repeated trial and error, unproductive searching, or failure after hitting the step limit.

    This is why the industry’s focus is shifting from model parameter count to agent behavior quality. A 27B-class model, when its tool use is properly shaped, can complete complex repair tasks with explicit chain-of-thought disabled while keeping generation cost low. That is exactly where the LoRA adapter approach delivers the most value: no base model retraining, only a change in behavioral policy.

    2. Core Idea: Keep the Base Model, Reshape Tool Use

    The conventional path is continued pre-training or full-parameter fine-tuning, which is expensive, slow, and risky. A lighter alternative keeps the base weights frozen and trains a low-rank adapter that specifically optimizes how the model makes decisions inside an agent framework, including:

    • When to search versus when to read the target file directly.
    • How much context a single tool call actually needs.
    • Whether to blindly rerun tests after a failure or locate the specific failing assertion.
    • How to balance task correctness against call efficiency.

    The key value of such an adapter is that it is pluggable. One base model can carry different adapters for code repair, terminal operations, data processing, and other scenarios, without them interfering with each other.

    3. Training Method: Asynchronous Reinforcement Learning Plus Low-Rank Adaptation

    In practice, the common industry combination of asynchronous reinforcement learning and LoRA works well. Asynchronous sampling lets multiple agent instances interact with the environment in parallel, easing the sampling bottleneck of long-chain tasks. Low-rank adaptation keeps the number of trainable parameters very small, which makes frequent iteration practical.

    A sketch of the training loop (pseudocode, structure only):

    1. for step in range(max_steps):
    2. batch = async_rollout(policy, env_pool, n_envs=32) # sample trajectories in parallel
    3. rewards = [task_correctness(t) + 0.1 * tool_efficiency(t) for t in batch]
    4. loss = policy_loss(batch, rewards, clip_ratio=0.2)
    5. loss.backward() # only LoRA parameters are updated
    6. optimizer.step()

    The reward is usually split into two parts: whether the task actually passes verification (the main reward) and whether tool calls are efficient (an auxiliary reward). The auxiliary weight should stay small, typically around 0.1; otherwise the model sacrifices correctness to save calls.

    4. Data and Environment: Task Distribution Sets the Capability Boundary

    Training data should cover two task types: software engineering repair, meaning locating and fixing bugs in real repositories, and terminal operations, meaning command composition, environment troubleshooting, and script handling. A few hundred to a thousand tasks is enough to start. What matters most is tiered task difficulty and complete verification scripts.

    The execution environment needs three properties: isolation, reset capability, and parallelism. In engineering practice, a containerized sandbox hosts each task instance, a unified environment interface issues commands and collects results, and failed trajectories are prevented from contaminating the next sampling round.

    5. Results: Efficiency and Accuracy Improve Together

    Across commonly reported public benchmark patterns, the adapted model improves on both task families: more than 20 percentage points on software engineering repair tasks and about 16 percentage points on terminal tasks. The process metrics are more telling:

    • On tasks both versions complete, the adapted model needs less than half the tool turns of the base model.
    • With thinking mode disabled, accuracy stays close to the base model running with a high thinking budget.
    • Generated tokens drop to roughly one quarter of the original.

    So the gain does not come from trading more compute for a higher score. It comes from cutting wasted actions.

    6. Case Breakdown: From Repeated Searching to a Single Hit

    A typical failure trajectory looks like this: the base model keeps running global keyword searches and rerunning tests, exhausts its steps, and never converges. The adapted version behaves differently. It narrows scope to the target function and call chain, edits the implementation directly, runs a targeted test, and if something still fails, applies a minimal patch based on the assertion output, usually closing the loop within a single-digit number of tool calls.

    The difference comes down to the model learning the minimal sufficient set of information it needs, instead of treating search as the default action.

    7. Deployment: An Adapter Is Not a Standalone Model

    An adapter must be loaded on top of its matching base model and cannot run on its own. Common delivery formats include low-rank weight files that mainstream inference frameworks and general tensor runtimes can load, as well as quantized packages that make low-resource local or edge deployment easier. If you use a quantized version, validate the accuracy loss on a small regression set first.

    8. Recommendations and Risks

    1. Build a reproducible evaluation set before training, so you avoid a score that goes up with no visible change in production.
    2. Keep the efficiency term at a small weight in the reward function to prevent behavior degradation.
    3. Pay attention to step limits and timeout settings; long-chain tasks need a sensible budget.
    4. Bind the adapter strictly to its base model version, and re-validate after any base model upgrade.
    5. In a cloud environment such as Baidu AI Cloud, which offers container platforms, object storage, and monitoring and alerting, you can manage the sandbox cluster, datasets, and training metrics together to improve iteration speed and observability.