CAPO: Constraint-Aware Prompt Optimization for LLM Agents
This paper introduces CAPO, a constraint-aware prompt optimization method that uses a primal-dual approach with adaptive constraint weighting to effectively optimize LLM agent system prompts for task performance while adhering to operational constraints like safety and formatting, alongside a dynamic variant (DCAPO) that trains a specialized rewriter to achieve feasible and high-performing prompts across diverse domains without modifying the underlying task agent.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Large language models are increasingly being deployed not just to chat, but to act. These digital agents are given a set of instructions, known as a system prompt, which tells them how to use tools, solve problems, and interact with users. Imagine a travel agent who can book flights, check insurance, and cancel reservations, but only if they follow a strict set of company rules. The challenge for developers is that a model can be incredibly good at solving a task while still failing to meet the specific operational requirements of a real-world job. It might use too many tools, ask for human help when it shouldn't, write responses that are too long, or accidentally violate safety policies. For a company to deploy such an agent, it cannot simply accept a model that is "mostly" good; it must ensure the agent stays within strict boundaries for every single requirement while still getting the job done.
The core difficulty lies in the fact that these requirements often pull in different directions. A prompt that makes an agent very accurate might also make it verbose or prone to using too many tools. Traditionally, developers have tried to solve this by manually adjusting the importance of each rule, essentially guessing which constraints matter most. However, this approach is fragile. A setting that works for one type of task or one specific model often fails when the situation changes. If the rules are fixed in advance, the system might satisfy one requirement by breaking another, leaving the agent unusable for deployment. The question researchers faced was whether they could build a system that automatically finds the perfect balance, adjusting the weight of each rule in real-time as it learns what works and what doesn't.
A team of researchers introduced a new method called CAPO, which stands for Constraint-Aware Prompt Optimization, to tackle this problem. Instead of guessing the right balance of rules, CAPO treats the optimization process like a negotiation where the rules themselves speak up. The system starts with a collection of candidate prompts and tests them against a set of tasks. As the agents run, they generate data on how well they performed and, crucially, how much they violated specific constraints, such as the number of tool calls or the length of the response. If a prompt violates a rule, the system automatically increases the "penalty" for that specific rule in the next round of testing. If a prompt has extra room under a rule, the penalty for that rule decreases. This dynamic adjustment allows the system to focus its search on the specific problems that are currently blocking the agent from being deployed, rather than treating all rules as equally important from the start.
The researchers tested this approach across several different domains, including airline customer service, retail support, and telecommunications. In these tests, the agents had to complete tasks while adhering to strict limits on how many times they could call a human agent, how many tools they could use, and how long their system instructions could be. The results were clear: CAPO consistently found prompts that satisfied every single constraint while maintaining high task performance. In contrast, other methods that used fixed weights or static rules failed to find a solution that worked for all requirements simultaneously. For instance, in the airline domain, while other methods managed to satisfy the rules in only one out of six different test scenarios, CAPO found a feasible solution in every single one. The method proved robust enough to work with different sizes of language models and even extended beyond tool-using agents to handle safety and formatting constraints in chatbot scenarios.
To make the process even more efficient, the researchers developed a second version called DCAPO. While the first version uses a separate, frozen language model to rewrite the prompts, DCAPO trains a specialized rewriter to learn from the process itself. This rewriter observes the behavior of the agent and the feedback from the constraints, learning to generate better prompts over time without ever changing the underlying task agent. In their experiments, this learned rewriter was able to produce feasible prompts across all tested domains, matching or improving the accuracy of the best baseline methods. The study also included a mathematical analysis to show how the system handles the fact that it is working with a limited number of examples and discrete text changes, confirming that the errors introduced by these limitations do not prevent the system from converging on a good solution.
The significance of this work lies in its ability to turn deployment requirements into a guiding force for the search process. By letting the measured violations of the rules determine which failures receive attention, the system avoids the trap of fixing one problem only to create another. The researchers found that this adaptive approach is essential because the most critical constraint changes depending on the specific task and the model being used. What matters most in an airline scenario might be irrelevant in a retail setting, and a model that is capable enough to solve a problem might still need different guidance to stay within budget. The study demonstrates that by continuously adjusting the focus of the optimization based on real-time feedback, it is possible to find operating points that are both effective and compliant. This suggests a shift in how we think about prompt engineering: rather than a static setup, it can be a dynamic process where the requirements themselves help steer the system toward a solution that is ready for the real world.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.