Use Agent Sandbox workloads in Knative

更新时间:
复制 MD 格式

Workloads like AI Agents or online IDEs have intermittently active user sessions. In a traditional serverless model, idle instances are destroyed, forcing a cold start and losing all runtime state upon the next access. Knative's Agent Sandbox workload addresses this by assigning a dedicated MicroVM to each session. When idle, the instance can be paused, preserving its file system and IP address. Upon the next access, it resumes in seconds. This model also supports autoscaling based on the number of active sessions and can scale to zero.

How it works

Agent Sandbox is a production-grade sandbox environment for AI Agents with the following core capabilities:

  • A MicroVM-level isolated runtime environment.

  • Memory-level pause and resume, with Checkpoint cloning capabilities.

  • Large-scale autoscaling of up to 15,000 sandboxes per minute.

  • Full compatibility with the native Kubernetes ecosystem and seamless integration with mainstream AI Agent frameworks and tools such as E2B SDK and AgentScope.

Alibaba Cloud Knative builds upon Agent Sandbox to provide serverless capabilities tailored for AI Agent scenarios. Unlike traditional stateless serverless models, Agent Sandbox supports:

  • Session affinity: Each session exclusively occupies one sandbox instance (target=1), and requests are always routed to the same instance.

  • State retention: When an instance is paused, its Pod IP and file system are preserved. When the instance resumes, it continues from where it left off.

  • Intelligent autoscaling: Instances automatically scale up or down based on the number of active sessions. Idle instances are automatically paused, allowing the service to scale to zero.

  • Warm pool: A specified number of instances are pre-created and paused. When a new session starts, it resumes a pre-warmed instance in seconds, eliminating cold starts.

Agent Sandbox supports two idle policies, depending on whether you need to retain the runtime state:

Aspect

Delete mode (default)

Pause mode

Behavior when idle

Deletes the instance.

Pauses the instance and retains its state.

On next access

A new instance is created through a cold start.

The existing instance resumes in seconds.

State retention

Not retained

IP address and file system

Resource usage

It consumes zero resources when idle.

A paused instance consumes a small amount of storage.

Use cases

Stateless tasks and batch processing

AI Agents, online IDEs, and long-running sessions

Prerequisites

  • You have or created a managed ACK cluster, and activated the Agent Sandbox service.

  • Ensure that your components meet the version requirements:

    To install or upgrade components, go to the ACK Cluster List page. Click the name of your target cluster. On the cluster details page, click Add-ons in the left-side navigation pane.
    • Upgrade Kube Scheduler to a supported version.

      Expand to view component version requirements

      Cluster version

      Kube Scheduler component version

      v1.28

      v1.28.12-apsara-6.11.8.9edb0089 or later

      v1.30

      v1.30.3-apsara.6.11.11.2155fa7b or later

      v1.31

      v1.31.0-apsara.6.11.7.17d202a9 or later

      v1.32

      v1.32.0-apsara.6.11.11.3187ac8f or later

      v1.33

      v1.33.0-apsara.6.11.12 or later

      v1.34

      v1.34.0-apsara.6.11.12 or later

      v1.35

      v1.35.0-apsara.6.11.12 or later

    • Install ACK Virtual Node v2.13.0 or later.

    • Install ack-agent-sandbox-controller v2.17.0 or later. You can use the default configuration.

    • You have installed Knative components v1.18.3 or later. For details, see Deploy and Manage Knative Components or .

  • (Optional, recommended for production) By default, the Activator stores session mappings in memory, which can be lost upon restart. For production environments or when running multiple Activator replicas, we recommend using Redis for storage. You must create an ApsaraDB for Tair (Redis-compatible) instance beforehand. For configuration details, see Configure session affinity for Knative services.

Configure Delete mode

The behavior of Delete mode is equivalent to a standard Knative Service with session persistence. This means that idle instances are deleted directly and recreated when they are accessed again. This mode is suitable for stateless tasks or batch processing scenarios that do not require a persistent runtime environment.

Create a sandbox service

  1. Apply the following configuration to your cluster to enable the sandbox workload type and session affinity.

    Replace registry-cn-wulanchabu-vpc in the image address with the endpoint for your region, such as registry-cn-hangzhou-vpc.
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: ai-agent-delete
    spec:
      template:
        metadata:
          annotations:
            # Enable the Sandbox workload type.
            serving.knative.dev/workload-type: "sandbox"
            # Enable session affinity to ensure requests from a user are routed to the same instance.
            serving.knative.dev/session-affinity: "true"
            # Release the instance after 30 minutes of inactivity.
            serving.knative.dev/session-timeout: "30m"
            # The cookie name used to identify the user session.
            serving.knative.dev/session-affinity-cookie: "app"
            # Scale based on the number of active sessions.
            autoscaling.knative.dev/metric: "session"
            # Strictly bind one session to each instance.
            autoscaling.knative.dev/target: "1"
        spec:
          containers:
          - image: registry-cn-wulanchabu-vpc.ack.aliyuncs.com/acs/knative-samples-helloworld-go-session:v1.0-bfdce98
  2. Wait for the ai-agent-delete service to be deployed. On the Services page, get the Gateway address and the service's Default Domain.

  3. Verify the result.

    1. The first request carries Cookie: app=11. Subsequent requests with the same cookie should be routed to the same Pod.

      curl -i -H "Host: ai-agent-delete.default.example.com" http://<GATEWAY_IP> \
        -H "Cookie: app=11"

      Expected output:

      HTTP/1.1 200 OK
      Date: Fri, 05 Jun 2026 08:00:53 GMT
      Content-Type: text/plain; charset=utf-8
      Content-Length: 75
      Connection: keep-alive
      
      Hello World!
      
      Cookies:
        Name:  app
        Value: 11
      
      Raw Cookie Header: app=11
    2. A subsequent request with Cookie: app=22 will access another Pod.

      curl -i -H "Host: ai-agent-delete.default.example.com" http://<GATEWAY_IP> \
        -H "Cookie: app=22"

      Expected output:

      HTTP/1.1 200 OK
      Date: Fri, 05 Jun 2026 08:02:49 GMT
      Content-Type: text/plain; charset=utf-8
      Content-Length: 75
      Connection: keep-alive
      
      Hello World!
      
      Cookies:
        Name:  app
        Value: 22
      
      Raw Cookie Header: app=22

