Evaluated on held-out tasks; click a card to jump to it. Decode: at equal weights, gap to the jointly trained policy in pass rate or clean code, with experts trained by standard GRPO or by tilt-GRPO (KL coefficient one). Security: two seeds, our evaluation harness, against task-only RL on the same episodes (Gemma 4 26B: 2.1–2.5% vs 4.8–5.4%). Dial: Qwen2.5-7B, shown for security seed one (it replicates with seed two); two forward passes per token until distilled.
TL;DR
- A GRPO reward defines a target. Weight each output's reference probability by the exponential of its reward. Rewards add, so these multipliers multiply, which gives a rule for combining separately trained policies.
- One target, four uses. The same weighted sum of reward terms reweights samples at inference, builds fine-tuning data, serves as the GRPO reward, and mixes trained policies at decode time.
- Case study: agent security. Task-only RL on BFCL made Qwen2.5-7B agents easier to hijack (12 → 18% on the official benchmark). On Qwen, task-only training on injected episodes already helps (17.8% → 5.1–6.0%), and a security penalty in the reward gets to 1.8–2.9%, at some clean-task utility. On the 26B mixture-of-experts Gemma 4, the penalty gets to 2.1–2.5% against 4.8–5.4% for task-only training. In both families it gives lower pooled hijacking than its control across four held-out attack templates (on Gemma the gain is concentrated in the template closest to training), On Qwen it is hijacked less than the instruct model with either of the two prompting defences we tested, and about as often as the instruct model behind a prompt-injection detector, with 18.6 points more utility under attack. Mixing the task agent and the security policy at decode time gives a caution dial; it moves along everything learned from the injected episodes, not the penalty alone. A chosen setting distils into one model (replicated with two security seeds). On Gemma, turning the dial from 1 to 2 adds no significant security and costs 2.5 points of utility under attack.
- Code and math. Correctness RL with GRPO's usual tiny KL weight makes code messier and less diverse. A stronger KL weight alone removes most of that (73.5% lint-clean, diversity back to the base model, −2.5 pass@1); a lint penalty goes further (lint-clean 59–68 → 89% at −0.8 pass@1) and beats averaging model weights. Separately trained code policies mixed at equal weights come within 3 points of joint training, and the Qwen correctness runs landed on the family of distributions predicted from base samples, on four measured statistics (residual 0.4–0.5; standard GRPO 3.1 at KL weight 0.001, 0.6 at 0.05).
01From a GRPO reward to a target distribution
Start with the reference model. Give each trajectory its original probability, then multiply that probability by the exponential of its reward. This defines a target distribution. If two trajectories both finish the task, the security penalty tells us exactly how their relative odds should change. And because rewards add, their probability multipliers multiply. That is the composition rule we test in this post, on agents and also on code and math.
The target behind the GRPO update
In our target-fitting runs, we subtract the sampled sequence log-ratio \(\log(\pi/p)\) from the reward with coefficient one (\(\beta = 1\)), subtract the group mean, and keep the advantage in its original units.
Why call it an energy?
Write each reward term as an energy, \(E_i = -r_i\), so lower is better. The target becomes
the same shape as a Boltzmann distribution in physics, where states with lower energy are exponentially more likely. Here the reference model \(p\) plays the prior, \(\beta\) the temperature, and \(\lambda_i\) how much energy one unit of each penalty costs. The useful intuition is about odds. Take two trajectories that are identical except that one carries out the injected instruction: the target multiplies their odds by \(e^{-\lambda/\beta}\). At \(\lambda = \beta = 1\), the odds of the hijacked trajectory relative to the equally successful safe one fall to \(1/e\) (about 0.37) of their original value; at \(\lambda = 3\), to \(e^{-3}\), about 1/20. Energies from different sources add, so their odds factors multiply, and that is the composition rule below, seen from the other side. A tool-call count, a lint count, a binary checker or a learned score can all play the role of an energy.
The composition rule
If separately trained policies reach their respective targets for a shared base \(p\) and the same KL coefficient \(\beta\), then at the sequence level
For the agent, let \(T\) be trained on the task reward and \(S\) on the task reward plus the security penalty. Then \(S/T\) carries the penalty alone, and mixing the two gives a target for any amount of caution, with no retraining:
The identities are exact for fixed rewards and policies that reach their targets. Two things can break them in practice. Online scores such as answer consensus depend on the other samples and on the policy generating them, so they change during training. And a trained policy's log-ratio to the base only approximates the reward it was trained on; for the agent, Section 05 shows that the real \(S/T\) also carries what task-only training on the injected episodes learns. How closely real policies get there is an empirical question, and the rest of the post measures it: first on agent security, then on code and math. Training with a KL coefficient of one and no division by the group's standard deviation gives the policy ratio a specific reward interpretation we can test. Whether the KL term sits in the reward, as in our runs, or in the loss does not change that ideal optimum. And none of this is a precondition for mixing: standard-GRPO experts also mixed well in our code experiments (Section 09).
Want the demonstration first? Jump to the interactive caution dial →
02Reward terms from any source
Each term in the sum is a cheap score of an output. The terms can come from almost anywhere, and nothing requires them to be trained together:
Adding an objective adds a term. Re-weighting changes a \(\lambda_i\). Negating flips a sign. The object being edited is always the same weighted sum.
03Four uses of one target
Sample, weight each output by the exponential of its summed reward, resample.
MATH500 +5.8Distil the reweighted samples into the model.
−18% lengthThe weighted sum as the GRPO reward, with a KL coefficient of one, so that the optimum is this target.
optimum = targetMix trained policies token by token, \(p \prod_i (\pi_i / p)^{c_i}\), with an importance-weight correction.
no retrainingBoth on Qwen2.5-Math-1.5B (base). Use 1: resampling 16 base samples by a verifier head (λ = 2) raises MATH500 per-sample accuracy from 41.1 to 46.9% against plain sampling at temperature 0.6, at unchanged pass@8; on GSM8K it beats every sampling temperature on both accuracy and pass@8. Use 2: fine-tuning on samples reweighted by verifier + conciseness heads cuts MATH500 answer length by 18% (242 → 199 tokens) against fine-tuning on the verifier alone, at similar accuracy (48.4 vs 48.2%); it also beats a weight soup of the two single-objective models.
04RL can make agents easier to hijack, and a security penalty fixes it
AgentDojo plants attacker instructions inside tool outputs (an email body, a web page) and checks whether the agent carries them out; its tools are in-memory simulations, so nothing real is executed. Our BFCL-trained Qwen2.5-7B agents turned out about 50% more hijackable than the instruct model they started from: 12.3% → 18.4% and 18.3% in two seeds, on the official benchmark. Chat safety barely moved, so a chat-safety eval would have missed it.
We add a security penalty to the reward: \(r = \mathbb{1}[\text{task done}] - \mathbb{1}[\text{injected goal executed}]\). In the notation of Section 01, the indicator is the energy (lower is better) and its negative is the reward contribution. The fair comparison is task-only RL on exactly the same injected episodes, which already helps a lot; the penalty goes further.
The headline, with its control. On held-out Qwen episodes using the trained attack template, task-only training already cut hijacking from 17.8% to 5.1–6.0%. Adding the security penalty brought it down to 1.8–2.9%, at a cost of 5–7 points of clean-task utility versus that control. Then turn the caution up or down at inference →
On held-out users and held-out injection goals:
Does it transfer to a kind of scenario it never saw?
Every suite above (email and calendar, Slack, travel, banking) appears in training. So we also held out one whole suite at a time, its tools, data, user tasks and attacker goals, trained on the other three, and compared against task-only RL on exactly the same episodes.
Show the table
| Held-out suite | Seen: start / control / security | Unseen: start / control / security |
|---|---|---|
| Slack | 61.0 / 28.1 / 9.8 | 85.0 / 61.4 / 32.9 |
| Banking | 33.2 / 21.5 / 12.8 | 31.6 / 22.6 / 16.1 |
| Travel | 25.0 / 2.7 / 0.2 | 47.3 / 26.8 / 14.6 |
| Email, calendar, files | 2.9 / 0.6 / 0.4 | 8.4 / 2.9 / 1.8 |
Attack families never used in training
All attacks so far are AgentDojo's own templates. AgentDojo also ships attack families we never used anywhere in training: direct, injecagent, system_message, and a variant of the training template with the names removed. On the same 204 held-out episodes per attack (our harness, 4 samples each), pooled over the four:
| Hijacked, pooled over 4 new attacks | Qwen2.5-7B | Gemma 4 26B |
|---|---|---|
| Instruct model | 8.2% | 4.8% |
| Task agent T (BFCL-only RL) | 13.7% | – |
| Control: task-only RL on injected episodes (2 seeds) | 6.2 / 7.6% | 3.4 / 3.2% |
| 3.7 / 4.9% | 2.1 / 2.2% | |
| Distilled dial (a = 1.5) | 1.8% | – |
BFCL-only RL (the task agent T vs the instruct model) raised hijacking on every one of the four new attacks (+3.8 to +8.3 points). The security penalty beats its matched control (task-only RL on the injected episodes) in both families (Qwen −2.5 and −2.7, Gemma −1.3 and −1.0, all significant). The gain is largest on the closest variant of the training template (−4.8 to −5.9 points on Qwen), smaller on injecagent and system_message, and not significant on direct, where hijacking is already low. Gemma sits at about 2% on the three different families for every model, so only the template variant moves. Qwen seed 2 again pays utility under attack (−5.7 points vs its control); seed 1 does not (−1.4, not significant).
What about defences that need no training?
AgentDojo ships three defences we could run: delimiting tool outputs and telling the model not to follow instructions inside them ("spotlighting"), repeating the user's request after every tool call, and a prompt-injection detector (a DeBERTa classifier) that replaces any tool output it flags with "data omitted". We ran them on the official benchmark (greedy decoding, held-out episodes), alone and on top of our models:
| Hijacked, seen / unseen template | No defence | Spotlighting | Repeat user prompt | Injection detector |
|---|---|---|---|---|
| Qwen2.5-7B-Instruct | 9.3 / 12.7 | 9.8 / 14.7 | 10.3 / 10.8 | 2.9 / 8.8 |
| Task agent T (BFCL-only RL) | 17.6 / 31.4 | 18.6 / 29.4 | 17.2 / 24.5 | 8.3 / 13.7 |
| Control: task-only RL on injected episodes | 6.4 / 13.7 | 8.8 / 12.7 | 9.3 / 10.3 | 4.4 / 8.3 |
| 4.9 / 8.8 | 4.4 / 9.3 | 5.4 / 7.4 | 2.9 / 5.4 | |
| Utility under attack: instruct model / + security penalty | 33.8 / 34.8 | 30.4 / 32.8 | 36.3 / 43.6 | 16.2 / 19.6 |
Qwen2.5-7B, one seed of each trained model. The two prompting defences barely change hijacking for any model (the one significant effect: repeat-user-prompt lowers the task agent's unseen-template rate by 6.9 points). The detector is the one that clearly works: it lowers hijacking for every model, most for the task agent (−9.3 / −17.6 points), but it also blanks legitimate tool outputs, and utility under attack falls by 12.7 to 17.6 points for every model. The security penalty alone is hijacked less than the instruct model with either prompting defence (spotlighting −4.9 / −5.9, significant; repeat-user-prompt −5.4 seen, significant, −2.0 unseen, not), and about as often as the instruct model with the detector (+2.0 / 0.0, not significant) at 18.6 points more utility under attack. It is hijacked less than task-only training under every defence (significant on at least one template each). Stacking: a prompting defence on top of the penalty adds no security, though repeat-user-prompt raises utility under attack by 8.8 points; the detector on top gives the lowest hijacking in the table (−2.0 / −3.4 vs the penalty alone, significant) at 15.2 points of utility under attack. AgentDojo's tool-filter defence only supports OpenAI-API models, so we could not run it.
05The caution dial
The security-trained policy S and the task agent T it started from can be mixed at decode time, \(T^{1-a} S^{a}\), with no retraining. Drag the dial: hijacking falls smoothly while task success stays flat, and going past the trained policy (a > 1) buys more safety. (Qwen2.5-7B. The dial mixes two models, so every setting other than a = 0 or 1 costs two forward passes per token; the transfer buttons mix a third agent with \(S/T\) or \(S/C\), three per token.)
a = 0 is the task agent; a = 1 is the security-trained policy.
Measured settings only, held-out AgentDojo and BFCL episodes. The dial runs its own token-by-token decoder (temperature 0.7, full softmax), so its end points are fresh samples: a = 0 reads 16.9 / 25.0% hijacked and 47.5% BFCL where our standard vLLM evaluation of T reads 17.8 / 27.0% and 48.9%, and a = 1 reads 1.5% seen where S reads 1.8%. These are two sampled evaluations of the same models. a = 0.5 vs a = 0: hijacking −10.5 (seen) and −10.3 (unseen), significant. The dial shown uses security seed one; it replicates with seed two (at a = 1.5: 1.2% seen and 3.2% unseen hijacking, no significant difference from seed one). The two transfer buttons add a learned direction, \((S/T)^{a}\) or \((S/C)^{a}\), to a different agent that never saw AgentDojo. S/T (security policy over the task agent it started from) carries everything learned from training on the injected episodes, and cuts that agent's hijacking by 13.2 and 16.7 points. But task-only training on the same episodes (the control C) already learns most of that. S/C, the part due to the security penalty alone, transfers more weakly: hijacking −6.1 points on the unseen template (significant) and −3.2 on the seen one (not significant), with utility under attack +6.1.
Where the dial stops working. On Gemma 4 26B (Section 06), turning the dial from a = 1 to a = 2 gives no significant additional security gain and costs 2.5 points of utility under attack: most remaining hijacks there are ambiguous episodes that every model fails. Extrapolation helped on Qwen because its security policy still had room to improve.
Then distil the dial back into one model
Mixing two policies costs two forward passes per token. So we sampled the dial at a = 1.5 on the training episodes and fine-tuned the task agent on those samples. No checker filters them, so no labels are used:
Hijacked: the training-time attack template / the one held-out template used for the dial (tool_knowledge). s1, s2: students distilled from the dials of security seeds one and two; steps 1 and 2 show seed one. Each student is statistically indistinguishable from the dial it learned from and is hijacked less than its own RL-trained security policy on the held-out template (−3.7 for seed one; −5.9 for seed two, which is also lower on the training template, −1.6). Weighting the samples by their exact importance weights gave the same result. (The task agent T alone, standard vLLM evaluation: 17.8 / 27.0% hijacked, BFCL 48.9%; the dial at a = 0 reads 16.9 / 25.0%.)
06A second family: a 26B mixture-of-experts
We repeated the security experiment on Gemma 4 26B-A4B (instruction-tuned, about 4B active parameters) with full-parameter RL. Its results are in the Gemma tab of the chart in Section 04: hijacking 5.4 / 4.8% after task-only RL, 2.5 / 2.1% with the security penalty (−2.9 and −2.7, significant), with no significant loss in clean utility or BFCL.
Strong models leave RL almost nothing to learn on standard benchmarks. Gemma solves BFCL 69% of the time and AgentDojo tasks 86%, and only 5–18% of training episodes give the mixed outcomes GRPO learns from. So we built harder training episodes inside AgentDojo, graded by its own checks:
The caution dial works on Gemma too (hijacking 5.4 → 3.7 → 2.5 → 2.0% for a = 0, 0.5, 1, 2), though going from a = 1 to 2 adds no significant safety and costs 2.5 points of utility under attack. Adding the penalty-only direction S/C (relative to the matched task-only control) to the untrained base halves its hijacking (7.6 → 3.4%), at a cost of 4 points of clean utility. Effects are smaller than on Qwen because Gemma starts low and about 2.5% of episodes are ambiguous by construction (for example, a user task that says "follow the instructions in the file precisely").
07Does training reach the target?
Section 01 defined the target; here we check whether trained policies actually land on it. Standard GRPO has the same kind of ideal optimum, but with a tiny KL coefficient, so its target puts nearly all mass on the highest-reward outputs. As usually configured it also divides advantages by the group's standard deviation, which rescales the reward prompt by prompt; our code baseline did not (every code arm here uses group-centred advantages without that division), so the difference between our two code arms is the KL coefficient (0.001 as a loss, against 1 in the reward) and how far training got. In our code runs the standard-GRPO policy at KL weight 0.001 ends up well off the predicted family, which is where the side effects showed up. With the KL term still in the loss, raising its coefficient from 0.001 to 0.05 reduced the four-metric distance from 3.1 to 0.6, like the tilt-GRPO runs, at a cost of 2.5 pass@1 points. So in this Qwen code run a larger coefficient was enough; the sweep does not test whether placement matters at a matched coefficient.
With a KL coefficient of one and no division by the group's standard deviation (we put the KL term in the reward and call this regime tilt-GRPO), the predicted family can be computed before any training, by reweighting base-model samples by \(e^{\lambda r}\). Each trained checkpoint can then be placed on, or off, that family. The distance to the family is the root mean square, over four observables (pass rate, share of lint-clean solutions, length, and effective number of distinct correct solutions), of the gap between the policy and its best-fit family member, with each gap divided by that observable's measured single-run seed-to-seed spread (0.2 points, 3.9 points, 4.6 tokens and 0.29 solutions).
Not a new formula. The tilted optimum of KL-regularized RL is classic: it is the derivation behind DPO, and PPO-based RLHF put the KL penalty in the reward before GRPO moved it to the loss. What we add is using this regime on purpose, with a reward built from several terms, then measuring that trained policies land on the target and compose.
Honest caveat. Tilt-GRPO moves along the family, but slower than the nominal λ (10–20% of it at our budgets). A pre-registered "pick λ from base samples, train once" test failed: it predicted an effective strength of 1.35–1.75 where training reached 0.95–1.25. What we did establish: the Qwen correctness runs landed on the predicted family on four measured statistics; where along it they land had to be fit.
08Composition repairs RL side effects
Training a code model only for correctness, with GRPO's usual tiny KL weight (0.001 here), makes its code messier. Adding a lint penalty to the reward removes the mess at a small pass@1 cost, and the composed reward beats the obvious alternative, averaging the weights of a correctness model and a lint model (Rewarded Soups).
A simpler fix to check first: a stronger KL weight. Much of this side effect comes from the tiny KL weight. With the same correctness-only reward and a KL weight of 0.05, standard GRPO keeps the code at least as clean as the base model and restores its diversity (table below), at 2.5 points of pass@1 after the same 39 steps. The lint penalty is still worth having when you want cleaner code than the base model: 89% lint-clean at 63.3 pass@1, against 73.5% at 61.6 for the stronger KL weight.
| Correctness-only GRPO, held-out KodCode | pass@1 | pass@8 | lint-clean | distinct correct solutions |
|---|---|---|---|---|
| Base model | 56.4 | 73.9 | 70.0 | 6.26 |
| KL weight 0.001 (two seeds) | 64.1 / 63.9 | 76.7 / 76.7 | 58.9 / 68.4 | 4.80 / 4.86 |
| KL weight 0.01 | 63.6 | 76.8 | 72.0 | 5.29 |
| KL weight 0.05 | 61.6 | 76.2 | 73.5 | 6.48 |
| KL weight 0.001 + lint penalty (two seeds) | 63.3 / 63.2 | 76.7 / 76.6 | 89.3 / 89.5 | – |
Qwen2.5-Coder-1.5B, 39 steps, standard GRPO with the KL term as a loss and no std division; one seed for the stronger KL weights. Against KL 0.001 (seed 1): KL 0.05 has −2.5 pass@1 (significant), −0.5 pass@8 (not significant), +14.6 lint-clean (significant). Distinct solutions: anonymised syntax trees, mean over problems (the base model's count comes from a second evaluation of the same model).
What it actually changes
We looked at the code. Most of the lint gain is formatting debt that correctness RL itself introduced (whitespace on blank lines); substantive findings barely move. We report it that way.
Correctness-only GRPO 2 × whitespace on blank line
def reverse_string_by_words(s: str) -> str:
if not s:
return ""
····
words = s.split()
words.reverse()
····
return ' '.join(words)+ lint penalty 0 findings
def reverse_string_by_words(s: str) -> str:
if not s:
return s
words = s.split()
reversed_words = words[::-1]
return ' '.join(reversed_words)A held-out problem; both versions pass the unit tests. Red dots mark invisible indentation. Correctness-only: 16 of 16 correct samples have a finding; with the lint penalty, 0 of 16.
Diversity, too
Distinct correct solutions per problem after anonymising identifiers. A gated diversity bonus with a length term, two seeds, at a cost of 2.0 and 2.3 points of pass@1. A stronger KL weight (0.05) also undoes the collapse, to 6.48, at a similar pass@1 cost; the diversity bonus goes further (7.0–7.7) and is the arm that raises pass@8 (77.9 / 78.5, against 76.2 for KL 0.05).
09Train once, mix at decode time
If each RL-trained policy is (approximately) the base model tilted by its own reward, then a product of policies is the base model tilted by the sum of rewards. So instead of training a joint model, train each objective once and combine at inference:
Why is the token-by-token mix not exact? A prefix's soft value for an objective is the expected exponentiated reward of the completions that start with it, under the base model: how promising that prefix is for the objective. Multiplying two policies' next-token probabilities gets the sequence-level target exactly right when their soft values do not move together across the candidate next tokens; otherwise our theory gives the error exactly, through that conditional covariance, and a sequence-level importance weight corrects it. At equal weights, every comparison we ran lands within 3 points of joint training:
| Equal-weight mix vs jointly trained policy (code) | Pass rate | Clean code |
|---|---|---|
| Qwen, standard-GRPO experts, token-level mix (the chart above) | −1.5 | +1.2 |
| Same, resampled with the exact sequence weights | −1.7 | +1.6 |
| deepseek-coder, standard-GRPO experts, token-level mix | −1.4 | −1.3 |
| Qwen, experts trained with KL coefficient one, token-level mix | −1.1 | −2.9 |
| Label-free: consensus and lint policies, token-level mix | −0.7 | +0.2 |
Points versus the jointly trained policy of the same training regime, 1,000 held-out problems. Resampling changes the equal-weight results by under a point in every row. Away from equal weights the picture changes: at weight 2 the dial loses 7.0 points of pass rate against joint training, and at the negative weight, resampling pushes clean code from 28% down to 10%.
10A normalization pitfall
GRPO usually divides advantages by the group's standard deviation. With composed rewards that is not harmless: when the primary reward term is constant within a group (all correct, or all wrong), dividing by the spread of the remaining terms silently rescales them prompt by prompt. Per-reward z-scoring (as in GDPO) does the same with a different weighting. Raising a conciseness weight \(\gamma\) from 0.1 to 0.3 on math:
On code, where lint varies inside every group, GDPO and our reward perform the same, as the analysis predicts.
11Tool-use agents
On multi-turn tool use (BFCL, held-out tasks, Qwen2.5-7B-Instruct) we wrote a token-exact rollout harness and a safe executor that never evaluates model text. Success-only RL nearly doubles task success but adds tool calls. A gated tool-cost penalty keeps the full gain and removes them.
Both arms at iteration 60, two seeds. Composed vs success-only: success not significantly different; calls −1.4 and −3.1, failed calls −0.26 and −0.19 (significant).
12What did not work
14Takeaways
15What I'm thinking about next
Can one model learn the whole caution dial? Right now I distil one setting at a time: set the teacher's dial to 1.5, collect its trajectories, and fine-tune a student that runs at normal cost. The next step is a single student that takes the dial value as an input. The test I have in mind: train it on a few settings, leave one out, and ask for that missing setting on a tool suite it has never seen. If it lands where the teacher would, the dial no longer needs two models at inference. Hijacking and task success have to be checked together here, because a student that simply refuses more often can look safer while losing the behaviour we wanted to keep.
Does the dial survive the agent learning a new tool suite? Adding a skill is exactly the kind of update that broke security the first time. The plan is to train a policy on the new suite alone, compose it with the current agent, and compare that with plain sequential RL and with training on a mix of old and new tasks, then turn the caution dial again. Two things to watch: whether the agent starts using familiar tools differently when the suites share them, and whether it is better to keep the security penalty on during the update or to add the security direction back afterwards. As everywhere in this post, the comparison is an agent trained on the same episodes without the penalty.
Can reward weights be chosen before paying for a run? On code, reweighting base samples previewed the trade-off well, but my guess of how far training would actually move was too optimistic. So the first question is a smaller one: does the preview at least rank which settings are worth training? On long agent trajectories there is a second question, how many samples really carry the estimate, since a handful can end up dominating it.
If you're working on any of these questions, or want to try the dial on your own agents, I'd love to hear from you.
Citation
To cite this post:
@misc{runwal2026onetarget,
title = {One target, many objectives: composing rewards in language model post-training},
author = {Bharat Runwal},
year = {2026},
note = {Blog post},
url = {https://bharat-runwal.github.io/composing-rewards/}
}