# Chapter 61 — Secure AI Agents & Tool Execution

![Post cover](https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/lvqky8quse86x037viqj.png align="center")

  

## 61.1 Introduction

Traditional AI inference usually follows:

```text
User
 ↓
Prompt
 ↓
Model
 ↓
Response
```

An AI agent introduces additional capabilities:

```text
User
 ↓
Agent
 ├── Reasoning
 ├── Memory
 ├── Retrieval
 ├── Tools
 ├── External Services
 └── Multi-Step Execution
```

This creates a significantly larger security boundary.

An agent may be able to:

*   search information;
    
*   read files;
    
*   create files;
    
*   modify records;
    
*   call APIs;
    
*   send notifications;
    
*   generate media;
    
*   execute workflows;
    
*   interact with other services.
    

Therefore the security principle becomes:

> **An agent must never be granted authority merely because the model requested it.**

* * *

# 61.2 Agent Security Model

A secure agent architecture separates:

```text
Model Intelligence
       ≠
Application Authority
```

The model decides what it wants to accomplish.

The application decides what it is actually allowed to do.

```text
Agent
 ↓
Proposed Action
 ↓
Policy Engine
 ↓
Authorization
 ↓
Validation
 ↓
Tool
```

This separation is foundational.

* * *

# 61.3 Agent Identity

Every production agent should have a distinct identity.

Example:

```ts
interface AgentIdentity {
  agentId: string;
  ownerId: string;
  tenantId: string;
  environment: "development" | "staging" | "production";
  role: string;
}
```

An agent should not simply inherit the full permissions of the user who created it.

* * *

# 61.4 User Identity vs Agent Identity

Consider:

```text
User
 ↓
Creates Agent
 ↓
Agent
```

The agent should retain information about both:

```text
Actor:
    Agent

Created By:
    User

Tenant:
    Organization
```

This makes auditing possible.

* * *

# 61.5 Delegated Authority

An agent may receive a limited subset of a user's authority.

Example:

```text
User Permissions
        │
        ▼
Delegation Policy
        │
        ▼
Agent Permissions
```

If a user can perform 20 operations, the agent may only receive 5.

This follows least privilege.

* * *

# 61.6 Agent Capability Tokens

A useful design is to provide scoped capabilities.

Conceptually:

```text
Agent
 ↓
Capability
 ├── tool:search
 ├── resource:project-123
 └── action:read
```

A capability should specify:

*   what action is allowed;
    
*   which resource;
    
*   which tenant;
    
*   expiration;
    
*   context;
    
*   maximum scope.
    

* * *

# 61.7 Tool Registry

All agent tools should be registered centrally.

Example:

```ts
interface AgentTool {
  id: string;
  name: string;
  description: string;
  riskLevel: "low" | "medium" | "high";
  requiresApproval: boolean;
}
```

Example registry:

```text
search_documents
read_file
create_file
generate_image
send_email
update_database
delete_resource
```

High-impact operations require stronger controls.

* * *

# 61.8 Tool Risk Classification

Tools can be classified:

### Low risk

*   read public information;
    
*   calculate values;
    
*   format text.
    

### Medium risk

*   read private documents;
    
*   create files;
    
*   modify project configuration.
    

### High risk

*   delete data;
    
*   send external messages;
    
*   modify financial records;
    
*   change account permissions;
    
*   deploy production systems.
    

Risk classification should determine authorization requirements.

* * *

# 61.9 Read vs Write Tools

A particularly useful distinction is:

```text
READ
  ↓
Usually lower impact

WRITE
  ↓
Higher impact

DELETE / IRREVERSIBLE
  ↓
Highest impact
```

The system should not treat all tools equally.

* * *

# 61.10 Agent Planning

Agents often create multi-step plans.

Example:

```text
Goal
 ↓
Step 1: Search
 ↓
Step 2: Analyze
 ↓
Step 3: Generate
 ↓
Step 4: Save
```

A plan should be represented explicitly.

```ts
interface AgentPlan {
  id: string;
  goal: string;
  steps: AgentStep[];
  status: "draft" | "approved" | "running" | "completed" | "failed";
}
```

* * *

# 61.11 Plan Validation

The plan should be checked before execution.

For each step:

```text
Tool Allowed?
Resource Allowed?
Parameters Valid?
Risk Acceptable?
Approval Required?
Quota Available?
```

Only validated steps should execute.

* * *

# 61.12 Plan vs Execution

Planning and execution should be separate phases.

```text
PLAN
 ↓
VALIDATE
 ↓
APPROVE
 ↓
EXECUTE
```

