Abstract
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.
Introduction
Large Language Model (LLM) agents are evolving from simple conversational interfaces into autonomous systems capable of planning and executing complex tasks in the real world. This autonomy, while powerful, introduces significant safety risks. For instance, an agent tasked with server optimization might inadvertently delete critical system files, or a robotic agent might attempt to clean a laptop by pouring water on it.
Current safety paradigms generally fall into two categories: model-based safeguards and sandboxing. Model-based approaches rely on the LLM itself to adhere to safety guidelines, but these are often vulnerable to adversarial manipulation. Sandboxing can identify risks but lacks a clear, real-time mechanism to intervene during execution. There is a critical need for a flexible, human-readable, and programmable method to enforce domain-specific safety policies during runtime.
The AgentSpec Framework
To address these gaps, we introduce AgentSpec, a lightweight domain-specific language (DSL) designed for structured safety rule enforcement. AgentSpec decouples safety logic from the agent's core reasoning, ensuring that safety policies remain transparent and verifiable even when the underlying LLM is a "black box."
The framework is designed to be framework-agnostic. While implemented for LangChain in this study, it can be integrated into other ecosystems like Microsoft’s AutoGen or autonomous vehicle stacks. As shown in the architecture, AgentSpec hooks into the agent's pipeline at three critical points: before an action is taken, after an observation is made, and upon task completion.
Rule Specification and Enforcement
Every AgentSpec rule follows a structured logic consisting of three components:
- Trigger: An event that activates the rule, such as a specific tool call or a state change.
- Check: A set of predicates or conditions that must be evaluated (e.g., checking if a destination is a family member or measuring obstacle distance).
- Enforce: The intervention mechanism triggered if the check fails. Enforcement actions can range from halting the agent (
stop) to requiringuser_inspectionor prompting the agent forllm_self_examine.
This modularity allows developers to define precise safety boundaries tailored to specific domains, such as robotics, software engineering, or autonomous driving.
Evaluation and Results
AgentSpec was evaluated across three high-risk domains using specialized benchmarks:
- Code Execution: Using RedCode-Exec, which covers 25 vulnerability types including path traversal and command injection. AgentSpec detected and intercepted risks in over 90% of cases.
- Embodied Agents (Robotics): Using SafeAgentBench, testing hazards like fire and electrical shocks. AgentSpec eliminated 100% of hazardous actions across 10 categories.
- Autonomous Driving: Using FixDrive, AgentSpec achieved 100% compliance with traffic laws in tested scenarios.
A key finding is that safety enforcement does not necessarily compromise performance. AgentSpec's computational overhead is measured in milliseconds, which is negligible compared to the seconds typically required for LLM reasoning. Furthermore, the study explored using LLMs (specifically OpenAI’s o1) to automatically generate AgentSpec rules. These automated rules achieved a 95.56% precision rate for robotic tasks, demonstrating the potential for scalable, automated safety policy generation.
Conclusion and Future Work
AgentSpec provides a practical and scalable solution for enforcing LLM agent safety. By externalizing safety rules into a verifiable DSL, it offers a robust alternative to model-based safeguards.
Current limitations include the deterministic nature of the rules, which focus on immediate actions rather than long-term consequences. Future research will explore probabilistic enforcement and "model-based foresight" to proactively intervene before unsafe states are reached. Additionally, while automated rule generation is promising, further work is needed to prevent "overfitting," where rules become overly restrictive and block safe, intended actions.
Summary updated