> ## Documentation Index
> Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Execution Environments

> Configure preinstalled packages, network access policies, and setup scripts for cloud environments, and bind an environment by ID when creating a session.

Kimi Hosted Agents provides two types of execution environments: platform-managed cloud environments and self-hosted environments. Every session must be bound to an execution environment via `environment_id` when it is created. This page covers cloud environments; to run a worker on your own infrastructure, see [Self-Hosted Environments](https://platform.kimi.ai/docs/hosted-agents/self-hosted-environments.md).

> [!NOTE]
>
> Each session runs in its own isolated sandbox instance. Sessions bound to the same execution environment do not share workspace state.

## Create an execution environment

Send a `POST /v1/environments` request to create an execution environment. The following example preinstalls packages with `pip` and `apt`, restricts outbound network access, and creates a working directory after package installation completes:

```bash cURL theme={null}
curl -sS -X POST "https://api.moonshot.ai/v1/environments" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -H "kimi-api-version: 2026-09-01-beta" \
  -H "Content-Type: application/json" \
  -d @- <<'JSON'
{
  "name": "Data Analysis Environment",
  "description": "Execution environment for data analysis tasks",
  "config": {
    "type": "cloud",
    "packages": {
      "pip": ["polars"],
      "apt": ["graphviz"]
    },
    "networking": {
      "type": "limited",
      "allowed_hosts": ["api.example.com"]
    },
    "setup_script": "mkdir -p /workspace/data"
  }
}
JSON
```

```bash CLI theme={null}
kha environments create --file - <<'JSON'
{
  "name": "Data Analysis Environment",
  "description": "Execution environment for data analysis tasks",
  "config": {
    "type": "cloud",
    "packages": {
      "pip": ["polars"],
      "apt": ["graphviz"]
    },
    "networking": {
      "type": "limited",
      "allowed_hosts": ["api.example.com"]
    },
    "setup_script": "mkdir -p /workspace/data"
  }
}
JSON
```

The response includes the environment's `id` and current `build_status`. Save the `id` so you can pass it as `environment_id` when creating a session.

## Configure an execution environment

For cloud environments, set `config.type` to `"cloud"`. The `packages`, `networking`, and `setup_script` fields are all optional—include or omit them based on your workload.

### Preinstalled packages

Declare dependencies to preinstall under `packages`, grouped by package manager. The platform installs these packages before the agent starts, and the resulting image is reused across sessions bound to the same execution environment.

| Package manager | Field | Example |
| - | - | - |
| System packages (`apt-get`) | `packages.apt` | `"graphviz"` |
| Rust (`cargo install`) | `packages.cargo` | `"ripgrep@14.0.0"` |
| Go modules | `packages.go` | `"golang.org/x/tools/cmd/goimports@latest"` |
| Node.js (`npm`) | `packages.npm` | `"express@4.18.0"` |
| Python (`pip`) | `packages.pip` | `"polars"` |

Each entry uses the native version syntax of its package manager. Specify a version to pin it, or omit the version to install the latest release. You can configure at most 128 entries per package manager.

### Network access

The `networking` field controls outbound network access from processes inside the sandbox. It does not affect network access performed by platform-managed tools. When `networking` is omitted, the environment uses `unrestricted` mode.

| Mode | Description |
| - | - |
| `unrestricted` | **Default**. Allows outbound access to the public internet while still blocking addresses on standard security blocklists. |
| `limited` | **Recommended for production**. Allows outbound access only to specified hosts, with optional flags for MCP servers and public package registries. |

In `limited` mode:

* Entries in `allowed_hosts` fall into two categories. Domain entries support exact hostnames and `*.example.com` wildcards (matching subdomains at any depth, excluding the apex domain itself) and may include a port (such as `api.example.com:8443`). If you omit the port, only ports 80 and 443 are allowed. IP address and CIDR block entries (such as `1.2.3.4` or `10.0.0.0/8`) allow direct TCP connections on all ports and must not include a port. Entries must not include a URL scheme (such as `https://`) or a path. You can configure at most 128 entries; invalid entries are rejected with HTTP `400` when creating or updating the execution environment.
* `allow_mcp_servers` controls whether to additionally allow access to MCP server endpoints configured on the agent. Defaults to `false`.
* `allow_package_managers` controls whether to additionally allow access to public package registries such as PyPI and npm. Defaults to `false`.

Both modes support configuring `proxy` to route outbound traffic for specified hosts through your own proxy server. `proxy.hosts` lists the hosts to route through the proxy; in `limited` mode, hosts listed in `proxy.hosts` can be accessed through the proxy directly without also adding them to `allowed_hosts`:

```json theme={null}
{
  "config": {
    "type": "cloud",
    "networking": {
      "type": "limited",
      "proxy": {
        "url": "https://proxy.example.com:8443",
        "auth": {
          "type": "basic",
          "vault_id": "vlt_your_vault_id",
          "credential_id": "vcrd_your_credential_id"
        },
        "hosts": ["api.example.com"]
      }
    }
  }
}
```

| `auth.type` | Credential value in Vault | Requirement |
| - | - | - |
| `basic` | `user:password` | Uses HTTP Basic authentication |
| `client_cert` | PEM bundle containing the certificate chain and private key | Uses TLS client certificate (mTLS) authentication |

Omit `auth` if your proxy does not require authentication. In production environments, the proxy URL must use HTTPS regardless of authentication method; `client_cert` requires HTTPS in all environments.

The optional `proxy.ca` field accepts a PEM-encoded CA certificate bundle. Proxy credentials are referenced by `vault_id` and `credential_id`, so plaintext secrets are never stored in the execution environment configuration. The referenced credential must be an active `environment_variable` credential in the vault; other credential types are rejected with HTTP `400`. See [Vaults](https://platform.kimi.ai/docs/hosted-agents/vaults.md) to create and manage credentials.

> [!WARNING]
>
> For production workloads, explicitly set `networking.type` to `limited` and follow the principle of least privilege by allowing only the hosts your agent actually needs. Omitting `networking` uses `unrestricted` mode.

### Setup script

Use `setup_script` for one-time preparation tasks such as creating directories or generating configuration files.

| Constraint | Value |
| - | - |
| Maximum length | 65,536 characters |
| Interpreter | `bash -c`, with `set -eo pipefail` prepended automatically |
| Execution timing | Runs as the final build step, after all preinstalled packages are installed |
| Failure behavior | If the script exits with a non-zero status, the build fails |

## Use in a session

Pass `environment_id` when creating a session to bind it to an execution environment. You cannot change a session's execution environment after binding. For complete request examples, see [Create a Session](https://platform.kimi.ai/docs/hosted-agents/sessions.md).

To be used for new sessions, an execution environment must be visible to the caller, must not be archived, and must have at least one `ready` version.

## Wait for the environment build to finish

After you create an environment or replace its configuration, the platform builds a new version asynchronously. If you want new sessions to use the configuration you just submitted, poll the environment by ID until `build_status` becomes `ready`:

```bash cURL theme={null}
curl -sS "https://api.moonshot.ai/v1/environments/$ENVIRONMENT_ID" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -H "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha environments get "$ENVIRONMENT_ID"
```

`build_status` indicates the build status of the latest configuration version:

| Status | Meaning |
| - | - |
| `building` | Build in progress |
| `ready` | Build succeeded; ready for new sessions |
| `failed` | Build failed |

If the latest version is still `building` or has `failed`, new sessions fall back to the most recent `ready` version. Session creation fails only if the execution environment has no `ready` version at all.

A session pins the latest `ready` version of the execution environment at creation time and continues using that version for its entire lifetime. Subsequent updates to the execution environment do not affect existing sessions.

## Update an execution environment

Send a `PATCH /v1/environments/{id}` request to update the fields included in the request body. When replacing `config`, submit the complete `config` object you want to retain. The following example keeps the existing configuration and adds DuckDB:

```bash cURL theme={null}
curl -sS -X PATCH "https://api.moonshot.ai/v1/environments/$ENVIRONMENT_ID" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -H "kimi-api-version: 2026-09-01-beta" \
  -H "Content-Type: application/json" \
  -d @- <<'JSON'
{
  "config": {
    "type": "cloud",
    "packages": {
      "pip": ["polars", "duckdb"],
      "apt": ["graphviz"]
    },
    "networking": {
      "type": "limited",
      "allowed_hosts": ["api.example.com"]
    },
    "setup_script": "mkdir -p /workspace/data"
  }
}
JSON
```

```bash CLI theme={null}
kha environments update "$ENVIRONMENT_ID" --file - <<'JSON'
{
  "config": {
    "type": "cloud",
    "packages": {
      "pip": ["polars", "duckdb"],
      "apt": ["graphviz"]
    },
    "networking": {
      "type": "limited",
      "allowed_hosts": ["api.example.com"]
    },
    "setup_script": "mkdir -p /workspace/data"
  }
}
JSON
```

The platform saves the updated configuration as a new version and builds it asynchronously. New versions do not affect existing sessions, and you cannot change `config.type` when updating an execution environment.

## Manage execution environments

List execution environments visible to the caller:

```bash cURL theme={null}
curl -sS "https://api.moonshot.ai/v1/environments?page_size=20" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -H "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha environments list --limit 20
```

Archived environments are excluded by default. Pass `include_archived=true` to include them.

Archive an execution environment that should no longer be used for new sessions:

```bash cURL theme={null}
curl -sS -X POST "https://api.moonshot.ai/v1/environments/$ENVIRONMENT_ID/archive" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -H "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha environments archive "$ENVIRONMENT_ID"
```

Archiving cannot be undone. An archived environment cannot be used to create new sessions, while existing sessions remain unaffected. A successful archive request returns HTTP `204`.

Delete an execution environment you no longer need. Only archived execution environments can be deleted; deleting an unarchived environment fails:

```bash cURL theme={null}
curl -sS -X DELETE "https://api.moonshot.ai/v1/environments/$ENVIRONMENT_ID" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -H "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha environments delete "$ENVIRONMENT_ID"
```

Deletion also fails if any non-terminated sessions still reference the environment. A successful delete request returns HTTP `204`.

## Related resources

> [card] [Sandbox Reference](https://platform.kimi.ai/docs/hosted-agents/sandbox-reference.md)
>
> Review the operating system, preinstalled runtimes and tools, filesystem layout, and network boundaries of cloud sandboxes.

> [card] [Create a Session](https://platform.kimi.ai/docs/hosted-agents/sessions.md)
>
> Learn how to create a session and bind an execution environment.