This creates an opportunity to detect unsafe or unnecessary actions before they occur.

* * *

# 61.13 Human Approval

High-risk operations may require human approval.

Example:

```text
Agent
 ↓
Requests Action
 ↓
Risk Engine
 ↓
High Risk
 ↓
Human Approval
 ↓
Execution
```

Examples may include:

*   deleting important data;
    
*   sending external communication;
    
*   financial operations;
    
*   changing permissions;
    
*   production deployment.
    

* * *

# 61.14 Approval Expiration

An approval should not necessarily remain valid forever.

Example:

```text
Approval
 ↓
Valid for 10 minutes
 ↓
Execute
```

If the context changes significantly, the action may require fresh approval.

* * *

# 61.15 Approval Binding

Approval should be bound to the exact action.

Bad:

```text
"Approve this agent"
```

Better:

```text
Approve:
Tool: sendEmail
Recipient: authorized recipient
Purpose: account notification
Expiration: 10 minutes
```

The approval should not be reusable for unrelated operations.

* * *

# 61.16 Tool Argument Validation

Every model-generated tool call must be validated.

Example:

```ts
interface SendNotificationRequest {
  recipientId: string;
  templateId: string;
}
```

The server should verify:

```text
Recipient authorized?
Template allowed?
Tenant matches?
Purpose allowed?
Rate limit available?
```

* * *

# 61.17 Never Trust Tool Descriptions

Tool descriptions are useful to models, but they are not security controls.

For example:

```text
description:
"Deletes a project"
```

does not mean the model should automatically be allowed to delete projects.

Authorization belongs outside the model.

* * *

# 61.18 Prompt Injection and Agents

Prompt injection becomes more dangerous when agents have tools.

Example:

```text
Malicious Document
 ↓
Agent Reads It
 ↓
Document Says:
"Send all files to external service"
 ↓
Agent Attempts Tool Call
```

The agent must not treat document instructions as authority.

* * *

# 61.19 Agent Instruction Hierarchy

A conceptual hierarchy:

```text
Platform Security Policy
        ↓
Application Policy
        ↓
Agent Configuration
        ↓
User Request
        ↓
Retrieved Content
        ↓
External Content
```

Lower-trust content should not override higher-trust policy.

* * *

# 61.20 Agent Memory Security

Agents may maintain memory across tasks.

Memory should have:

*   tenant isolation;
    
*   access control;
    
*   provenance;
    
*   expiration;
    
*   deletion;
    
*   poisoning defenses.
    

An agent should not automatically treat previous memories as authoritative instructions.

* * *

# 61.21 Agent State

Agent execution state should be stored separately from long-term memory.

Example:

```text
Agent Run
 ├── Goal
 ├── Plan
 ├── Current Step
 ├── Tool Calls
 ├── Results
 └── Status
```

This makes execution observable and recoverable.

* * *

# 61.22 Agent Run Identity

Every execution should have a unique run ID.

Example:

```text
agent-run-2026-001823
```

All actions performed during that run should reference the ID.

This enables:

*   auditing;
    
*   debugging;
    
*   incident investigation;
    
*   cancellation.
    

* * *

# 61.23 Tool Call Audit Trail

Record events such as:

```text
agent.run.started
agent.plan.created
agent.tool.requested
agent.tool.approved
agent.tool.denied
agent.tool.executed
agent.tool.failed
agent.run.completed
agent.run.cancelled
```

Sensitive payloads should be minimized in logs.

* * *

# 61.24 Agent Execution Loop

A simplified agent loop is:

```text
Observe
  ↓
Plan
  ↓
Validate
  ↓
Request Tool
  ↓
Authorize
  ↓
Execute
  ↓
Observe Result
  ↓
Repeat
```

The loop should contain explicit limits.

* * *

# 61.25 Maximum Steps

An agent should have a maximum number of steps.

Example:

```ts
interface AgentLimits {
  maxSteps: number;
  maxRuntimeMs: number;
  maxToolCalls: number;
  maxCost: number;
}
```

This prevents accidental infinite loops and runaway execution.

* * *

# 61.26 Maximum Runtime

Every agent run should have a deadline.

```text
Start
 ↓
Timer
 ↓
Deadline
 ↓
Cancel
```

Long-running workflows can be resumed through durable job infrastructure rather than keeping an unrestricted process alive.

* * *

# 61.27 Maximum Cost

Agent loops can consume expensive model and tool resources.

A budget may include:

```text
Model Tokens
+
Tool Calls
+
External API Cost
+
Compute
```

