August 10, 2025
AI agents are becoming increasingly capable and increasingly autonomous. They no longer behave like traditional chatbots that simply generate text. Instead, modern agents can read documents, call APIs, access internal systems, perform multi-step reasoning and take meaningful actions in the real world. As these agents move closer to production environments, especially in enterprise settings, their security surface grows dramatically.
Fundamentally, an AI agent is software that can act, not just software that talks. That shift requires a new mindset. Traditional prompt engineering alone is no longer enough. The challenge is not merely generating safe text but ensuring that every action triggered by an agent is authorized, predictable and aligned with the boundaries we set for it.
This article walks through practical, grounded best practices for securely building AI agents. It focuses on real-world risks and approaches observed across today’s agent ecosystems, without requiring any specific framework or tool.
Before wiring tools or actions into an agent, it is essential to define what the agent should and should not be able to do. This process does not need to be complicated, but it should be concrete.
It begins with understanding the agent’s purpose. Is it summarizing internal documentation? Is it helping customer support representatives? Is it making outward-facing API calls? Or is it orchestrating workflows with multiple tools? The clearer this is, the easier it becomes to constrain behavior appropriately.
Next, it’s important to understand the data accessible to the agent. Agents often operate on a mix of sensitive and non-sensitive inputs, including user queries, documents, logs and structured records. Without knowing what the agent can see, you cannot meaningfully evaluate misuse or potential exposure.
Finally, consider the worst-case outcomes if the agent misbehaves accidentally or maliciously. Could it spill secrets? Modify a database? Trigger payments? Email sensitive logs? The potential blast radius influences every downstream decision including tool permissions, execution environments and monitoring requirements.
A simple one-page threat model can save months of problems later.
The most important security control for AI agents is ensuring they have only the minimal access they absolutely need. Tools serve as the “hands” of the agent. If you expose a powerful tool, the agent can use it even in ways you didn’t intend.
One common mistake is giving an agent a generic, highly flexible tool, such as a raw SQL execution function or an unrestricted HTTP client. Although convenient, these create enormous risk. A better approach is to expose highly scoped tools designed for specific tasks. Instead of letting the agent run any SQL query, expose a tool that retrieves a customer profile by ID or retrieves a sanitized dataset. Instead of handing over a generic network client, expose a tool that calls a carefully constrained endpoint or a small set of verified domains.
Credentials should be similarly limited. Avoid reusing production secrets or broad-scoped tokens when building agent capabilities. Create new credentials with the smallest possible set of permissions, ideally scoped to a specific tool or action. Agents are built for convenience and adaptability, but that flexibility must be bounded by the same principles of least privilege that govern secure backend services.
While prompt injection is a well-known risk, the majority of real-world vulnerabilities arise not from the text the model produces but from the actions the agent performs as a result of that text. Tool execution is where theoretical vulnerabilities become operational ones.
Any tool that touches external systems like databases, APIs, file systems, email, payments, automation workflows should be treated as a high-risk capability. This means applying allowlists, strict validation, argument schemas and domain-level restrictions.
A common and extremely dangerous pattern is allowing agents to call URLs supplied directly by user input. Even if this seems harmless, it can lead to internal network access, SSRF attempts, metadata endpoint exposure or data exfiltration. Always strictly control which domains and addresses the agent may contact. Similarly, tools that modify state should require additional controls. There is a meaningful difference between retrieving data and changing it and agents should always be held to the same standards of privilege separation as human engineers.
Well-written system prompts help guide agent behavior, but they cannot serve as the foundation for security. Prompts can guide the model, but they cannot enforce rules. They can be overridden, bypassed or influenced by cleverly crafted inputs or malicious content inside documents or tool outputs.
Prompts should clearly articulate priorities, such as following safety and security rules when they conflict with user instructions. They should clarify that the agent must treat all user inputs and tool outputs as untrusted. However, prompts should avoid embedding sensitive internal details, because prompts themselves can be leaked through various jailbreaking techniques.
Prompts are best thought of as the first layer of protection, not the authoritative source. Real security must come from runtime checks, policy enforcement and tool constraints.
Even with carefully written prompts and well-scoped tools, it’s important to validate what goes into the model and what comes out. Framework-level safeguards work up to a point, but runtime checks add protection where it matters most.
Input checks help ensure the agent receives clean, reasonable instructions before the model even begins generating. These checks can detect common injection attempts, suspicious instructions that contradict the agent’s role, malformed data or unusually large payloads that might be intended to manipulate the model. They also provide a place to enforce basic operational constraints, such as maximum input size or restricted keywords.
Output checks examine the model’s final response to ensure it aligns with the agent’s safety and operational boundaries. This is especially important when the agent’s output is used as arguments to tools or other system components. Output checks can help catch unintended behavior such as generating URLs to unapproved domains, producing sensitive or confidential data or constructing instructions that could misuse downstream tools.
The combination of input and output validation creates a predictable envelope around the agent’s behavior. It ensures that even if unusual or adversarial inputs appear, the agent’s actions remain within a safe and pre-defined boundary.
Visibility is essential for debugging, governance and auditability. Logging should cover tool invocations, model decisions, blocks, redactions and relevant metadata. However, caution is necessary: logs must never expose raw secrets, personal data or model-internal content that may contain sensitive details.
Redaction with deterministic hashing is a useful strategy. It allows correlation of repeated exposures without logging raw content. Logs should also cap the size of stored text and avoid capturing unusually large or suspicious payloads.
Good logs help you understand how your agent behaves in the real world and they form the foundation of a review process that catches issues before they cause harm.
No agent should reach production without being tested against adversarial input. This includes direct prompt injection, indirect injection hidden in documents, malicious tool responses, attempts to bypass allowlists and requests designed to trigger data leakage or unauthorized behavior.
Even simple testing scripts can reveal surprising flaws. Real-world agents fail in ways developers often do not anticipate: following hidden instructions in PDFs, concatenating payloads in unexpected ways or acting on malformed tool responses.
Ultimately, testing is not just about making sure the happy path works. It is about discovering whether the system behaves safely when confronted with unexpected, deceptive or adversarial scenarios.
Agent security is not a single technique or a single safeguard. It is a layered approach to managing an evolving and inherently flexible system. Securing AI agents requires understanding what the agent is expected to do, constraining how it can interact with external systems, verifying its behavior at runtime and continually improving its performance through testing and observation.
Agents are powerful and increasingly integrated into critical workflows. With the right boundaries, constraints and safeguards, they can be both transformative and safe.
August 10, 2025
August 10, 2025
August 10, 2025
Copyright © DEEPLOCK



