Introduction: AI agents introduce a new application security challenge
Traditional software executes operations according to explicitly programmed rules. AI-powered applications introduce an additional layer: a model interprets requests, generates responses, and may select tools to accomplish a task.
Consider an AI assistant built for a customer support team. It can:
-
Search customer orders.
-
Retrieve internal documentation.
-
Create support tickets.
-
Draft emails.
-
Request refunds through business APIs.
These capabilities can significantly reduce repetitive work. However, they also create new security challenges.
What happens if a malicious document influences the model to request an unauthorized operation? What happens if an agent can access more data than its task requires? What happens if an AI-generated response is treated as a trusted instruction by the backend?
These are not simply model-quality problems. They are application security problems.
The central principle is simple: an AI model may recommend an action, but the application must decide whether that action is allowed.
This guide explains how to design, implement, test, and operate safer AI agents in production environments.
1. What is AI agent security?
AI agent security is the practice of protecting AI systems that can interpret tasks, interact with tools, retrieve information, and initiate application operations.
A traditional chatbot might answer a question using a language model. An agent can go further by selecting a tool, supplying arguments, receiving a result, and deciding what to do next.
For example:
Traditional chatbot
User asks a question → Model generates an answer → User reads the answer.
AI agent
User requests an action → Model selects a tool → Application validates the request → Tool executes → Result is evaluated → Agent returns a response.
The second workflow creates more opportunities for unauthorized access, unsafe execution, and unintended side effects.
AI agent security therefore combines established application security controls with safeguards specific to model-driven workflows.
The main security objectives
A secure agent should preserve:
-
Confidentiality: Users must not receive information they are not authorized to access.
-
Integrity: The agent must not modify data outside its permitted scope.
-
Availability: Uncontrolled loops and excessive requests must not exhaust system resources.
-
Accountability: Sensitive actions must be traceable.
-
Least privilege: The agent should receive only the capabilities required for its task.
-
Human oversight: High-impact operations should have appropriate approval and recovery mechanisms.
These objectives should be reflected in the architecture, not merely documented in a prompt.
2. Understand the major AI agent security risks
2.1 Prompt injection
Prompt injection occurs when malicious or untrusted content attempts to influence a model's behavior.
Suppose an agent summarizes customer documents. One document contains an instruction telling the agent to ignore its original task and attempt to disclose confidential records.
The document is supposed to be data, not an authorized source of application policy. However, a language model may still be influenced by the instruction.
Prompt injection can originate from:
-
Direct user input.
-
Uploaded documents.
-
Retrieved knowledge-base articles.
-
Webpages.
-
Emails and support tickets.
-
Tool responses.
-
Third-party content.
Mitigation strategy
Treat all external and retrieved content as untrusted. Separate trusted application instructions from retrieved data, restrict the available tools, and enforce authorization independently of the model.
You can also test adversarial inputs and use model or framework features designed to help detect suspicious content. These measures can reduce risk, but none guarantees that prompt injection will be eliminated.
The most important defense is limiting what the agent can actually do if it is manipulated.
2.2 Excessive agency and permissions
An agent has excessive agency when it can perform actions beyond what its task requires.
Imagine an order-status assistant that needs read access to order records but also receives permission to delete customers and modify payments.
Those additional capabilities increase the impact of a compromised or mistaken workflow.
Apply least privilege:
-
Give read-only agents read-only tools.
-
Scope access to authorized resources.
-
Separate approval and execution capabilities.
-
Restrict high-impact operations.
-
Avoid unrestricted administrative credentials.
-
Reassess permissions when the task or user changes.
A prompt saying "do not delete customer records" is not a substitute for a backend permission check.
2.3 Sensitive information disclosure
An agent may process customer information, financial data, internal documents, API credentials, or business secrets.
Disclosure can occur through generated responses, retrieval results, tool outputs, logs, or external model requests.
Mitigations include data minimization, access-controlled retrieval, secret management, appropriate redaction, and clear data-retention policies.
Do not include credentials in prompts or expose confidential application configuration to the model.
2.4 Insecure tool execution
Tool calling allows a model to request actions through application functions.
The risk increases when tools accept unrestricted inputs or expose powerful operations.
Examples of dangerous designs include:
-
Executing arbitrary SQL generated by a model.
-
Running unrestricted shell commands.
-
Allowing arbitrary outbound HTTP requests.
-
Exposing a generic administrative API.
-
Accepting model-generated tenant IDs as authorization evidence.
Prefer narrow, typed operations that enforce validation and authorization before execution.
2.5 Unbounded execution and denial of service
An agent may repeatedly call tools, retry failing operations, or generate excessive tokens.
Without limits, a single request can consume substantial compute resources or API budget.
Apply timeouts, cancellation, retry limits, token budgets, rate limiting, and maximum tool-call counts.
Track resource consumption by workflow and user so abnormal behavior can be investigated.
2.6 Insecure retrieval and memory
Retrieval-augmented generation (RAG) introduces another risk: the agent may retrieve documents that are relevant to a query but unauthorized for the current user.
A vector search system should not treat semantic similarity as an access-control mechanism.
Enforce tenant and resource permissions before retrieved content is passed to the model. Ensure that document deletion and permission changes propagate to derived chunks and indexes.
3. Design a secure AI agent architecture
A safer architecture separates the model from protected business operations.
User or Client
|
v
ASP.NET Core API
|
v
Authentication and Authorization
|
v
AI Orchestration Layer
|
v
Tool Registry and Input Validation
|
v
Policy and Permission Checks
|
v
Business Services
|
v
Database / External APIs
This diagram represents logical responsibilities. Some applications may place policy checks in a separate service or use a dedicated authorization layer.
Component responsibilities
API layer
Authenticates the caller, validates the request, applies rate limits, and establishes trusted user context.
AI orchestration layer
Sends appropriate context to the model, receives proposed tool calls, and coordinates the workflow.
Tool registry
Exposes only approved capabilities and validates the structure of tool arguments.
Authorization and policy layer
Determines whether the requested operation is permitted for the authenticated actor and target resource.
Business services
Enforce domain rules, transactional consistency, and resource-level authorization.
Data layer
Provides controlled access to databases, search indexes, files, and other resources.
This separation helps prevent model behavior from bypassing established application controls.
4. Implement authentication and authorization in ASP.NET Core
ASP.NET Core provides established authentication and authorization mechanisms that can be used in AI-enabled applications.
For example, suppose your application has a read-only order-status tool.
You can define an authorization policy:
builder.Services.AddAuthorization(options =>
{
options.AddPolicy(
"CanReadOrders",
policy => policy
.RequireAuthenticatedUser()
.RequireClaim("permission", "orders.read"));
});
An API endpoint can then require that policy:
[Authorize(Policy = "CanReadOrders")]
[HttpGet("{orderId}")]
public async Task<IActionResult> GetOrderStatus(
string orderId,
CancellationToken cancellationToken)
{
// Validate the order identifier.
// Check access to this specific order.
// Retrieve the authorized order status.
return Ok();
}
This example illustrates policy-based authorization; it is not a complete endpoint implementation.
In a real application, the identity provider and token validation configuration must be correct. Claims must originate from a trusted authentication process.
Also, an endpoint policy alone does not solve every resource-access problem.
A user may be allowed to read orders generally but not orders belonging to another organization. The application must enforce that resource-level restriction as well.
Why authorization must not depend on the prompt
Consider this instruction:
"You are a support assistant. Never access orders belonging to another customer."
It is useful as behavioral guidance, but it is not a reliable security boundary.
Instead, derive the authenticated identity and permitted tenant context from trusted server-side state. When the agent requests an order, the application must independently check whether that identity can access the resource.
If authorization fails, the operation must be rejected regardless of the model's explanation or confidence.
5. Design safe tool contracts
Avoid exposing a single generic tool capable of performing arbitrary operations.
Instead, define narrowly scoped tools.
For example:
public sealed record GetOrderStatusRequest(
string OrderId);
The tool should validate the identifier, check the caller's permission, and retrieve only the information required for the task.
A separate refund operation should have a different contract, authorization policy, and validation process.
A safer tool execution sequence
-
Receive the model's proposed tool call.
-
Verify that the tool is on the approved allowlist.
-
Validate the arguments.
-
Resolve the authenticated actor and authorized resource scope.
-
Enforce application and business policies.
-
Obtain human approval if required.
-
Execute the operation.
-
Record the result and return only the necessary data.
Do not permit the model to choose its own authorization level or silently expand its tool permissions.
6. Add approval gates for high-impact actions
Not every action requires human approval.
Retrieving a public document may be low risk. Issuing a refund, deleting records, changing account permissions, or deploying production infrastructure can have substantial consequences.
Classify operations by impact and reversibility.
| Operation | Possible control |
|---|---|
| Search public documentation | Tool allowlist and rate limits |
| Read a customer's order | Authentication and resource authorization |
| Draft an email | Validate content and recipient |
| Send an external email | Approval or policy-based authorization |
| Issue a refund | Business rules, limits, and appropriate approval |
| Delete production data | Strong authorization, confirmation, and recovery controls |
The appropriate control depends on the business context.
When approval is required, bind it to the exact operation and its parameters. If the proposed action changes after approval, validate it again.
7. Secure RAG and vector database access
For a RAG application, access control must happen before protected document chunks enter the model's context.
A secure retrieval flow should:
-
Authenticate the user.
-
Resolve the user's permitted resources.
-
Apply tenant and document-level filters.
-
Retrieve only authorized chunks.
-
Construct the model context.
-
Generate the response.
-
Validate the response and its references.
Do not ask the model to decide whether a retrieved document should have been accessible.
For multi-tenant systems, test cross-tenant isolation, permission revocation, document deletion, and stale vector records.
Also consider whether shared indexes with enforced filters or separate indexes are appropriate for the application's threat model.
8. Implement execution limits and cost controls
A production agent should have a defined execution budget.
Useful limits include:
-
Maximum workflow duration.
-
Maximum model calls.
-
Maximum tool invocations.
-
Maximum retries.
-
Token or spending budgets.
-
Request-size limits.
-
Rate limits.
-
Cancellation and timeout behavior.
Use idempotency controls for operations that may be retried. For example, a repeated request should not unintentionally issue the same refund twice.
Design failure handling so that a timeout or unavailable policy service does not cause the application to bypass a required security check.
9. Monitor and audit agent activity
Observability should help teams answer:
-
Who initiated the workflow?
-
Which tools were invoked?
-
Which resources were accessed?
-
Which authorization checks passed or failed?
-
Were approvals required and obtained?
-
How long did execution take?
-
Did the workflow exceed its expected resource budget?
Use structured logs, correlation identifiers, and suitable access controls.
Avoid recording secrets, complete confidential prompts, or unnecessary customer data. Define retention periods and protect audit records against unauthorized modification.
Monitoring can identify suspicious patterns, but logging alone does not prevent unauthorized operations. Preventive controls must still exist in the execution path.
10. Test the agent before production
A comprehensive test strategy should include ordinary functional tests, authorization tests, and adversarial scenarios.
Test cases should cover:
-
Malicious instructions inside retrieved documents.
-
Attempts to access another tenant's data.
-
Unauthorized tool invocations.
-
Invalid tool arguments.
-
Attempts to exceed execution limits.
-
Duplicate operations caused by retries.
-
Model timeouts and provider failures.
-
Sensitive information in responses and logs.
-
Permission changes after a document has been indexed.
-
Approval decisions associated with changed tool parameters.
Test the actual application and tool execution path. A model that produces a safe-looking answer does not prove that the backend is secure.
11. Production deployment checklist
Before releasing an AI agent, verify the following:
Identity and access
-
Authentication is correctly configured.
-
Every sensitive operation enforces authorization.
-
Resource and tenant boundaries are validated.
-
Tool permissions follow least privilege.
Input and execution
-
Tool arguments are validated.
-
Arbitrary SQL and shell execution are prohibited or confined to rigorously designed environments.
-
Sensitive operations have appropriate approval controls.
-
Timeouts, retries, and resource budgets are enforced.
Data protection
-
Retrieval respects document permissions.
-
Secrets are stored securely.
-
Logs minimize sensitive data.
-
Data retention and deletion policies are defined.
Operations
-
Security events are observable.
-
Alerts exist for suspicious behavior.
-
Adversarial tests are automated.
-
Incident response and recovery procedures are documented.
12. Further reading
Use current official documentation when selecting implementation-specific controls:
-
OWASP GenAI Security Project: https://genai.owasp.org/llm-top-10/
-
ASP.NET Core authorization: https://learn.microsoft.com/en-us/aspnet/core/security/authorization/introduction
-
Microsoft Agent Framework: https://learn.microsoft.com/en-us/agent-framework/overview/
-
Microsoft Azure AI security documentation: https://learn.microsoft.com/en-us/azure/ai-foundry/responsible-ai/
Framework APIs and security capabilities evolve. Verify the documentation for the exact version you use.
Conclusion
AI agents can make software more useful by connecting language models to real application capabilities. That same connection creates a responsibility to enforce clear boundaries.
The strongest design does not assume that a model will always follow its instructions. It assumes that mistakes and manipulation are possible—and limits their consequences through authorization, validation, isolation, monitoring, and approval.
Let the model reason about the task. Let your application enforce the rules.
At Toolliyo, we focus on practical software engineering, AI application development, and production-ready architecture.
Explore more tutorials at https://toolliyo.com
Question for developers: Which control would you implement first when moving an AI agent into production: least-privilege tool access, resource-level authorization, or human approval for sensitive actions?
Disclaimer: This guide is educational and does not replace a security assessment. Validate all controls against your application's architecture, framework versions, and threat model.