When the budget is exhausted:

```text
Stop
```

or require additional authorization.

* * *

# 61.28 Tool Concurrency

Agents may attempt multiple tools simultaneously.

Concurrency should be limited.

```text
Agent
 ├── Tool A
 ├── Tool B
 └── Tool C
```

The system should enforce a maximum number of concurrent operations.

* * *

# 61.29 Recursive Agents

An agent may be able to invoke another agent.

This creates additional risk.

Use explicit rules:

```text
Agent A
 ↓
Can Invoke Agent B?
 ↓
Policy Check
 ↓
Allowed / Denied
```

Do not allow unrestricted recursive agent creation.

* * *

# 61.30 Agent-to-Agent Authentication

If one agent calls another service:

```text
Agent A
 ↓
Agent Service
```

the receiving service should authenticate the caller.

It should know:

```text
Who?
Which Tenant?
Which Agent?
Which Run?
Which Capability?
```

* * *

# 61.31 Sandbox Architecture

High-risk tool execution should occur in isolated environments.

For example:

```text
Agent
 ↓
Execution Sandbox
 ├── Temporary Filesystem
 ├── Restricted Network
 ├── Limited CPU
 ├── Limited Memory
 └── No Production Credentials
```

This is particularly important for code execution or untrusted media processing.

* * *

# 61.32 Filesystem Isolation

An agent should not automatically have access to the host filesystem.

Instead:

```text
Agent Workspace
      ↓
Temporary Sandbox
```

The workspace should be destroyed or cleaned according to policy after execution.

* * *

# 61.33 Network Isolation

A sandbox should use deny-by-default network access where practical.

Possible policy:

```text
ALLOW
 ├── Approved API
 └── Approved Storage

DENY
 ├── Internal Admin Services
 ├── Metadata Services
 └── Unapproved Internet Destinations
```

* * *

# 61.34 Credential Isolation

A sandbox should not receive the host's credentials.

Instead use narrowly scoped service identities.

```text
Sandbox
 ↓
Scoped Credential
 ↓
One Specific Service
```

Credentials should be:

*   short-lived where possible;
    
*   scoped;
    
*   revocable;
    
*   audited.
    

* * *

# 61.35 Code Execution

If an agent is allowed to generate and execute code:

```text
Generated Code
 ↓
Validation
 ↓
Sandbox
 ↓
Resource Limits
 ↓
Execution
 ↓
Output Validation
```

Never execute generated code directly inside the main application server.

* * *

# 61.36 Browser Automation

Browser-capable agents introduce additional risks.

A browser agent may interact with:

*   websites;
    
*   forms;
    
*   downloads;
    
*   accounts;
    
*   external messages.
    

Security controls should include:

*   domain allowlists;
    
*   session isolation;
    
*   download restrictions;
    
*   credential isolation;
    
*   action confirmation;
    
*   navigation policies.
    

* * *

# 61.37 External Communication

Agents that can send email, SMS, or notifications should have strict controls.

For example:

```text
Agent
 ↓
Draft Message
 ↓
Policy Check
 ↓
Recipient Check
 ↓
Approval if required
 ↓
Send
```

The model should not independently decide that external communication is authorized.

* * *

# 61.38 Destructive Actions

Destructive operations should receive the strongest controls.

Examples:

```text
Delete
Destroy
Revoke
Overwrite
Publish
Transfer
```

Recommended pattern:

```text
Agent Request
 ↓
Risk Classification
 ↓
Explicit Authorization
 ↓
Optional Human Approval
 ↓
Execute
 ↓
Audit
```

* * *

# 61.39 Transaction Boundaries

Multi-step agent workflows should use transaction-like boundaries where possible.

For example:

```text
Step 1: Create Draft
Step 2: Validate
Step 3: Approve
Step 4: Publish
```

If Step 3 fails, Step 4 should not execute.

* * *

# 61.40 Idempotency

Agent retries can accidentally duplicate actions.

For example:

```text
sendNotification()
```

might execute twice if a worker retries.

Use idempotency keys:

```ts
interface ToolExecution {
  idempotencyKey: string;
  toolId: string;
  runId: string;
}
```

This helps prevent duplicate side effects.

* * *

# 61.41 Retry Policy

Retries should be limited.

```text
Attempt 1
 ↓
Failure
 ↓
Attempt 2
 ↓
Failure
 ↓
Attempt 3
 ↓
Stop
```

Do not retry every failure indefinitely.

* * *

# 61.42 Failure Classification

Different failures require different handling.

