63.1 Introduction
A production AI platform rarely consists of one application server.
A realistic architecture may contain:
- Web frontend
- Mobile application
- API gateway
- Authentication service
- User service
- AI orchestration service
- Model gateway
- Media processing workers
- Document processing service
- Storage service
- Billing service
- Notification service
- Search/RAG service
- Agent service
- Monitoring infrastructure
As the number of services increases, direct communication between every service becomes difficult to secure and operate.
A secure architecture therefore introduces controlled communication layers:
Client
│
▼
API Gateway
│
├── Authentication
├── Authorization
├── Rate Limiting
├── Request Validation
├── Threat Protection
└── Routing
│
▼
Internal Services
│
▼
Service-to-Service Security
The central principle is:
Every request should be authenticated, authorized, validated, observed, and constrained according to its risk.
63.2 API Gateway
The API gateway is the controlled entry point for external application traffic.
Typical responsibilities include:
TLS termination
authentication integration
request validation
routing
rate limiting
quota enforcement
request-size limits
logging
tracing
API versioning
security headers
abuse protection
The gateway should not become the only security layer.
For example:
Internet
↓
Gateway ✓
↓
Service
↓
Database
does not mean the service can blindly trust all requests arriving from the gateway.
The downstream service should still enforce its own authorization.
63.3 Gateway vs Application Authorization
A common architectural mistake is:
Gateway:
"User is authenticated."
Service:
"Therefore user can perform anything."
Authentication only establishes identity.
Authorization determines what that identity is allowed to do.
Therefore:
Gateway
↓
Authentication
↓
Service
↓
Authorization
↓
Operation
is safer.
63.4 Request Lifecycle
A secure request can follow this pipeline:
Client
↓
TLS
↓
Gateway
↓
Request size check
↓
Authentication
↓
Rate limit
↓
Schema validation
↓
Route policy
↓
Service
↓
Authorization
↓
Business logic
↓
Database / AI / external provider
↓
Response validation
↓
Audit + telemetry
Each layer should have a clearly defined responsibility.
63.5 API Authentication
Common authentication approaches include:
- session cookies
- OAuth/OIDC
- short-lived access tokens
- API keys for controlled machine clients
- service identities
- workload identities
For browser applications, authentication should be designed around the application's threat model rather than simply placing long-lived secrets into JavaScript.
A useful distinction is:
Human identity
Service identity
Workflow identity
Agent identity
Device identity
These identities should not be treated as interchangeable.
63.6 Service-to-Service Authentication
Internal traffic should also be authenticated.
Example:
AI Service
│
│ authenticated request
▼
Model Gateway
The Model Gateway should be able to determine:
Which service called me?
Which workload instance?
Which tenant?
Which operation?
What authorization scope?
This prevents an internal service from impersonating another service without authorization.
63.7 Zero-Trust Service Communication
A zero-trust architecture does not assume that an internal network is automatically trusted.
Instead:
Service A
↓
Authenticate
↓
Authorize
↓
Service B
This is stronger than:
Inside VPC = trusted
because internal environments can still experience:
- compromised workloads
- configuration errors
- stolen credentials
- vulnerable dependencies
- accidental exposure
- malicious insiders
63.8 Mutual TLS
Mutual TLS, or mTLS, can provide cryptographic service identity.
Normal TLS:
Client ───────► Server
server identity
mTLS:
Client ◄──────► Server
both authenticate
This can establish:
Service A identity
Service B identity
encrypted channel
certificate-based authentication
mTLS is particularly useful in environments containing many internal services.
63.9 Service Identity
Each production workload should have a distinct identity.
For example:
service:
ai-orchestrator
identity:
spiffe://platform/prod/ai-orchestrator
Another service:
service:
media-worker
identity:
spiffe://platform/prod/media-worker
The identities should be separate.
A compromised media worker should not automatically gain the permissions of the billing service.
63.10 Least-Privilege Service Permissions
Service permissions should be narrow.
Example:
Media Worker
────────────
READ:
object-storage/uploads
WRITE:
object-storage/processed
DENY:
billing/*
users/*
secrets/*
The worker does not need access to unrelated systems.
This is the service equivalent of least-privilege user authorization.
63.11 API Scopes
Machine identities can use scopes such as:
media:read
media:write
generation:create
generation:read
billing:read
notifications:send
A service should receive only the scopes required for its function.
Avoid broad scopes such as:
admin:*
unless there is a genuine, controlled requirement.
63.12 API Schema Validation
Every API should validate request structures.
Example:
{
"prompt": "Generate an image",
"width": 1024,
"height": 1024
}
The server should verify:
prompt is a string
prompt length is acceptable
width is allowed
height is allowed
unknown fields are handled safely
user has generation permission
quota is available
Validation should happen before expensive AI processing.
63.13 Request Size Limits
Attackers can exploit unrestricted request sizes.
Possible limits include:
JSON body: 1 MB
metadata: 100 KB
prompt: 10,000 characters
file upload: policy-defined maximum
header size: bounded
batch size: bounded
Limits should match legitimate application requirements.
63.14 Rate Limiting
Rate limiting protects APIs from excessive requests.
Possible dimensions:
IP
user
tenant
API key
service identity
endpoint
workflow
model
resource
For example:
Anonymous:
10 requests/minute
Authenticated:
100 requests/minute
Premium tenant:
custom quota
Limits should be risk-aware rather than identical for every endpoint.
63.15 AI-Specific Rate Limits
AI APIs often require additional controls because one request can be expensive.
For example:
POST /generate-image
might consume significantly more resources than:
GET /profile
Therefore, rate limits can consider:
requests
tokens
GPU seconds
estimated cost
image resolution
video duration
concurrent generations
63.16 Token-Based Quotas
A request quota such as:
100 requests/hour
may not adequately control AI usage.
Consider:
Request A = 500 tokens
Request B = 500,000 tokens
Both are one request but have radically different resource costs.
Therefore, AI platforms can use multiple dimensions:
requests
tokens
compute
storage
financial budget
63.17 API Versioning
APIs evolve.
Instead of unexpectedly changing behavior:
/api/generate
a platform can support controlled versions:
/api/v1/generate
/api/v2/generate
Version metadata should be observable.
This becomes particularly important for AI APIs because model behavior can change independently from application code.
63.18 Model Gateway
A dedicated model gateway can centralize AI provider access.
Architecture:
Application
↓
AI Orchestrator
↓
Model Gateway
↓
┌─────────────┬─────────────┬─────────────┐
│ Provider A │ Provider B │ Local Model │
└─────────────┴─────────────┴─────────────┘
Benefits include:
- centralized credentials
- model allowlists
- provider routing
- usage tracking
- token limits
- content policy enforcement
- fallback control
- logging
- cost management
63.19 Provider Credential Isolation
Provider credentials should remain server-side.
The browser should not receive:
PROVIDER_API_KEY
Instead:
Browser
↓
Application API
↓
Model Gateway
↓
Provider
This keeps provider credentials outside the client trust boundary.
63.20 Model Allowlisting
Applications should not necessarily permit users or agents to select arbitrary models.
Instead:
Allowed Models
───────────────
image-standard
image-high-quality
text-fast
text-reasoning
local-safe-model
The gateway maps logical names to actual provider configurations.
This makes model changes easier to control.
63.21 Service Mesh
In a larger deployment, a service mesh can provide standardized service-to-service networking features.
Conceptually:
Service A
│
Proxy A
│
│ encrypted/authenticated
▼
Proxy B
│
Service B
The mesh can assist with:
- service identity
- encryption
- traffic routing
- telemetry
- retries
- timeouts
- circuit breaking
- policy enforcement
However, a service mesh does not replace application-level authorization.
63.22 Service Mesh Policy
Example policy:
media-worker
↓
storage-service
Allowed:
GET /objects/upload/*
PUT /objects/processed/*
Denied:
GET /users/*
POST /billing/*
DELETE /accounts/*
The policy should follow least privilege.
63.23 Network Segmentation
Services can be separated into logical zones.
Example:
Internet Zone
│
▼
Edge/API Zone
│
▼
Application Zone
│
┌───┴────┐
▼ ▼
AI Zone Worker Zone
│ │
└───┬────┘
▼
Data Zone
Database services should generally not be directly reachable from the public internet.
63.24 Egress Control
Security architecture often focuses on incoming traffic.
AI systems also need to control outgoing traffic.
A compromised worker might otherwise attempt to communicate with arbitrary destinations.
Egress policy can restrict:
allowed domains
allowed ports
allowed protocols
provider endpoints
DNS destinations
network destinations
For example:
AI Worker
↓
Allowed:
model-provider.example
storage.example
Denied:
unknown destinations
The exact implementation depends on the deployment environment.
63.25 SSRF Defense
Server-side request forgery is especially relevant to AI applications containing:
- URL import
- web browsing
- document fetching
- image URL processing
- webhook processing
- agent tools
A secure URL-fetching service should validate:
scheme
hostname
resolved IP
redirect chain
port
response size
content type
timeout
It should also prevent access to sensitive internal network destinations.
63.26 Internal API Protection
Internal endpoints should not be assumed safe merely because they are not publicly documented.
Example:
/internal/admin/reindex
should still require:
service identity
authorization
input validation
audit logging
Security through obscurity is insufficient.
63.27 Gateway Error Handling
Error messages should not expose internal information.
Avoid returning:
Database connection failed at 10.x.x.x
PostgreSQL password authentication failed
Provider secret xyz...
Instead return:
{
"error": "internal_error",
"request_id": "req_123"
}
Detailed diagnostics should remain in protected logs.
63.28 Request Correlation
Every request should receive a correlation identifier.
Example:
request_id = req_123
trace_id = trace_456
The identifiers can follow the request:
Gateway
↓
AI Service
↓
Model Gateway
↓
Provider
↓
Worker
This dramatically improves debugging and incident investigation.
63.29 Distributed Tracing
A trace can represent:
HTTP Request
├── authentication
├── database query
├── AI routing
├── model request
├── storage
└── notification
Engineers can identify where latency and failures occur.
Security teams can also correlate suspicious activity across services.
63.30 Circuit Breaking
Internal services can fail too.
Example:
AI Provider
↓
unavailable
↓
Model Gateway
↓
requests accumulate
Circuit breakers can stop repeated calls after a defined failure threshold.
This reduces cascading failures.
63.31 Timeouts
Every network request should have an explicit timeout.
Examples:
Authentication: short
Database: bounded
Internal API: bounded
AI provider: model-specific
File service: bounded
Webhook: bounded
Never assume that a network call will eventually return.
63.32 Retry Policy
Retries should be carefully designed.
For safe operations:
timeout
↓
retry with backoff
For non-idempotent operations:
timeout
↓
determine whether execution occurred
↓
use idempotency mechanism
Otherwise, a retry could duplicate an external side effect.
63.33 Request Authentication Context
The gateway can establish an authenticated context, but downstream services should receive only the minimum information they require.
Example:
{
"subject": "user_123",
"tenant": "tenant_456",
"scopes": [
"generation:create"
],
"trace_id": "trace_789"
}
Sensitive authentication material should not be unnecessarily propagated through every service.
63.34 Tenant Isolation
Multi-tenant AI systems require tenant context across service calls.
Example:
Gateway
↓ tenant=A
AI Service
↓ tenant=A
Storage
↓ tenant=A
Database
A service must not accept:
authenticated_user = A
tenant_id = B
without verifying that the relationship is authorized.
63.35 Cross-Tenant Request Defense
Every service that accepts resource identifiers should verify ownership.
For example:
GET /generations/gen_123
should not mean:
If gen_123 exists → return it
It should mean:
Does gen_123 belong to the authorized tenant/user?
This is one of the most important controls in SaaS security.
63.36 API Gateway for Agents
AI agents should use a controlled API boundary.
Instead of:
Agent
↓
Internet
use:
Agent
↓
Tool Gateway
↓
Policy
↓
Approved Tool
This extends the security architecture from Chapter 61.
The agent should not receive unrestricted network capability.
63.37 Tool Gateway
A tool gateway can enforce:
tool allowlist
authentication
authorization
argument validation
rate limits
resource scope
approval requirements
audit logging
network restrictions
Example:
Agent
↓
search_web
↓
Tool Gateway
↓
Policy Check
↓
Execution
63.38 Secure Webhook Architecture
External systems may call webhooks.
A webhook endpoint should validate:
signature
timestamp
event type
schema
event ID
replay protection
The system should not trust:
POST /webhook
simply because the URL is secret.
63.39 Webhook Replay Defense
Suppose an attacker obtains a previously valid webhook.
They may attempt:
same event
same signature
same payload
again.
The receiver should use:
event_id
timestamp
expiration window
processed-event store
to prevent inappropriate replay.
63.40 Secure Internal DNS
Internal service discovery should be controlled.
Instead of arbitrary host resolution:
service-a → random-host
use managed service discovery:
ai-service.internal
storage-service.internal
billing-service.internal
Network policy should still enforce which workloads can communicate.
63.41 Secrets and Service Mesh
Service-to-service credentials should be rotated.
Avoid:
permanent static secret
Prefer:
short-lived identity
↓
credential
↓
expiration
↓
automatic renewal
This limits the impact of credential compromise.
63.42 Gateway Security Headers
For browser-facing APIs and web applications, appropriate security headers can help reduce browser attack surface.
Depending on the application:
Content-Security-Policy
Strict-Transport-Security
X-Content-Type-Options
Referrer-Policy
Permissions-Policy
Headers should be configured according to the actual application requirements rather than copied blindly.
63.43 API Logging
Log useful security metadata:
timestamp
request_id
trace_id
subject
tenant
route
method
status
latency
rate-limit result
authorization result
service identity
Avoid logging:
passwords
access tokens
API keys
private credentials
unnecessary sensitive content
63.44 API Abuse Detection
Monitoring should identify abnormal patterns.
Examples:
sudden request spike
credential failures
cross-tenant access attempts
unusual model usage
large token consumption
repeated policy violations
unexpected service calls
unusual geographic patterns
Detection should feed into the incident-response system.
63.45 API Gateway Architecture
A production architecture may look like:
INTERNET
│
▼
┌─────────────────┐
│ Load Balancer │
└────────┬────────┘
│
▼
┌─────────────────┐
│ API Gateway │
├─────────────────┤
│ TLS │
│ Auth │
│ Rate Limits │
│ Validation │
│ Routing │
│ Observability │
└────────┬────────┘
│
┌────────────┼────────────┐
▼ ▼ ▼
Auth API AI API Media API
│ │ │
└────────────┼────────────┘
▼
Internal Network
│
┌─────────┴─────────┐
▼ ▼
Model Gateway Worker Services
│ │
└─────────┬─────────┘
▼
Data Services
63.46 Secure Communication Principles
A mature service architecture follows these rules:
Authenticate every service
Authorize every sensitive operation
Encrypt communication
Validate every request
Limit every resource
Observe every important action
Expire temporary authority
Isolate tenants
Restrict network access
Protect secrets
63.47 Production Checklist
Gateway
- TLS enforced
- authentication integrated
- request limits configured
- schema validation enabled
- rate limiting enabled
- routing controlled
- API versions managed
- security logging enabled
Services
- unique service identity
- least-privilege permissions
- independent authorization
- input validation
- bounded timeouts
- controlled retries
- structured errors
Network
- segmentation
- private data services
- controlled ingress
- controlled egress
- service-to-service authentication
- network policies
AI
- provider credentials isolated
- model allowlist
- token quotas
- cost controls
- prompt/output policies
- agent tool gateway
Observability
- request IDs
- trace IDs
- service identities
- audit logs
- anomaly detection
- security alerts
63.48 Final Principle
The most dangerous assumption in a distributed AI system is:
“The request came from inside, therefore it is trusted.”
A stronger model is:
Who are you?
↓
What are you allowed to do?
↓
What resource are you accessing?
↓
Is this request valid?
↓
Is it within policy?
↓
Is it within quota?
↓
Can the operation be safely executed?
↓
Was the result valid?
↓
Was everything recorded?
The API gateway provides the first controlled boundary.
The service layer provides additional authorization.
The service mesh and network layer provide communication security.
The application itself remains responsible for business authorization.
Together, these controls create a defense-in-depth architecture in which compromise or failure of one layer does not automatically grant unrestricted access to the entire AI platform.
Next chapter: Chapter 64 — Secure AI Database & Data Access Layer: Database Authorization, Row-Level Security, Encryption, Query Safety, Connection Security, Tenant Isolation, Backup Protection & Data Exfiltration Defense.
Top comments (0)