Long-Horizon Agent RL: A Low-Cost Training Playbook

    This article breaks down how to train competitive long-horizon agents with reinforcement learning on a modest GPU budget. It covers the three core challenges of long-horizon RL—credit assignment, sampling cost, and environment instability—and three practical design choices: layered rewards, asynchronous rollout scheduling, and fault tolerance with reproducible environments. It also explains why per-task inference cost can drop by an order of magnitude and outlines a five-step implementation path for small and mid-sized teams.

    1. Why Long-Horizon Agents Are Expensive to Train

    Long-horizon agents are moving beyond short interactions like “fix a bug” toward continuous terminal operations and software engineering work that can last for hours. As task chains get longer, the cost of reinforcement learning training is amplified significantly: a single training sample often corresponds to a complete attempt that spans hours and involves hundreds of tool calls. If any step in the middle fails, what is wasted is not a single forward pass but hours of compute occupancy.

    The traditional approach is to “throw more GPUs at it”: scale parallel experiments across thousands of accelerators and trade scale for stability. For most teams, however, this path is neither feasible nor economical. The question worth studying is whether you can train an agent that is competitive on terminal and coding benchmarks within a window of a few days on a few dozen GPUs, while pushing per-task inference cost extremely low.

    A research team recently published a terminal and coding agent approach that starts from a 300B-class foundation model and is trained with full-parameter reinforcement learning: using only 64 next-generation accelerators and less than 5 days of training, it reaches a Pass@1 of around 46% on mainstream terminal task benchmarks, on par with top closed-source solutions, while achieving significantly lower per-task cost and demonstrating capability transfer across multiple software engineering benchmarks. Setting aside the specific product attributes, the reusable engineering methods are extracted below.

    2. Three Core Challenges of Long-Horizon RL

    1. Difficult credit assignment: A single task involves hundreds of tool calls, and the final success or failure is a sparse terminal signal, making it hard to tell which step was right or wrong.
    2. Extremely high sampling cost: Every rollout is a complete long trajectory, and sampling throughput directly determines training wall-clock time.
    3. Unstable environments: External dependencies such as terminals, compilation, and testing bring timeouts, network jitter, and dependency conflicts, causing trajectories to fail for “non-model reasons” and polluting the training signal.

    3. Key Design One: Layered Rewards and Tasks

    Do not use “whether the final test passes” as the only reward. A layered structure can be adopted:

    1. R_total = w1 * R_outcome + w2 * R_process + w3 * R_efficiency - w4 * R_penalty
    • R_outcome: whether the final test passes;
    • R_process: intermediate milestones (such as successfully locating a file, passing compilation, passing local tests);
    • R_efficiency: efficiency reward for turns and tokens, suppressing meaningless long loops;
    • R_penalty: penalties for illegal operations, destructive commands, and environment pollution.

    Layered rewards can significantly alleviate the credit assignment problem while making “shorter paths” an optimizable objective. This is one of the key sources of reduced per-task cost.

    4. Key Design Two: Rollout Scheduling and Sampling Efficiency

    • Asynchronous sampling: Decouple the inference engine from the training engine, generate long trajectories in a separate resource pool, and avoid training GPUs sitting idle.
    • Trajectory-level batching: Batch by “task” rather than by “single step” to reduce the drag of long-tail tasks on the whole batch.
    • Partial trajectory reuse: Truncate and re-enter timed-out or failed trajectories rather than discarding them entirely.
    • Difficulty bucketing: Bucket tasks by historical pass rate to control the difficulty distribution of each batch and stabilize gradients.

    Illustrative scheduling pseudocode:

    1. for batch in task_buckets.sample(n=global_batch):
    2. traj = async_rollout(policy, batch, max_turns=200, timeout=10800)
    3. traj = sanitize(traj) # remove environment-caused failures
    4. loss = ppo_loss(policy, traj, reward=layered_reward(traj))
    5. optimizer.step()

    5. Key Design Three: Fault Tolerance, Reproducibility, and Data Hygiene

    The biggest hidden cost of long-horizon training is “running for nothing.” Recommendations:

    • Build a reproducible container snapshot for each task, so failures can be rebuilt with one click;
    • Distinguish “model failures” from “environment failures,” and do not count the latter in the training signal;
    • Log complete trajectories and tool call logs for easy attribution;
    • Set checkpoints to allow recovery from the most recent stable point rather than starting over.

    6. Cost Control: Why Per-Task Cost Can Drop by an Order of Magnitude

    Cost = amortized training + inference consumption. Ways to reduce inference consumption include: shorter effective trajectories (efficiency rewards), lower default inference intensity, on-demand upgrades of the inference budget, and caching and batching of high-frequency tool calls. When the policy learns to “solve problems with fewer turns,” per-task cost drops approximately linearly as turns decrease. This is also the engineering source of gaps like “30x lower.”

    7. A Practical Implementation Path

    1. Pick an internal evaluation set of 100 to 300 tasks and first get a zero-training baseline running;
    2. Integrate asynchronous rollout and layered rewards, and run small-scale ablations;
    3. Introduce difficulty bucketing and checkpoints to stabilize training;
    4. Validate capability transfer on both terminal and software engineering benchmarks;
    5. Finally, stress-test per-task cost to establish an online threshold.

    8. Conclusion

    The competition in long-horizon agents is shifting from “who has more GPUs” to “whose training and inference pipeline is more efficient.” By solidly doing the three things—layered rewards, sampling scheduling, and fault tolerance with reproducibility—small and mid-sized teams can also train usable agents with limited compute and push per-task cost down to a level suitable for scalable deployment. If you need elastic compute and supporting capabilities, you can build a training and evaluation pipeline on top of general capabilities such as the container platform, object storage, log service, and monitoring and alerting offered by Baidu AI Cloud.