```text
Validation Error → Do Not Retry

Authorization Error → Do Not Retry

Temporary Network Error → Controlled Retry

Provider Timeout → Limited Retry

Policy Denial → Stop
```

This prevents unsafe retry loops.

* * *

# 61.43 Agent Cancellation

Users and administrators should be able to cancel active runs.

```text
Running
   ↓
Cancel Requested
   ↓
Stop New Actions
   ↓
Cancel Active Work
   ↓
Cleanup
   ↓
Cancelled
```

Cancellation should be observable.

* * *

# 61.44 Kill Switch

High-risk agent systems should have an operational kill switch.

It may disable:

*   a specific agent;
    
*   a specific tool;
    
*   a model;
    
*   a tenant;
    
*   an entire automation feature.
    

Example:

```text
Security Incident
      ↓
Disable Tool
      ↓
Agents Cannot Execute Tool
```

The kill switch should be protected with strong administrative controls.

* * *

# 61.45 Agent Quarantine

If suspicious behavior is detected:

```text
Normal Agent
     ↓
Suspicious Behavior
     ↓
Quarantine
```

Quarantine can:

*   stop tool execution;
    
*   prevent external communication;
    
*   preserve logs;
    
*   restrict network access;
    
*   require human review.
    

* * *

# 61.46 Agent Behavior Monitoring

Monitor:

*   tool-call frequency;
    
*   tool diversity;
    
*   failed authorization attempts;
    
*   unusual destinations;
    
*   excessive loops;
    
*   token consumption;
    
*   execution duration;
    
*   failed actions;
    
*   unusual data access.
    

Behavioral baselines can help identify compromised or malfunctioning agents.

* * *

# 61.47 Agent Anomaly Detection

Example:

```text
Normal:
5 tool calls/run

Observed:
850 tool calls/run
```

This should trigger an anomaly signal.

Similarly:

```text
Normal destinations:
approved internal services

Observed:
unexpected external destination
```

should be investigated.

* * *

# 61.48 Agent Security Dashboard

A dashboard may display:

```text
Active Runs
Tool Calls
Denied Actions
High-Risk Actions
Average Runtime
Average Cost
Failed Runs
Quarantined Agents
Security Alerts
```

This provides operational visibility.

* * *

# 61.49 Agent Threat Model

| Threat | Impact | Defense |
| --- | --- | --- |
| Prompt injection | High | Context separation + policy |
| Unauthorized tool call | High | Tool authorization |
| Excessive autonomy | High | Limits + approvals |
| Credential exposure | Critical | Credential isolation |
| Infinite loop | Medium | Step/time limits |
| Duplicate side effects | High | Idempotency |
| Cross-tenant access | Critical | Tenant isolation |
| Malicious tool input | High | Schema validation |
| Unsafe code execution | Critical | Sandbox |
| External communication abuse | High | Recipient/policy controls |
| Agent takeover | High | Identity + monitoring |
| Recursive agent abuse | Medium/High | Delegation limits |

* * *

# 61.50 Secure Agent API

A secure agent API may expose:

```ts
interface AgentRunRequest {
  agentId: string;
  goal: string;
  allowedTools?: string[];
  maxSteps?: number;
  requireApprovalForHighRisk?: boolean;
}
```

The server should not blindly trust these fields.

For example:

```text
Client says:
maxSteps = 10,000

Server Policy:
maxSteps = 100

Effective:
100
```

Server policy must take precedence.

* * *

# 61.51 Effective Policy

The final policy can be calculated from several layers:

```text
Platform Policy
      +
Tenant Policy
      +
User Permissions
      +
Agent Permissions
      +
Tool Policy
      +
Runtime Limits
      ↓
Effective Policy
```

The most restrictive applicable policy should normally win.

* * *

# 61.52 Agent Configuration

Agent configuration should be versioned.

Example:

```text
Agent:
research-agent

Config:
v12

Tools:
search, summarize

Limits:
50 steps
10 minutes
$1 budget
```

Changing tool permissions should create a new configuration version.

* * *

# 61.53 Agent Change Management

Security-sensitive changes include:

*   adding a tool;
    
*   increasing limits;
    
*   changing system instructions;
    
*   changing model;
    
*   changing memory access;
    
*   changing external destinations.
    

These changes should receive appropriate review.

* * *

# 61.54 Production Agent Deployment

A secure deployment flow:

```text
Agent Definition
 ↓
Security Review
 ↓
Tool Review
 ↓
Risk Classification
 ↓
Testing
 ↓
Approval
 ↓
Staging
 ↓
Canary
 ↓
Production
```

