Hugging Face outlines a safer way to train tool-using agents with RL
A token-handling mistake in multi-turn RL can silently corrupt gradients when models call tools mid-rollout.

Hugging Face’s Agentic RL: Token-In, Token-Out Done Right, published May 29, 2026, warns that a subtle tokenization error can undermine reinforcement learning for tool-using language models.
In a standard RL loop, the model is updated using the exact tokens it generated. Tool-enabled agents complicate that process: after producing a tool call, the system may decode the output, parse it, add the tool result, and re-render the entire conversation for the next turn. That decode-and-encode round trip can produce different token IDs, even when the visible text appears unchanged.
The result is especially dangerous because the pipeline may continue running while the gradient is calculated against tokens the model never sampled. Developers may see unstable losses, odd reward behavior, or eventual shape-mismatch failures without an obvious cause.
Two implementation paths
The post presents two ways to preserve the Token-In, Token-Out invariant. One is to use model-specific renderers that format messages, parse completions, and carry state between turns without re-rendering. Hugging Face points to the open-source renderers library, which supports major open-weight model families but requires ongoing per-model maintenance.
The preferred design is simpler: never re-encode tokens that have already been decoded. Tool-message handling then depends on the chat template being prefix-preserving, a property the authors say most current templates already have.
For teams building agent training infrastructure, the lesson is practical: conversation formatting is part of the learning algorithm, not just an interface detail. Preserving sampled token IDs can prevent silent training corruption as rollouts become multi-turn and tool-driven.
Source: Hugging Face Blog
Comments
Log in to join the discussion