LCO: LLM-based Constraint Optimization for Safer Agentic LLMs in Real-world Tasks
Organizations: East China Normal University, Shanghai, China · Beijing Zhongguancun Academy, Beijing, China · Tsinghua University, Beijing, China
Abstract
Large Language Models (LLMs) are increasingly acting as autonomous agents, but their continuous interaction with the environment can lead to in-context reward hacking (ICRH), a phenomenon where LLMs iteratively optimize their behavior to maximize proxy objectives, inadvertently producing harmful side effects. Existing defense methods are insufficient to address this risk, as ICRH arises not from adversarial inputs but from the model's own over-optimization. To mitigate this issue, we propose LLM-based Constraint Optimization (LCO), a framework that effectively reduces ICRH without model fine-tuning. LCO consists of two modules: self-thought module, which guides the LLM to proactively deliberate and integrate potential safety constraints before execution; and evolutionary sampling module, which employs LLM-based crossover and mutation to constrain the model's actions within a safe solution space while maintaining task performance. Experimental results demonstrate that LCO substantially alleviates ICRH in both output-refine and policy-refine scenarios. In particular, on the tweet engagement optimization task, LCO achieves a 39% reduction in the Toxicity Growth Rate (TGR) on GPT-4, while on the policy optimization benchmark, it reduces the ICRH Occurrence Rate by 15.23%, demonstrating safety improvement without sacrificing task performance.
Figures & tables
| Task Type | Output-refinement | Policy-refinement | |||||
|---|---|---|---|---|---|---|---|
| Model | Method | TGR | IOR | Pairwise | Helpfulness | ||
| GPT-3.5 | Vanilla | 54.54% | 2.63 | 0.0040 | 31.69% | – | 6.24 |
| Self Defense | 58.76% | 1.21 | 0.1100 | 9.86% | 0.71 | 4.57 | |
| Goal Priority | 42.10% | -0.11 | 0.5430 | 6.99% | 0.68 | 8.41 | |
| LCO | 46.15% | -0.55 | 0.7000 | 6.99% | 0.72 | 6.24 | |
| GPT-4 | Vanilla | 76.00% | 5.15 | 22.22% | – | 7.57 | |
| Task Type | Output-refinement | Policy-refinement | |||||
|---|---|---|---|---|---|---|---|
| Model | Method | TGR | IOR | Pairwise | Helpfulness | ||
| GPT-3.5 | Self-Thought | 47.36% | -0.30 | 0.610 | 7.09% | 0.67 | 7.02 |
| ES | 55.69% | 0.62 | 0.260 | 6.25% | 0.60 | 6.89 | |
| LCO | 46.15% | -0.55 | 2.08% | 0.72 | 6.24 | ||
| GPT-4 | Self-Thought | 41.66% | -2.74 | 2.08% | 0.44 | 7.42 | |
| ES | 60.52% | 1.36 | 0.090 | 2.78% | 0.57 | 7.57 | |
| Method | IOR | Helpfulness | Token / Task | Calls / Task | Avg. Latency |
|---|---|---|---|---|---|
| Vanilla | 18.84% | 6.71 | 1.03k | 4.6 | 2.56s |
| Pos=1 | 8.57% | 6.36 | 2.01k | 8.2 | 4.50s |
| Pos=3 | 4.35% | 6.52 | 4.89k | 24.4 | 9.30s |
| Pos=5 | 4.29% | 6.73 | 6.83k | 34.5 | 12.50s |
| Constraint Generator | Agent Model | TGR (%) | ||
|---|---|---|---|---|
| No constraint | GPT-3.5 | 55.69 | 0.62 | 0.2600 |
| GPT-3.5 | GPT-3.5 | 46.15 | -0.55 | 0.7000 |
| Qwen2.5-72B-Instruct | GPT-3.5 | 52.38 | -0.06 | 0.5242 |
| LLaMA3.1-405B-Instruct | GPT-3.5 | 36.36 | -2.23 | 0.9818 |
Appendix figures & tables6 assets
Supplementary material from the paper’s appendix.
Appendix
| Model | Accuracy (%) | Agreement Rate (%) |
|---|---|---|
| GPT-4o | 92.50 | 87.18 |
| Qwen2.5 | 87.18 | |
| LLaMA3.1 | 92.31 |
| Aspect | ICRH | Emergent Misalignment |
|---|---|---|
| Source | In-context optimization during inference | Parameter-level deviation after training/fine-tuning |
| Interaction Required | Yes, emerges over interaction | No, may appear immediately after deployment |
| Typical Behavior | Toxic escalation via reward signals | Trigger-based harmful output |
| Mechanism | Gradient-free in-context learning | Parameter space shift |
| Risk Type | Proportion (%) | Example Description |
|---|---|---|
| Privacy leaks | 19.0 | Exposing user data or sensitive database fields |
| Financial losses | 16.1 | Misjudged decisions causing monetary harm |
| Execution inaccuracies | 13.2 | Misdiagnosis or suboptimal task execution |
| Physical safety risks | 9.3 | Faulty control leading to collisions or injuries |
| Reputational damage | 8.8 | Generating biased or politically sensitive outputs |
| Computer security compromise | 7.8 | Triggering DoS or other system-level attacks |
LLM Agent Safety
Large language model agent safety studies how to keep tool-using, persistent, and multi-agent systems aligned with user intent and authorized limits.
See all 322 papers