This mirrors the secure model lifecycle.

* * *

# 61.55 Agent Evaluation

Evaluate agents on:

### Functional performance

Does the agent accomplish legitimate tasks?

### Security

Does it respect permissions?

### Reliability

Does it terminate correctly?

### Safety

Does it avoid harmful actions?

### Robustness

Does it resist adversarial inputs?

### Cost

Does it remain within expected resource budgets?

* * *

# 61.56 Agent Security Test Cases

Examples:

```text
Attempt unauthorized tool
Attempt cross-tenant read
Inject malicious document
Exceed step limit
Exceed cost limit
Trigger repeated failures
Attempt prohibited destination
Modify protected resource
Submit malformed tool arguments
Cancel active run
```

Each test should have an expected result.

* * *

# 61.57 Agent Audit Example

A run might produce:

```text
Run: agent-run-1823

10:00 Agent started
10:01 Plan created
10:01 Search requested
10:01 Search approved
10:02 Search completed
10:03 File creation requested
10:03 File creation approved
10:04 File created
10:05 Run completed
```

This provides a clear execution trail.

* * *

# 61.58 Secure Agent Architecture

A complete reference architecture:

```text
                         USER
                           │
                           ▼
                    API GATEWAY
                           │
                           ▼
                    AUTHENTICATION
                           │
                           ▼
                    AUTHORIZATION
                           │
                           ▼
                     AGENT POLICY
                           │
                           ▼
                    AGENT RUNTIME
                     /     |      \
                    /      |       \
                   ▼       ▼        ▼
               MEMORY   RETRIEVAL  MODEL
                   \       |       /
                    \      |      /
                     ▼     ▼     ▼
                       PLAN
                         │
                         ▼
                    TOOL REQUEST
                         │
                         ▼
                  RISK / POLICY ENGINE
                         │
              ┌──────────┴──────────┐
              ▼                     ▼
           DENY                 APPROVE
                                    │
                                    ▼
                             HUMAN APPROVAL
                              if required
                                    │
                                    ▼
                              TOOL EXECUTION
                                    │
                             ┌──────┴──────┐
                             ▼             ▼
                         SANDBOX       EXTERNAL API
                             │             │
                             └──────┬──────┘
                                    ▼
                                RESULT
                                    │
                                    ▼
                              OBSERVABILITY
                                    │
                                    ▼
                                  AUDIT
```

* * *

# 61.59 Production Checklist

### Identity

*   \[ \] Dedicated agent identity
    
*   \[ \] Tenant association
    
*   \[ \] Delegated authority
    
*   \[ \] Scoped capabilities
    

### Tools

*   \[ \] Central tool registry
    
*   \[ \] Risk classification
    
*   \[ \] Allowlist
    
*   \[ \] Argument validation
    
*   \[ \] Separate read/write/delete permissions
    

### Execution

*   \[ \] Maximum steps
    
*   \[ \] Runtime timeout
    
*   \[ \] Cost limit
    
*   \[ \] Concurrency limit
    
*   \[ \] Cancellation
    
*   \[ \] Idempotency
    

### High-Risk Actions

*   \[ \] Risk classification
    
*   \[ \] Explicit authorization
    
*   \[ \] Human approval where required
    
*   \[ \] Approval expiration
    
*   \[ \] Exact-action binding
    

### Isolation

*   \[ \] Sandbox
    
*   \[ \] Network restrictions
    
*   \[ \] Credential isolation
    
*   \[ \] Temporary filesystem
    
*   \[ \] Resource limits
    

### Monitoring

*   \[ \] Run IDs
    
*   \[ \] Tool-call audit
    
*   \[ \] Anomaly detection
    
*   \[ \] Security alerts
    
*   \[ \] Kill switch
    
*   \[ \] Quarantine capability
    

* * *

# 61.60 Final Principle

Agent security should never depend on the assumption that the model will always behave correctly.

The secure architecture assumes:

```text
Model may misunderstand
Model may hallucinate
Input may be malicious
Retrieved data may be poisoned
Tool arguments may be unsafe
External services may fail
```

Therefore:

```text
MODEL
  ↓
REQUESTS
  ↓
POLICY
  ↓
AUTHORIZATION
  ↓
VALIDATION
  ↓
ISOLATION
  ↓
EXECUTION
  ↓
AUDIT
```

The model provides reasoning.

The **security architecture provides authority boundaries**.

This separation is what allows AI agents to become useful automation systems without turning every model error into a system-level security incident.
