Buckets:

hf-doc-build/doc-dev / openenv /pr_1027 /en /tutorials /opencode-agent-grpo.html
download
raw
17.4 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Training a Real Coding Agent with GRPO (OpenCode)&quot;,&quot;local&quot;:&quot;training-a-real-coding-agent-with-grpo-opencode&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;How It Works&quot;,&quot;local&quot;:&quot;how-it-works&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Run The Recipe&quot;,&quot;local&quot;:&quot;run-the-recipe&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Full Recipe&quot;,&quot;local&quot;:&quot;full-recipe&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/openenv/pr_1027/en/_app/immutable/entry/start.cQO0wAkq.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/chunks/BtRqeX6K.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/chunks/Bb-LL0eD.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/entry/app.RvAWJdqN.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/chunks/CY-Wb_o3.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/chunks/CC8tNNdZ.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/chunks/Cy2JwhB0.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/nodes/0.BVP1YLlb.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/chunks/DLds7x_i.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/nodes/5.BmtqML2M.js" rel="modulepreload">
<link href="/docs/openenv/pr_1027/en/_app/immutable/chunks/CKEMnrQO.js" rel="modulepreload">
<!--ekqdbr--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Training a Real Coding Agent with GRPO (OpenCode)&quot;,&quot;local&quot;:&quot;training-a-real-coding-agent-with-grpo-opencode&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;How It Works&quot;,&quot;local&quot;:&quot;how-it-works&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Run The Recipe&quot;,&quot;local&quot;:&quot;run-the-recipe&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Full Recipe&quot;,&quot;local&quot;:&quot;full-recipe&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/openenv/pr_1027/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="training-a-real-coding-agent-with-grpo-opencode" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-a-real-coding-agent-with-grpo-opencode"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>Training a Real Coding Agent with GRPO (OpenCode)</span></h1><!--]--><!----> <p>This tutorial covers the black-box training path: training the actual <a href="https://opencode.ai" rel="nofollow"><code>opencode</code></a> coding agent, with its own planner, tools,
context management, and stop condition, using TRL’s experimental <code>AsyncGRPOTrainer</code>. The agent owns its loop, and OpenEnv captures what it did.</p> <blockquote class="note"><p>Three GRPO patterns, three tutorials. For a standard <code>reset()</code> / <code>step()</code> flow where TRL drives the episode, see the <a href="wordle-grpo">Wordle GRPO tutorial</a>. For harness rollouts where the
trainer still generates each turn (white-box), see the <a href="browsergym-harness">BrowserGym harness tutorial</a>. Use this page when you
want to train a production agent as-is, without reimplementing its loop.</p></blockquote> <!--[1--><h2 class="relative group"><a id="how-it-works" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#how-it-works"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>How It Works</span></h2><!--]--><!----> <p>The full recipe lives in TRL. The moving pieces:</p> <ol><li>Each rollout runs the agent inside an OpenEnv session created by <code>OpenCodeSessionFactory</code> from <a href="https://github.com/huggingface/OpenEnv/tree/main/envs/opencode_env" rel="nofollow"><code>opencode_env</code></a>,
in <code>transparent_proxy</code> mode. A small proxy inside the sandbox forwards the
agent’s <code>/v1/chat/completions</code> calls to your vLLM server and records each
turn’s token ids and logprobs to a trace.</li> <li>When the agent stops, TRL’s <code>HarnessRolloutWorker</code> reads the trace, rebuilds
the per-turn training rows from the recorded ids, and scores the final
workspace with the session’s <code>verify()</code> method (a held-out verifier the
agent never sees).</li> <li><code>AsyncGRPOTrainer</code> trains on those rows, propagating the rollout reward to
every trained token through the group-relative advantage. NCCL weight sync
keeps the vLLM server on the current policy, so the agent always samples
from the model being trained.</li></ol> <p>Each rollout gets its own isolated session: one sandbox, one proxy port, one
agent process. Three small functions adapt the recipe to your task: <code>rollout_reward_fn</code> (outcome to scalar reward), <code>train_turn_fn</code> (which turns
receive gradient), and <code>agent_turn_fn</code> (which trace entries are real agent
turns rather than auxiliary calls like title generation). All three are
documented in <a href="https://huggingface.co/docs/trl/openenv#training-on-harnesses-training-a-real-coding-agent-opencode" rel="nofollow">TRL’s harness training guide</a>.</p> <!--[1--><h2 class="relative group"><a id="run-the-recipe" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#run-the-recipe"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>Run The Recipe</span></h2><!--]--><!----> <p>The reference script trains on competitive-coding problems from <code>agentica-org/DeepCoder-Preview-Dataset</code>. The agent writes <code>solution.py</code>, and
the verifier runs it against held-out tests, returning the fraction passed.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-bash "><!---->pip install trl trackio datasets
pip install <span class="hljs-string">&quot;openenv-opencode-env @ git+https://github.com/huggingface/OpenEnv.git#subdirectory=envs/opencode_env&quot;</span><!----></pre></div><!----> <p>Serve the policy with tool calling, token ids, and NCCL weight sync enabled
(one GPU), then train (a second GPU):</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Copied</div><!----></button><!----></div> <pre class="language-bash "><!----><span class="hljs-comment"># Terminal 1</span>
CUDA_VISIBLE_DEVICES=0 VLLM_SERVER_DEV_MODE=1 vllm serve Qwen/Qwen3-4B-Instruct-2507 \
--host 0.0.0.0 --port 8000 \
--enable-auto-tool-choice --tool-call-parser hermes \
--logprobs-mode processed_logprobs \
--return-tokens-as-token-ids \
--weight-transfer-config <span class="hljs-string">&#x27;{&quot;backend&quot;:&quot;nccl&quot;}&#x27;</span>
<span class="hljs-comment"># Terminal 2</span>
CUDA_VISIBLE_DEVICES=1 python examples/scripts/openenv/opencode.py \
--model Qwen/Qwen3-4B-Instruct-2507 --vllm-url http://localhost:8000<!----></pre></div><!----> <p>The script is self-contained and runs the agent in a local subprocess sandbox,
so no container setup is needed. The recipe has been validated end to end on
Qwen3 (see <a href="https://github.com/huggingface/trl/pull/6420" rel="nofollow">huggingface/trl#6420</a>).</p> <!--[1--><h2 class="relative group"><a id="full-recipe" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#full-recipe"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span>Full Recipe</span></h2><!--]--><!----> <ul><li><a href="https://huggingface.co/docs/trl/openenv#training-on-harnesses-training-a-real-coding-agent-opencode" rel="nofollow">Training on harnesses</a> in TRL’s OpenEnv docs: rollout semantics, the reward path, turn selection,
and the trace contract.</li> <li><a href="https://github.com/huggingface/trl/blob/main/examples/scripts/openenv/opencode.py" rel="nofollow"><code>examples/scripts/openenv/opencode.py</code></a> in TRL: the complete, runnable script.</li> <li><a href="https://github.com/huggingface/OpenEnv/tree/main/envs/opencode_env" rel="nofollow"><code>envs/opencode_env</code></a>:
the OpenEnv side, including the session factory, sandbox backends, and the
transparent interception proxy.</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/openenv/blob/main/docs/source/tutorials/opencode-agent-grpo.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]-->
<script>
{
__sveltekit_b566xn = {
base: "/docs/openenv/pr_1027/en",
assets: "/docs/openenv/pr_1027/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/openenv/pr_1027/en/_app/immutable/entry/start.cQO0wAkq.js"),
import("/docs/openenv/pr_1027/en/_app/immutable/entry/app.RvAWJdqN.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 5],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
17.4 kB
·
Xet hash:
85297c3328c3bec32816baa0bda53be583373f2096f9a577904c0f26154fe364

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.