[RFC] Agentic RL training on the DeepSpeed hybrid engine
Is your feature request related to a problem? Please describe.
The DeepSpeed Q4 2026 roadmap (#8672) lists "Agent Sandbox enablement for Agentic post-training". This RFC plans that work for the hybrid engine.
In agentic RL, the policy acts in an environment over many turns (calling tools, running code, editing a repository) and is trained on the trajectories it produces. HybridEngineRollout today generates a single response per prompt and returns it as a padded [B, T] tensor. That is enough for single-turn RL, but an agentic rollout needs four things it does not provide:
- Interleaved spans. A trajectory alternates model tokens with environment tokens such as tool output. Only the model's tokens may be training targets, so the output needs a per-token loss mask, not a single response boundary.
- Per-trajectory metadata. Trajectories differ in length and end for different reasons (answered, out of budget, failed). The trainer needs each one's status, reward and rollout log-probs, which a
[B, T]tensor cannot carry. - An environment loop. Something has to parse the model's actions, execute them and feed the results back, turn after turn. It must continue from the exact tokens the model sampled, or the recorded log-probs no longer match the sequence that is trained.
- Isolation. Model-written code and shell commands must run where they cannot harm the training job or the host.
The goal of this RFC is native agentic training support in the hybrid engine that keeps its main advantage, rollout and training on one engine with no inference server and no weight sync, while staying efficient as trajectories grow longer and tools grow slower.
Describe the solution you'd like
A staged plan. Each stage is useful on its own and builds on the previous one.
| Stage | What the model does | What DeepSpeed adds | Example workload |
|---|---|---|---|
| 1. Multi-turn tool calling | Calls stateless tools in a loop the rollout owns | Record-based rollout API, multi-turn generation loop, turn-level continuous batching, tools and parsers, GRPO loss | ReTool: math with a Python interpreter |
| 2. Agentic tool calling | Works over long horizons with stateful, slow tools | Per-trajectory environment state, timeouts and abort handling, environment rewards | Multi-step search or interpreter tasks with persistent state |
| 3. Harness | Runs inside an agent harness that owns the loop | A harness interface; multi-segment trajectories | Existing agent scaffolds, e.g. coding agents |
| 4. Sandbox | Acts in an isolated, resettable environment | Sandbox interface, lifecycle and pooling, resource limits, verifiers run in the sandbox | Repository tasks scored by unit tests, terminal tasks |
Stage 1: multi-turn tool calling
The model calls tools across several turns, with the rollout owning the loop, as in ReTool (math with a Python interpreter). This is the foundation the later stages extend.
- Rollout records. A rollout output that carries one trajectory per record: interleaved model and tool spans with a loss mask, per-trajectory status, reward and rollout log-probs, and the grouping of a prompt's samples that GRPO needs.
- Multi-turn generation. A generate, parse, execute, append loop inside the rollout, with turn, token and context budgets. The next turn continues from the exact tokens the model sampled, so the recorded log-probs match the trained sequence.
- Turn-level continuous batching. The scheduling unit becomes a turn, not a request. When a turn ends, its decode row is freed; the tool runs on the host while the row serves another trajectory, and the next turn is prefilled into any free row. This extends request-level continuous batching to multi-turn rollouts.
- Sampling for RL. Sampled decode on every decode path, including CUDA graph decode and continuous batching (#8934); log-probs recorded under the distribution actually sampled; sampling settings that are explicit, so a checkpoint's generation config never changes them silently.
- Tool calling. A tool interface and tool-call parsers for common model families, with tool errors returned to the model rather than failing the rollout.
- Training on masked trajectories. Training batches that honor the loss mask, so tool output is never a target, and GRPO loss primitives: group advantages and a PPO-clip policy loss over the masked tokens.
Example. A companion PR in DeepSpeedExamples (training/grpo) uses stage 1 end to end: GRPO on math with a sandboxed Python interpreter and a DAPO strict-box reward.
Stage 2: agentic tool calling
Agentic tasks run for many turns, and their tools keep state and take real time. Stage 1 treats tools as stateless functions that return quickly, which stops holding there.
- Per-trajectory environment state. Tools receive a context object created when a trajectory starts and closed when it ends, including on abort, so an interpreter session, a working directory or a browser can persist across calls.
- Timeouts and failure handling. Per-call and per-trajectory time limits; a trajectory that exceeds them, or whose environment fails, is
ABORTED. The trainer chooses whether aborted records are dropped or kept with zero advantage. - Environment rewards. The environment can set the reward (and optionally per-turn rewards) on the record, instead of a reward function applied after the rollout.
Stage 3: harness
Real agents run inside a harness that builds prompts, chooses tools, manages context and may run sub-agents. To train on harness-driven trajectories, the harness owns the loop and the rollout serves its generation requests, while still recording exactly what the policy sampled.
- Sessions. A token-in/token-out generation API keyed by trajectory: each call returns the sampled tokens and their log-probs and appends them to the trajectory's record. Calls from many concurrent harness instances are batched by the stage 1 turn-level scheduler.
- Message adapter. For harnesses that speak in chat messages, the adapter renders the conversation and checks that it extends the session's previous tokens. If it does, only the new suffix is tokenized; if the harness rewrote history (for example by compacting context), a new segment starts.
- Multi-segment trajectories. A record can hold several segments, each a separate training sequence with its own loss mask, sharing one trajectory-level reward and advantage.
- Harness interface.
async run(task, session) -> result; the rollout runs many harness instances concurrently. Optionally, an OpenAI-compatible endpoint backed by the session API lets an unmodified harness run against the hybrid engine during the rollout phase.
Stage 4: sandbox
Tools that execute model-written code, or act on repositories and shells, must run isolated, many at a time.
- Sandbox interface. Create from a spec, execute a command with a timeout, read and write files, reset or snapshot, close. Backends: a local subprocess with resource limits (for tests and debugging), a container backend (with Apptainer for HPC clusters without Docker), and a hook for remote sandbox services. Heavy backends can live outside DeepSpeed.
- Lifecycle and pooling. Sandboxes are tied to the stage 2 trajectory context and kept in a warm pool to hide startup latency; they are always cleaned up, including on abort.
- Placement. Sandboxes run on CPU resources, either per rank (each rank owns its trajectories' sandboxes) or as a shared service, with per-node quotas.
- Isolation defaults. No network and no access to the training job's files unless configured; CPU, memory and time limits.
- Verifiers. Rewards computed inside the sandbox, for example by running a task's unit tests.
Out of scope
- Asynchronous (off-policy) RL that overlaps rollout and training.
Hi @anyadontfly, I'd like to extend the scope of #8790 to cover what RL training needs from vocab-parallel ops. My earlier plan was to bring TRL-style chunking optimizations into DeepSpeed, such as computing per-token log-probs and entropy in chunks instead of materializing the full logits. Since this RFC is shaping the agentic RL side of the hybrid engine, I'd love to hear your thoughts: do you have any suggestions for this direction, or requirements you expect from the trainer side?
Hi @anyadontfly, I'd like to extend the scope of #8790 to cover what RL training needs from vocab-parallel ops. My earlier plan was to bring TRL-style chunking optimizations into DeepSpeed, such as computing per-token log-probs and entropy in chunks instead of materializing the full logits. Since this RFC is shaping the agentic RL side of the hybrid engine, I'd love to hear your thoughts: do you have any suggestions for this direction, or requirements you expect from the trainer side?
Hi @jinyouzhi , thanks for extending #8790 towards RL. I am revisiting how the agentic RL system around the Hybrid Engine is structured, but what won't change is that the Hybrid Engine stays the runtime that both generates and trains. So the vocab-parallel ops are engine primitives and belong in runtime core.
Here are some points I would care for RL side:
- Matching rollout and training log-probs. Even though we could recompute log-probs at trainer, if rollout records log-probs with the same op used in training, the trainer can skip the old-log-prob forward pass.
- Keep the ops per-token. GRPO variants aggregate differently (token-mean, sequence-mean, DAPO-style), so masking and reduction are better left in the trainer, as you proposed.
- Streamed logprobs and entropy with a position mask. Agentic trajectories reach 32K–128K tokens, and most of them are prompt and tool output that sit between the model's turns. Since the streamed op does the LM-head projection itself, taking a position mask would let it skip those tokens entirely.
Hi @anyadontfly, I'd like to extend the scope of #8790 to cover what RL training needs from vocab-parallel ops. My earlier plan was to bring TRL-style chunking optimizations into DeepSpeed, such as computing per-token log-probs and entropy in chunks instead of materializing the full logits. Since this RFC is shaping the agentic RL side of the hybrid engine, I'd love to hear your thoughts: do you have any suggestions for this direction, or requirements you expect from the trainer side?
Hi @jinyouzhi , thanks for extending #8790 towards RL. I am revisiting how the agentic RL system around the Hybrid Engine is structured, but what won't change is that the Hybrid Engine stays the runtime that both generates and trains. So the vocab-parallel ops are engine primitives and belong in runtime core.
Here are some points I would care for RL side:
- Matching rollout and training log-probs. Even though we could recompute log-probs at trainer, if rollout records log-probs with the same op used in training, the trainer can skip the old-log-prob forward pass.
- Keep the ops per-token. GRPO variants aggregate differently (token-mean, sequence-mean, DAPO-style), so masking and reduction are better left in the trainer, as you proposed.
- Streamed logprobs and entropy with a position mask. Agentic trajectories reach 32K–128K tokens, and most of them are prompt and tool output that sit between the model's turns. Since the streamed op does the LM-head projection itself, taking a position mask would let it skip those tokens entirely.
Thanks for the helpful suggestions! I’ve updated #8790 and plan to start with your third point—streamed vocab-parallel log-probs and entropy with a position mask—as a reusable primitive that should fit alongside your evolving architecture.
Thanks for proposing this RFC. I generally don't feel we should prioritize Agentic RL training for HybridEngine, it's completely out of scope for HybridEngine. Agentic RL requires significant systems optimization which goes far beyond our current capabilities. I strongly suggest one turns to Slime or Miles for such workloads.
Thanks for proposing this RFC. I generally don't feel we should prioritize Agentic RL training for HybridEngine, it's completely out of scope for HybridEngine. Agentic RL requires significant systems optimization which goes far beyond our current capabilities. I strongly suggest one turns to Slime or Miles for such workloads.
Thanks, @PKUWZP I agree that we should keep HybridEngine’s scope focused. That said, I’m not sure moving toward slime/Miles alone gives DeepSpeed a clear path forward in post-training. Perhaps a more practical direction would be to gradually bring relevant RL capabilities into a DeepSpeed-centered workflow, rather than depend on those frameworks adopting DeepSpeed as a component.
Thanks for proposing this RFC. I generally don't feel we should prioritize Agentic RL training for HybridEngine, it's completely out of scope for HybridEngine. Agentic RL requires significant systems optimization which goes far beyond our current capabilities. I strongly suggest one turns to Slime or Miles for such workloads.
Thanks, @PKUWZP I agree that we should keep HybridEngine’s scope focused. That said, I’m not sure moving toward slime/Miles alone gives DeepSpeed a clear path forward in post-training. Perhaps a more practical direction would be to gradually bring relevant RL capabilities into a DeepSpeed-centered workflow, rather than depend on those frameworks adopting DeepSpeed as a component.
Great point. I think I will defer to @sfc-gh-truwase for the scope of DeepSpeed in the RL space. Given that we already have Arctic RL that leverages DeepSpeed as the training engine.
Thanks for sharing ideas on this @PKUWZP @jinyouzhi. I will to talk to @minjiazhang this week on how DeepSpeed can fit not only as the training engine but also rollout engine for a RL system. And I will update this plan after landing on a more practical path.
@anyadontfly please see https://github.com/Snowflake-AI-Research/Arctic-Platform#arctic-reinforcement-learning