Long-Horizon Agent RL: A Practical Guide to Low-Cost Training
This article explains why reinforcement learning for long-horizon agents is expensive and how to reduce that cost without giving up performance. It breaks down the three main cost drivers (sampling, environment, and scheduling), presents a simplified cost model, and walks through a reusable five-part training recipe covering base model selection, reward design, trajectory filtering, parallel sampling with environment isolation, and checkpointing. It also covers the compute and scheduling setup, evaluation and cost reporting practices, and common pitfalls to avoid.
1. Why Long-Horizon Agents Are Expensive to Train
Long-horizon agents are pushing task boundaries from “fix one bug” to hours of continuous terminal operations and software engineering collaboration. They need to read logs, modify code, run tests, roll back, and verify again. A single training sample often corresponds to one complete attempt that lasts hours and involves hundreds of tool calls. Unlike single-turn question answering, the cost of reinforcement learning for these tasks does not come mainly from model parameter size. It comes from the multiplicative amplification of “trajectory length × failure probability × retry count”: if any stage goes wrong, the wasted resource is hours of compute time.
The industry usually groups these costs into three categories. The first is sampling cost: long trajectories mean each sample consumes more GPU memory and more wall-clock time. The second is environment cost: terminals, compilation, and testing require isolated and reproducible execution environments. The third is scheduling cost: a large number of concurrent tasks place higher demands on storage, networking, and failure recovery. Understanding these three cost categories is the starting point for designing a low-cost training recipe.
2. Core Idea: Treat Performance and Cost as First-Class Concerns
Public engineering practice shows that a 300B-class terminal and coding agent can reach the same Pass@1 level as top closed-source solutions on mainstream terminal benchmarks, while the cost per task is significantly lower. This improvement can also transfer to benchmarks closer to real software engineering. The key is not a single technique, but treating the “performance–cost Pareto frontier” as the training objective: under a fixed compute budget, prioritize cutting ineffective trajectories and repeated sampling rather than blindly increasing training steps.
A simplified cost model can express this:
Total cost ≈ number of sampled trajectories × average trajectory length × per-step inference cost+ number of environment executions × per-execution environment cost+ number of failed retries × per-retry cost
The optimization directions are therefore clear: reduce average trajectory length, increase the success rate of each attempt, and reduce ineffective retries.
3. Breaking Down the Training Recipe: Five Reusable Engineering Stages
3.1 Base Model Selection and Full-Parameter Training
Start from a medium-sized base model with controllable inference cost and perform full-parameter reinforcement learning, rather than training only a small number of adapter layers. Full-parameter training is more conducive to forming stable tool-use habits and error recovery strategies on long-horizon tasks, while avoiding the diminishing returns of multi-stage fine-tuning.
3.2 Reward Design: Outcome-Focused, Process as Support
Long-horizon tasks should not use dense step-by-step rewards, which easily encourage “step farming.” The industry generally uses outcome rewards as the main signal, supplemented by verifiable intermediate signals, such as whether tests pass or whether a command returns the expected status code. Example:
def reward(trajectory, env_result):# Outcome reward dominates to avoid encouraging ineffective stepsr = 1.0 if env_result.tests_passed else 0.0# Slight process constraint: apply a tiny penalty for overly long trajectoriesr -= 0.001 * max(0, len(trajectory) - 120)return r
3.3 Trajectory Filtering and Difficulty Stratification
Stratify tasks by historical success rate and prioritize sampling from the “medium difficulty” range: tasks that are too easy contribute low gradients, while tasks that are too hard produce almost entirely failed trajectories. Attribute failed trajectories (environment errors, tool-call errors, reasoning errors) and use only failures attributable to the policy for training.
3.4 Parallel Sampling and Environment Isolation
Run each trajectory in an independent container with fixed file systems, networking, and dependency versions. Decouple the sampling side from the training side: sampling results are written asynchronously to a queue, and the training side consumes them in batches. This way, even if an individual environment crashes, it does not block the overall training.
3.5 Checkpoints and Fast Rollback
The biggest fear in long-horizon training is “crashing halfway through.” Save checkpoints at fixed step intervals, and combine them with training logs and a metrics dashboard. When reward collapse occurs, quickly roll back to the last stable point to avoid wasting an entire round of compute.
4. Compute and Scheduling: Feasibility with 64 GPUs in Under Five Days
Completing full-parameter training of a 300B-class model on 64 high-bandwidth accelerator cards relies on three key points. First, use a hybrid split of tensor parallelism and pipeline parallelism to distribute parameters, gradients, and optimizer states across multiple cards. Second, overlap sampling and training so that GPUs continue performing gradient updates while waiting for environments to return. Third, place data loading and checkpoint writes on high-throughput storage to reduce I/O waits.
When deploying on the cloud, equivalent results can be achieved by combining general-purpose capabilities: a container platform hosts the isolated sampling environments, object storage holds checkpoints and trajectory data, a message queue decouples sampling from training, and a monitoring and alerting service tracks reward curves and failure rates. This allows building a reproducible training pipeline without binding to any proprietary interfaces.
5. Evaluation and Cost Accounting: Making Results Comparable
Evaluation should report Pass@1 averaged over multiple trials, along with the average cost per task. The cost accounting must be clearly stated: whether it is converted from public pricing or from self-deployment depreciation, and whether it includes environment execution and retries. Only under the same accounting basis is a performance–cost scatter plot meaningful, and only then can the Pareto frontier be verified.
6. Common Pitfalls and How to Avoid Them
- Looking only at the final success rate without looking at trajectory length, leading to out-of-control costs;
- Rewards that are too dense, causing the model to learn to “call more tools” rather than “solve the problem”;
- Environments that are not reproducible, making failures impossible to attribute and drowning the training signal in noise;
- Sampling and training running serially, leaving GPUs largely idle;
- Missing checkpoints, so a single crash loses hours of compute.
7. Conclusion
The competition in long-horizon agent training is shifting from “who has more GPUs” to “whose recipe is more economical.” By writing the cost model into the training objective and turning trajectory filtering, environment isolation, asynchronous sampling, and fast rollback into standard components, limited compute can still approach the performance–cost frontier. For most teams, the value of this approach lies not in reproducing a specific number, but in establishing a measurable, reproducible, and continuously optimizable training engineering system.
