一句话:提出轻量级 DSL AgentSpec,在运行时对 LLM 智能体施加可定制的安全约束,并支持用 LLM 自动生成规则。
基于 LLM 的智能体被广泛部署于复杂决策与任务执行,但其自主性带来安全漏洞、违法与有害行为等风险;现有基于模型的防护与早期干预方法在鲁棒性、可解释性和适应性方面不足。本文提出 AgentSpec,一种轻量级领域特定语言,用于在运行时指定并执行对 LLM 智能体的约束:用户以触发器、谓词和执法机制三类要素编写结构化规则,使智能体在预定安全边界内运行。作者在代码执行、具身智能体与自动驾驶等多个领域实现了 AgentSpec 并加以评估:其阻止了代码智能体超过 90% 的不安全执行,消除了具身任务中的全部危险动作,并使自动驾驶车辆实现 100% 合规,同时运行时开销仅为毫秒级。此外,作者还利用 LLM 自动生成规则并评估其效果:OpenAI o1 生成的规则在具身智能体上达到 95.56% 精确率与 70.96% 召回率,能识别 87.26% 的风险代码,并在 8 个场景中的 5 个里阻止自动驾驶车辆违法。
Agents built on LLMs are increasingly deployed across diverse domains, automating complex decision-making and task execution. However, their autonomy introduces safety risks, including security vulnerabilities, legal violations, and unintended harmful actions. Existing mitigation methods, such as model-based safeguards and early enforcement strategies, fall short in robustness, interpretability, and adaptability. To address these challenges, we propose AgentSpec, a lightweight domain-specific language for specifying and enforcing runtime constraints on LLM agents. With AgentSpec, users define structured rules that incorporate triggers, predicates, and enforcement mechanisms, ensuring agents operate within predefined safety boundaries. We implement AgentSpec across multiple domains, including code execution, embodied agents, and autonomous driving, demonstrating its adaptability and effectiveness. Our evaluation shows that AgentSpec successfully prevents unsafe executions in over 90% of code agent cases, eliminates all hazardous actions in embodied agent tasks, and enforces 100% compliance by autonomous vehicles (AVs). Despite its strong safety guarantees, AgentSpec remains computationally lightweight, with overheads in milliseconds. By combining interpretability, modularity, and efficiency, AgentSpec provides a practical and scalable solution for enforcing LLM agent safety across diverse applications. We also automate the generation of rules using LLMs and assess their effectiveness. Our evaluation shows that the rules generated by OpenAI o1 achieve a precision of 95.56% and recall of 70.96% for embodied agents, successfully identify 87.26% of the risky code, and prevent AVs from breaking laws in 5 out of 8 scenarios.