Reinforcement Learning for Long-Horizon AI Agents: From Outcome Rewards to Process-Level Credit Assignment

    Long-horizon tasks, such as fixing bugs in code or running penetration tests in isolated environments, are becoming a central focus of agent training. This article explains why outcome-only rewards are sparse and misattribute credit across long trajectories, why step-by-step scoring creates a bootstrapping problem, and how counterfactual local branching lets a model estimate the value of individual actions using its own follow-up performance. It also covers the infrastructure needed to make this approach scale, including environment snapshots and large sandbox pools.

    Reinforcement Learning for Long-Horizon Agents: From Outcome Reward Dilemmas to Process-Level Credit Assignment

    1. Background: Long-Horizon Tasks Become the New Frontier for Agent Training

    As post-training for large language models continues to advance, research attention is shifting from single-turn question answering and short reasoning chains to long-horizon tasks. These tasks require dozens or even hundreds of interaction turns. Examples include automatically locating and fixing code defects, conducting penetration tests in isolated environments, and carrying out multi-step software engineering changes. These tasks share three traits: many interaction turns, long decision chains, and sparse environment feedback.

    Recent industry practice shows that training method innovation can sometimes yield larger gains than scaling parameter counts. Some models that do not hold a clear parameter advantage have outperformed larger comparable models on long-horizon benchmarks. They achieved this through more refined post-training methods and a much larger number of sandbox environments. This suggests that for agents, how a model is trained is becoming as important as how large the model is. Some researchers call this “Sandbox Scaling”: the number and diversity of environments become a scalable training resource in their own right.

    2. The Fundamental Flaw of Outcome Rewards

    2.1 Sparse Rewards: One Bad Move Loses the Game

    Consider a typical automated bug-fixing task. An agent may need 30 to 80 interaction turns to finish it. Suppose the model explores the problem, locates the defect, and understands the architecture flawlessly through step 78, but at step 79 it writes an incorrect variable name while editing the code. At step 80, the unit tests fail. The environment then returns a final reward of 0.

    Traditional policy gradient methods, which optimize based on the total return of the whole trajectory, assign this failure equally to all actions across all 80 steps. This resembles a Go game in which losing the final result leads to denying every strong move the player made earlier. The consequences are systematic:

    • Misattribution: High-quality early exploration gets penalized, while the true cause of failure, a single variable name, is buried in a large volume of token-level gradient signal.
    • Conservative behavior: The model learns to avoid risky, deep exploration and prefers shallow, “safe” actions.
    • Training degradation: Sample efficiency on long tasks becomes very low. The model can even grow more hesitant to act over training while its token consumption rises.

    2.2 The Chicken-and-Egg Loop of Process Scoring

    The obvious fix is to stop relying only on outcomes and score every step. This raises an immediate question: who does the scoring?

    To judge whether a code change or a Go move is good, the evaluator must understand software engineering or Go strategy. A sufficiently strong evaluator that scores each step accurately may already match or exceed the policy model being trained. This creates a paradox. The model cannot improve without accurate scores, and it cannot get accurate scores without first improving. Manual annotation is also impractical. Long-horizon tasks often span dozens of steps and thousands of trajectories, so evaluating each one by hand is too costly.

    3. A Way Out: Counterfactual Branch Valuation

    3.1 Core Idea

    Drawing on ideas from Monte Carlo tree search and causal reasoning, a training paradigm called Self-Adaptive Optimization offers a practical answer. It does not depend on an external evaluator. Instead, it uses the model’s own subsequent performance to infer the value of a given step. The key technique can be summarized as “Counterfactual Local Branching.”

    The logic resembles reviewing a Go game. Suppose the player lost, but how can we show that move 78 was a brilliant move? The method keeps the context up to step 77 unchanged, then clones many parallel branches starting from step 78. An “explorer” with the same model and the same capability then continues from each branch. If tasks starting from these branches succeed at a clearly higher rate than from other baseline branches, step 78 itself improved the position. It was a good move, even though the game was ultimately lost.

    3.2 Engineering Key Points

    Turning this idea into a working agent training pipeline requires solving several problems:

    1. Environment snapshots and cloning: The system must take deterministic snapshots of the environment state at any interaction turn and clone hundreds or thousands of branch environments in batches. This is where large-scale sandbox infrastructure matters. Without expanding the number of environments by one or more orders of magnitude, branch valuation is not feasible in terms of compute cost.

    2. Branch sampling strategy: Not every step is worth valuing. In practice, branches are usually expanded at key decision points, such as steps that modify code right before a commit or test run. This keeps the variance and cost of Monte Carlo sampling under control.

    3. Advantage estimation and gradient allocation: The difference between a branch’s success rate and a baseline serves as the local advantage of the action at that step. This advantage replaces or corrects the global return of the full trajectory. As a result, the policy gradient rewards only steps that actually improve the probability of success and penalizes only actions that actually cause harm.

    4. Guarding against self-evaluation bias: Because valuation depends on the model’s own rollout ability, the model may be systematically weak in some states, which can distort the estimates. Common mitigations include sampling at multiple temperatures and mixing evaluations across checkpoints.

    4. Broader Implications: Training Infrastructure as a Competitive Advantage

    This paradigm has two broader implications.

    First, RL is repeating the history of game-playing AI. Combining self-play with tree search once produced a leap in board game intelligence. A combination of self-evaluation, branch search, and large-scale environments may become a general recipe for agent post-training. The model acts as both learner and its own evaluator. Its evaluation ability improves alongside its policy, which breaks the loop described above.

    Second, the importance of environment engineering is seriously underestimated. Whether the setting is a code execution sandbox, a network attack and defense range, or a general tool-use environment, the number, fidelity, snapshot capability, and concurrency of environments directly determine whether these credit assignment methods can scale. For engineering teams, investing in high-concurrency, snapshot-capable sandbox infrastructure is as important as improving the algorithm itself.

    5. Conclusion

    Reinforcement learning for long-horizon agents is, at its core, a fight against the classic problem of credit assignment under sparse feedback. Crude outcome-based attribution can make models more conservative over training. Counterfactual branch valuation offers a path that does not depend on an external evaluator and that improves as the model’s capability grows. When algorithmic innovation and the scaling of sandbox environments reinforce each other, the result may be more than higher benchmark scores. It may mark a shift in how agents behave on complex, real-world tasks, toward exploring boldly and exploring well.