AI agents are increasingly being used for tasks that require more than generating text. An agent may execute code, process files, call APIs, run data transformations, or perform multiple tasks at the same time.
That creates an important production challenge: how do you let AI agents execute work without giving them direct access to the application server?
Running agent-generated or user-provided code inside the same process as your main application can create unnecessary security and reliability risks. A better architecture is to isolate execution inside a sandbox and treat every execution request as untrusted work.
Azure Container Apps provides a useful foundation for this architecture. Containers can be used as isolated execution environments, while parallel agent workloads can be distributed across multiple container instances.
This article explains how to design a secure parallel AI-agent execution architecture using Azure Container Apps sandboxes, with a focus on isolation, resource control, communication, failures, and production considerations.
Why AI Agents Need Sandboxing
Traditional applications generally execute code that developers have written and reviewed.
AI agents introduce another category of workload. An agent may generate code dynamically based on a user request or decide which tools to invoke.
For example, an AI agent might receive:
Analyze this CSV file, calculate statistics,
generate a report, and save the result.
The agent may need to execute a series of operations:
User Request
|
v
AI Agent
|
+----> Data Processing
|
+----> Code Execution
|
+----> File Processing
|
+----> Report Generation
If these operations run directly inside the main application environment, a failure or unsafe operation can affect the application itself.
Sandboxing creates a boundary:
Main Application
|
v
AI Agent
|
Execution Request
|
+----------------+----------------+
| | |
v v v
Sandbox 1 Sandbox 2 Sandbox 3
Agent Task Agent Task Agent Task
Each execution environment can be treated as temporary infrastructure rather than part of the trusted application process.
What Is a Container Sandbox?
A container sandbox is an isolated environment where a workload can execute without directly sharing the application's runtime environment.
For AI agents, the sandbox can contain:
Generated code
Temporary files
Runtime dependencies
Input data
Output artifacts
Process execution
The main application communicates with the sandbox through a controlled interface.
A simplified architecture looks like this:
+---------------------------+
| Client / User |
+-------------+-------------+
|
v
+---------------------------+
| Agent Orchestrator |
| |
| - Task planning |
| - Authorization |
| - Validation |
+-------------+-------------+
|
v
+---------------------------+
| Execution Dispatcher |
+------+------+-------------+
| |
| |
v v
+-------+ +-------+
|Sandbox| |Sandbox|
| A | | B |
+-------+ +-------+
| |
+----+---+
|
v
Results / Artifacts
The important security principle is that the agent should not receive unrestricted access to the orchestrator.
Why Azure Container Apps?
Azure Container Apps can host containerized workloads without requiring you to manage the underlying Kubernetes control plane directly.
For an agent-execution architecture, that can simplify several infrastructure concerns.
A containerized sandbox can provide a consistent runtime containing the dependencies required by a particular task.
For example:
Sandbox Image
|
+-- .NET Runtime
+-- Python Runtime
+-- Required Libraries
+-- Execution Worker
When a task arrives, the execution environment can process it and return a result.
This model is especially useful when agent workloads are independent and can be executed concurrently.
Designing Parallel Agent Execution
Suppose an AI agent needs to process three independent tasks:
Task A: Analyze sales data
Task B: Process customer feedback
Task C: Generate an inventory report
Running them sequentially creates a simple workflow:
Task A → Task B → Task C
Parallel execution changes the model:
+--> Task A
|
Agent -------+--> Task B
|
+--> Task C
Each task can be dispatched to an isolated execution environment.
The orchestrator should track each task independently.
A simple C# model could look like this:
public sealed record AgentTask(
string Id,
string Type,
string Payload);
public sealed record AgentTaskResult(
string TaskId,
bool Success,
string? Output,
string? Error);
The task identifier is important because parallel operations can complete in any order.
Use a Dispatcher Instead of Direct Execution
The main application should not directly execute arbitrary agent-generated commands.
Instead, introduce an execution dispatcher.
public interface IAgentExecutionDispatcher
{
Task<AgentTaskResult> ExecuteAsync(
AgentTask task,
CancellationToken cancellationToken);
}
The dispatcher becomes a policy boundary.
It can validate:
Task type
User authorization
Input size
Allowed runtime
Resource limits
Timeout
Required permissions
Only after validation should the task be sent to a sandbox.
This separation makes the architecture easier to secure and test.
Validate Before Starting a Sandbox
Never assume that every task generated by an agent should be executed.
For example:
public bool IsAllowed(AgentTask task)
{
if (string.IsNullOrWhiteSpace(task.Type))
return false;
if (task.Payload.Length > 1_000_000)
return false;
return task.Type switch
{
"data-analysis" => true,
"document-processing" => true,
"code-execution" => false,
_ => false
};
}
The exact rules depend on the application.
The important idea is that the agent's decision should not automatically become an infrastructure command.
The orchestrator should enforce the policy independently.
Restrict Network Access
Network access is one of the most important considerations when executing untrusted workloads.
A sandbox may need network access for legitimate reasons, but unrestricted outbound access can significantly increase the potential impact of a compromised workload.
For example, an execution environment might attempt to access:
Internal APIs
Cloud services
Metadata endpoints
Private databases
External websites
The safest architecture is to allow only the network destinations actually required by the workload.
If a task only processes an uploaded file, it may not need external network access at all.
The principle is simple:
No network access unless the workload requires it.
When network access is necessary, use explicit allowlists and appropriate identity and access controls.
Do Not Pass Secrets Into the Sandbox
A sandbox should not automatically inherit the application's credentials.
For example, avoid making sensitive credentials available through the execution environment simply because the main application can access them.
Instead, use scoped access where possible.
The architecture should look like:
Main Application
|
| Controlled identity
v
Specific Service
rather than:
Main Application
|
| All application credentials
v
Sandbox
If a task requires access to a service, provide only the minimum permission required for that operation.
Add Execution Timeouts
An agent-generated workload can fail to terminate.
Without a timeout, a task could consume resources indefinitely.
In .NET, cancellation can be used to establish an execution boundary:
using var timeout =
CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken);
timeout.CancelAfter(TimeSpan.FromMinutes(2));
await dispatcher.ExecuteAsync(
task,
timeout.Token);
The timeout should be selected based on the expected workload.
Long-running tasks may require an asynchronous job architecture rather than keeping an HTTP request open.
Control CPU, Memory, and Storage
Isolation is not only about security.
It is also about resource management.
One poorly behaved task should not consume resources needed by other workloads.
Useful controls include:
Resource | Control |
|---|---|
CPU | Limit available compute |
Memory | Define memory limits |
Storage | Use temporary bounded storage |
Runtime | Enforce execution timeout |
Network | Restrict destinations |
Concurrency | Limit simultaneous tasks |
Input | Restrict payload size |
Output | Restrict artifact size |
These limits should be enforced outside the AI model's decision-making process.
The model can request an operation, but infrastructure policy determines whether that operation is allowed.
Handling Parallel Failures
Parallel execution means that some tasks may succeed while others fail.
For example:
Task A → Success
Task B → Timeout
Task C → Success
Task D → Resource Limit
Do not treat this as one generic failure.
Track individual task states.
A useful state model is:
public enum AgentTaskStatus
{
Pending,
Running,
Completed,
Failed,
TimedOut,
Cancelled
}
The orchestrator can then decide whether a failed task should be retried, ignored, or cause the larger workflow to stop.
Retry Carefully
Retries are useful for infrastructure failures, but they should not automatically be applied to every execution failure.
For example:
Container startup failure → Potential retry
Temporary service failure → Potential retry
Invalid agent-generated code → Usually no retry
Security policy violation → No retry
Invalid input → No retry
Blind retries can increase resource consumption and may repeatedly execute unsafe operations.
For tasks with side effects, idempotency should also be considered before implementing retries.
Logging and Observability
An AI-agent sandbox architecture should provide enough logging to answer:
Who initiated the task?
Which agent requested it?
What task type was requested?
Which sandbox processed it?
When did execution start?
How long did it run?
Did it succeed?
Why did it fail?
What resources were consumed?
Avoid logging secrets or sensitive input data simply for debugging.
A useful task record might contain:
public sealed record ExecutionRecord(
string TaskId,
string AgentId,
DateTimeOffset StartedAt,
DateTimeOffset? CompletedAt,
AgentTaskStatus Status);
This provides traceability without requiring the entire task payload to be stored in every log entry.
Common Mistakes
Running Agent Code Inside the Main Application
This removes an important isolation boundary.
Use a separate execution environment for untrusted workloads.
Giving Every Sandbox Full Network Access
Network access should be explicit and limited.
Passing Cloud Credentials Into Containers
Use scoped identity and permissions rather than copying credentials into the execution environment.
Allowing Unlimited Resource Consumption
CPU, memory, storage, concurrency, and execution time should have reasonable limits.
Treating Every Failure as Retryable
Different failures require different handling.
Trusting the Agent to Enforce Security
The model should never be the final security boundary.
Application and infrastructure controls must enforce the rules independently.
Advantages and Disadvantages
Advantages
A sandboxed container architecture can provide:
Better workload isolation
Controlled execution of agent-generated work
Independent resource limits
Parallel task execution
Easier cleanup of temporary workloads
Clear separation between orchestration and execution
A stronger security boundary than running arbitrary work inside the main process
Disadvantages
There are also trade-offs:
Additional infrastructure complexity
Container startup and scheduling overhead
More components to monitor
More complicated failure handling
Additional networking configuration
Cost associated with running execution workloads
More effort required to manage policies and resource limits
Sandboxing is therefore an architectural decision rather than a feature that can simply be added without considering the workload.
Summary
Secure AI-agent execution requires more than model-level safeguards. When agents need to execute code or process potentially untrusted workloads, isolated container environments can provide an important execution boundary.
A practical architecture separates the agent orchestrator from the execution layer, validates tasks before execution, limits resources, restricts network access, protects credentials, tracks individual task failures, and keeps high-impact operations under explicit control.
The result is a more manageable foundation for running parallel AI-agent workloads without treating the application server as an unrestricted execution environment.

Join the conversation! Your thoughts help the community grow.