# tinker_cookbook.rl.RolloutLoop *Generated from tinker-cookbook at commit [`4fa3ab8`](https://github.com/thinking-machines-lab/tinker-cookbook/tree/4fa3ab8949b7f172ce37256bfcd8203f85d88e37). Source links point at that snapshot.* ## *class* [**tinker_cookbook.rl.RolloutLoop**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/4fa3ab8949b7f172ce37256bfcd8203f85d88e37/tinker_cookbook/rl/types.py#L312)(*ABC*) Runs one group of rollouts against a policy and returns a [`TrajectoryGroup`](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/trajectorygroup/index.md). This is the unit of rollout work that [`RLDataset`](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/rldataset/index.md) hands to the training and evaluation loops. There are two ways to implement it: - Subclass [`EnvGroupBuilder`](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/envgroupbuilder/index.md) when episodes are step-by-step Python [`Env`](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/env/index.md) instances. It implements `run` from `make_envs()` and `compute_group_rewards()`. - Subclass [`RolloutLoop`](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/rolloutloop/index.md) directly when something else drives the whole multi-turn episode loop and the grading, such as a sandboxed coding agent or an external rollout server. **Picklability:** Implementations must be pickleable (via standard `pickle`) to support distributed rollout execution where rollout loops are serialized and sent to remote workers. Avoid storing live network connections, file handles, or other unpickleable objects as fields. Store configuration strings (URLs, model names, task paths) and construct heavy clients lazily inside `run()`. ### [**run**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/4fa3ab8949b7f172ce37256bfcd8203f85d88e37/tinker_cookbook/rl/types.py#L333)(*policy*) Run all rollouts for this group against *policy* and return the graded group. Called by `~tinker_cookbook.rl.rollouts.do_group_rollout`, which then logs the trajectories and calls `cleanup`. The returned [`TrajectoryGroup`](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/trajectorygroup/index.md) must have equal-length `trajectories_G`, `final_rewards_G`, and `metrics_G` lists. Per-trajectory rewards may be placed on transition `reward` fields, in `final_rewards_G`, or both (`TrajectoryGroup.get_total_rewards` sums them). The implementation owns everything about how the group is produced: episodes, retries, grading, and how trajectories that stopped on a limit are rewarded. Failures it tolerates can be recorded on `TrajectoryGroup.rollout_errors`; they are reported to `~tinker_cookbook.rl.rollouts.RolloutErrorCounter`. An exception raised here is a group-level error: it fails the run, or skips the group when the caller sets `catch_group_errors`. **Parameters:** - [**policy**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/4fa3ab8949b7f172ce37256bfcd8203f85d88e37/tinker_cookbook/rl/types.py#L333) (*[TokenCompleter](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/completers/tokencompleter/index.md)*) – The token-level policy for this rollout group (in training and eval loops, a `~tinker_cookbook.completers.TinkerTokenCompleter`). **Returns:** *[TrajectoryGroup](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/trajectorygroup/index.md)* – The completed trajectories, final group rewards, per-trajectory metrics, and any captured rollout errors. *Abstract method.* ### [**cleanup**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/4fa3ab8949b7f172ce37256bfcd8203f85d88e37/tinker_cookbook/rl/types.py#L362)() Clean up resources allocated during `run`. Called after the group rollout finishes, regardless of success, failure, or task cancellation. Override this to release expensive resources like cloud sandboxes, remote browsers, etc. Default is a no-op. Implementations should be idempotent (safe to call multiple times) and handle exceptions internally, as `do_group_rollout` does not catch exceptions from this method. **Returns:** *None* ### [**logging_tags**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/4fa3ab8949b7f172ce37256bfcd8203f85d88e37/tinker_cookbook/rl/types.py#L375)() Return tags used to aggregate metrics in training logs. Tags let the training loop group metrics (rewards, episode lengths, etc.) by environment type. Return a short list of names at different levels of granularity. **Returns:** *list[str]* – Tag strings for this rollout group. Default is an empty list. ```python def logging_tags(self) -> list[str]: return ["gsm", "math", "rlvr"] ``` ## Referenced by - [tinker_cookbook.rl.EnvGroupBuilder](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/envgroupbuilder/index.md) - [tinker_cookbook.rl.RLDataset](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/rldataset/index.md) - [tinker_cookbook.rl.RLDataset.get_batch](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/rldataset/#rldataset-get_batch) - [tinker_cookbook.rl.TrajectoryGroup](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/rl/trajectorygroup/index.md)