Request routing logic

After the configuration is applied, user requests are processed as follows:

  1. The first user request arrives. No instances are available, so Sandbox-1 is created and the session is bound to it.

  2. The second user request arrives. Sandbox-1 is exclusively occupied by session-1 (target=1), so the request waits. APA automatically scales out Sandbox-2, and the session is bound to Sandbox-2.

  3. If a user is inactive for 30 minutes, the session times out, the instance is deleted, and the resources are released.

  4. No active users: Scales down to 0, consuming zero resources.

Configure Pause mode

Pause mode is a core feature of Agent Sandbox, designed for scenarios that require a persistent runtime environment, such as AI Agents, online IDEs, and remote development. When an instance becomes idle, it is paused instead of deleted. When the user accesses it again, the instance resumes its previous state in seconds, with all open files, running processes, and installed dependencies intact.

Create a sandbox service in Pause mode

  1. Compared to Delete mode, you must add the serving.knative.dev/sandbox-idle-policy: "pause" annotation.

    Replace registry-cn-wulanchabu-vpc in the image address with the actual region, such as registry-cn-hangzhou-vpc.
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: ai-agent-pause
    spec:
      template:
        metadata:
          annotations:
            serving.knative.dev/workload-type: "sandbox"
            serving.knative.dev/sandbox-idle-policy: "pause"
            serving.knative.dev/session-affinity: "true"
            # Specify the size of the warm pool (number of pre-warmed Sandbox instances).
            serving.knative.dev/warm-pool: "3"
            serving.knative.dev/session-timeout: "30m"
            serving.knative.dev/session-expiry: "2h"
            serving.knative.dev/session-affinity-cookie: "test"
            autoscaling.knative.dev/metric: "session"
            autoscaling.knative.dev/target: "1"
        spec:
          containers:
          - image: registry-cn-wulanchabu-vpc.ack.aliyuncs.com/acs/knative-samples-helloworld-go-session:v1.0-bfdce98
  2. Wait for the ai-agent-pause service to be deployed. On the Services page, get the Gateway address and the service's Default Domain.

  3. Verify the result.

    curl -i -H "Host: ai-agent-pause.default.example.com" http://<GATEWAY_IP> \
      -H "Cookie: app=11"

    Expected output:

    HTTP/1.1 200 OK
    Date: Fri, 05 Jun 2026 08:33:06 GMT
    Content-Type: text/plain; charset=utf-8
    Content-Length: 75
    Connection: keep-alive
    
    Hello World!
    
    Cookies:
      Name:  app
      Value: 11
    
    Raw Cookie Header: app=11

Scope of state retention

When an instance is paused, the following state is fully retained, eliminating the need for re-initialization upon resume.

Item

Description

IP address

The Pod IP address remains unchanged, and network connections can be restored.

File system

The entire file system within the container is preserved.

Configure warm pool

A warm pool is an advanced feature of Pause mode. It pre-creates and pauses a specified number of Sandbox instances. This allows a new user to immediately resume a pre-warmed instance, eliminating the wait for a cold start.

Use the following annotation to specify the size of the warm pool.

annotations:
  # Specify the size of the warm pool (number of pre-warmed Sandbox instances).
  serving.knative.dev/warm-pool: "3"

Annotation reference

Annotation

Description

Default

serving.knative.dev/workload-type

The workload type. Set to "sandbox" to enable Agent Sandbox.

None ("deployment")

serving.knative.dev/sandbox-idle-policy

The idle policy. Can be "delete" or "pause".

"delete"

serving.knative.dev/session-affinity

Enables session affinity.

"false"

serving.knative.dev/session-timeout

The session timeout period. The instance is released or paused after this period of inactivity.

"30m"

autoscaling.knative.dev/metric

The autoscaling metric. Set to "session" for session affinity use cases.

"concurrency"

autoscaling.knative.dev/target

The target number of sessions per instance.

"100"

serving.knative.dev/warm-pool

Only effective in Pause mode.

The size of the warm pool.

"0"

autoscaling.knative.dev/min-scale

The minimum number of instances.

"0"

autoscaling.knative.dev/max-scale

The maximum number of instances.

Unlimited

FAQ

Choosing between Pause and Delete mode

The choice depends on whether you need to retain the runtime state:

  • Use Pause mode if you need to preserve the container's state, such as its files and processes.

  • Use Delete mode if you do not require state retention and want to achieve zero resource usage when idle.

Request routing for multiple users

This is not the case when target=1 is set. Each Sandbox instance is strictly bound to a single session. When a new user arrives, they wait for a new instance to become ready or for a frozen instance from the warm pool to be resumed.

Session persistence on Activator restart

It depends on the session storage mode:

  • Memory mode: Limited session data can be recovered for a single instance.

  • Redis mode: Session data is fully recovered from Redis, with zero data loss.

For production environments, use Redis mode to ensure complete session recovery if the Activator restarts.