The Core of Agentic Security
Secure agentic AI by enforcing rules through external, testable code rather than relying on the model to follow instructions. Use hard guardrails like spending limits and mandatory human approvals to prevent autonomous systems from making costly or high-stakes errors.
When building an agent, you must understand that the model is the decision-maker inside a loop, but the loop itself is ordinary code. Relying on a model to "behave" is a recipe for failure. Instead, you must implement security in the software layer that wraps the model.
To build a resilient system, focus on these three pillars of enforcement:
- External Guardrails: Implement rules like spending limits, blocked domains, and output filters in your code. These are boundaries the model cannot bypass because they are enforced by your application logic, not the LLM's reasoning.
- Mandatory Human-in-the-loop: For any action that is irreversible or high-stakes—such as sending a payment to a vendor or deleting a database record—the system must pause and require a human to say "yes" or "no."
- Testable Code: Security must be enforced by ordinary, testable software. If you can't write a unit test to prove a rule is being followed, your security is likely too fragile.
Managing Autonomy and Risk
Not all agent actions are created equal. A small business owner might want an agent to read an email (low risk), but they certainly do not want it to issue a refund without permission (high risk). You must categorize your tools by their risk class to manage this spectrum of autonomy.
| Risk Class | Action Type | Example |
|---|---|---|
| Read | Viewing information | Checking an invoice status |
| Fetch | Retrieving external data | Searching for a GST number |
| Write | Modifying internal data | Updating a customer's address |
| External | Interacting with outside APIs | Sending a WhatsApp message |
| Critical | High-stakes financial/data changes | Processing a bank transfer |
Once you have categorized your tools, you can gate access based on the agent's assigned autonomy level. An agent set to "Observe" mode should only have access to "Read" tools. An agent set to "Act" might have "Write" access but still require human approval for "Critical" tools. Finally, always implement a kill switch—a way to immediately stop all agent execution if the system begins behaving unpredictably.
Auditability and Traceability
If an agent makes a mistake, you need to know exactly why it happened. You cannot rely on a standard chat history to reconstruct events, as these can be misleading or incomplete. You need a professional-grade audit trail.
To ensure total transparency, implement these observability patterns:
- Hash-chained Audit Logs: Every action taken by the agent should be recorded in a hash-chained log. This ensures that once a record is written, it cannot be quietly edited or deleted after the fact, providing a permanent history of the agent's behavior.
- Durable Audit Trails: Ensure every single step is recorded in a durable way so you can perform a post-run review. This is vital for debugging and for regulatory compliance.
- Structured Reasoning Logs: Don't just log the final result. Use structured logs to track the agent's internal reasoning, the specific tool calls it made, and the observations it received from those tools.
This level of detail helps you move from "black box" AI to a predictable system where you can "time travel" or fork a run from an earlier step to test a different path.
Preventing Loop and Cost Risks
One of the biggest risks for business owners using agentic AI is the "runaway loop." This happens when an agent gets stuck in a cycle of repeating the same failed action, consuming thousands of tokens and racking up massive API bills in minutes.
To prevent this, you must implement strict resource constraints:
- Token and Step Budgets: Set a hard limit on how many tokens or how many steps an agent can take during a single run. Once the budget is hit, the agent must stop.
- Deterministic Verifiers: Do not use a second AI model to check if the first model did its job correctly—this is expensive and can be just as unreliable. Instead, use "cheap" deterministic code. For example, if an agent is calculating a total, use a simple Python script to verify that the sum of the line items actually equals the total.
- Timeouts and Retries: If a tool fails to respond, use timeouts to prevent the agent from waiting forever. Implement smart retry logic to handle temporary network glitches without crashing the entire workflow.
By combining these patterns, you ensure that your agent remains a productive tool rather than a financial liability.
Data Residency and Privacy
Data privacy is a non-negotiable requirement for any production-grade agent. When an agent processes information, it is often moving data between different systems and potentially different countries. You must implement strict governance to protect your users.
Key considerations include:
- Data-Residency Guards: Use software controls to ensure that sensitive data—such as Indian personal data or children's information—is kept within specific jurisdictions and is not sent to models hosted in unauthorized regions.
- AI Identity Disclosure: When agents interact with external parties (like customers via WhatsApp or email), they should clearly disclose that they are an AI. Transparency builds trust and prevents deceptive practices.
- Access Control: Just as you would with a human employee, limit the agent's access to only the specific categories of data required to complete its assigned task.
Safe Architectural Patterns
The way you structure your agentic system determines its inherent safety. Rather than letting an agent "free-roam" through your entire codebase, use proven patterns to restrict its scope.
Plan-and-Execute Pattern
In this pattern, the model first writes out a full plan of the steps it intends to take. This plan is then presented to a human for review. Only after the human approves the plan does the "executor" begin performing the actions. This is ideal for complex tasks like "Migrate these 300 product listings," where you want to verify the approach before any changes are made.
Router Pattern
A "Router" acts as a traffic controller. It uses a small, fast model to classify an incoming request and sends it down a very narrow, specialized path. For example, a billing question is sent to a specific billing workflow, while a technical support question goes to a different one. This reduces the "surface area" for errors because each path only has access to the tools and prompts it absolutely needs.
Hybrid Workflow Shells
The most reliable systems are often hybrids. Use a predictable, code-based workflow as the "outer shell" to handle the high-level logic, and place flexible agentic steps inside that shell only where reasoning is truly required. This gives you the flexibility of AI with the reliability of traditional software.

Questions people ask
How do you stop an AI agent from spending too much money?
Set strict token and step budgets on every run to ensure the agent stops once a predefined limit is reached.
Can I trust an agent to follow my instructions for safety?
No. You should not rely on the model to respect safety rules; instead, enforce guardrails through code outside the model.
What is the best way to verify an agent's work?
Use cheap, deterministic code-based checks whenever possible, such as verifying if a sum of line items equals a total.
How can I ensure an agent's actions are recorded honestly?
Use a hash-chained audit log of every action to create a permanent, unchangeable record of what the agent did.
How do I handle risky actions in an autonomous system?
Implement human-in-the-loop approval points where a person must manually approve a proposed action before it executes.
