> ## 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.

# Self-Hosted Environments

> Run Kimi Hosted Agents sandbox tools on your own infrastructure using the Console and kha CLI.

A self-hosted environment runs sandbox tools on hosts that you manage, while the platform continues to handle model inference, agent orchestration, and session management.

> [!WARNING]
>
> Self-hosted environments are available only to verified enterprise organizations. Complete enterprise verification before using this feature.

> [!NOTE]
>
> A self-hosted environment is not an on-premises deployment of the full platform. Tool inputs and execution outputs still pass through the platform's model inference endpoints.

## Use cases

* You need code execution and file processing to remain within your own infrastructure so that your organization can apply its compliance, auditing, and operational controls to the hosts running sandbox tools.
* You need to reach services accessible only on an internal corporate network (you can also achieve this with a [cloud sandbox](https://platform.kimi.ai/docs/hosted-agents/environments.md#network-access) and outbound proxy rules).

## Differences from cloud environments

The following differences are expected behavior in the current release.

| Capability | Cloud environment | Self-hosted environment |
| - | - | - |
| Built-in sandbox toolset versions | Supports `agent_toolset_20261101` (default) and `agent_toolset_20260901` | Supports `agent_toolset_20261101` and `agent_toolset_20260901` (including the legacy alias `default_toolset_20260901`) |
| `read_file`, `write_file`, `edit_file`, `shell`, `todo_read`, `todo_write` (`agent_toolset_20260901`) | Executed in the cloud sandbox; billed by sandbox runtime | Executed on the worker host with the worker host's OS permissions; no sandbox charge from the platform |
| `read`, `write`, `edit`, `bash`, `glob`, `grep` (`agent_toolset_20261101`) | Executed in the cloud sandbox; `read` supports image, video, PDF, and Office document conversion | Implemented in the worker and executed on the worker host, with no sandbox charge; `read` reads local text files only, so use `bash` to process images, videos, PDFs, and Office documents |
| `web_search`, `web_open_url`, `web_fetch` | Executed by the platform; billed per call | Also executed by the platform and billed per call; see [Hosted Agents Pricing](https://platform.kimi.ai/docs/pricing/hosted-agents.md) |
| `ipython`, `search_image_by_text`, `search_image_by_image`, `save_artifact` | Executed in the cloud sandbox | Not currently supported |
| Session [files](https://platform.kimi.ai/docs/hosted-agents/files.md) | Uploaded to the cloud sandbox; tool outputs appear in the session file list | Uploaded files are not synced to the worker host; tool outputs on the host do not appear in the session file list |
| [Skills](https://platform.kimi.ai/docs/hosted-agents/skills.md) | Mounted at `/mnt/agents/skills/<name>/` | Delivered to `skills/<name>/` in the worker's working directory; access them via `${KHA_WORKDIR}/skills/<name>/SKILL.md` or relative paths |
| [Memory](https://platform.kimi.ai/docs/hosted-agents/memory.md) | Mounted into the cloud sandbox | Not mounted onto the worker host |

## How it works

1. When you create a session bound to a self-hosted environment, the agent loop continues to run in the cloud.
2. On your worker host, you run the official worker command to register with the environment. The worker then polls for work items in that environment and executes sandbox tool calls locally.
3. The worker reports tool outputs back to the session, and the agent loop resumes execution.

MCP servers and custom tools do not run through the worker; they continue to execute through their standard paths.

## Prerequisites

* **An available agent**: Used when creating a session. If you do not have one yet, complete the [Quickstart](https://platform.kimi.ai/docs/hosted-agents/quickstart.md) first.
* **A worker host**: The examples below use macOS or Linux. The host must have outbound HTTPS access to the platform.
* **A management API key (optional)**: `KIMI_API_KEY` is required only if you create the environment via the API; it is not needed when creating the environment in the console.

## Run your first session

**1. Create a self-hosted environment**

In the [Kimi API Platform Console](https://platform.kimi.ai/console/hosted-agents/resources), go to **Hosted Agents > Environments**, click **New environment**, and select **Self-hosted**.

You can also create the environment via the management API:

```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 '{
    "name": "Internal Network Environment",
    "config": {
      "type": "self_hosted"
    }
  }'
```

```bash CLI theme={null}
kha environments create --name "Internal Network Environment" --self-hosted
```

Self-hosted environments do not require a build step and are ready to use immediately. Copy the environment ID and export it on your worker host, along with the platform API endpoint (`kha worker` commands read `--base-url` or `KHA_BASE_URL` rather than `kha login` profiles, and default to the CN endpoint if unset):

```bash theme={null}
export ENVIRONMENT_ID="env_your_environment_id"
export KHA_BASE_URL="https://api.moonshot.ai"
```

**2. Generate an environment work key**

On the environment details page, open the **Worker** tab and click **Generate Environment Key**. The full key is shown only once—copy and store it securely right away.

Environment work keys start with `ewk-` and can only connect workers to that specific environment. If a host is decommissioned or a key is compromised, revoke it in the console and generate a new one.

**3. Install kha CLI and start the worker**

The commands below assume macOS or Linux. Install `kha` CLI on the worker host:

```bash theme={null}
curl -fsSL https://ha-static.moonshot.cn/kha/install.sh | env -u KHA_BASE_URL sh
export PATH="$HOME/.local/bin:$PATH"
```

The installer is served globally from `ha-static.moonshot.cn`. `env -u KHA_BASE_URL` prevents the API endpoint exported above from overriding the installer's download mirror.

Run the following command and paste the environment work key at the hidden prompt. The key is stored in your system keychain:

```bash theme={null}
kha worker login "$ENVIRONMENT_ID"
```

Start the worker from the directory where you want tasks to run:

```bash theme={null}
kha worker poll "$ENVIRONMENT_ID" --workdir "$PWD"
```

`--workdir "$PWD"` sets the current directory as the working directory for the `shell` tool. Keep this command running.

**4. Create a session and verify**

In the console, go to **Hosted Agents > Sessions**, select an existing agent and the environment you just created, create a session, and send the following task:

```text theme={null}
Use shell to create hello.txt in the current working directory containing the output of uname -a.
```

Once the task completes, check the file in the working directory on your worker host:

```bash theme={null}
cat hello.txt
```

The file should contain the system information of your worker host. You can also view active workers and queue status on the **Worker** tab of the environment details page.

## Networking and credentials

* The worker host needs outbound HTTPS access to `https://api.moonshot.ai`; you do not need to open any inbound ports for the work queue.
* Self-hosted sandbox tools use the network egress of the worker host. Execution locations for remote MCP servers, platform-managed tools, and custom tools remain unchanged.
* You are responsible for hardening the worker host, isolating processes and filesystems, managing dependencies, and provisioning credentials for external services.
* Standard `KIMI_API_KEY` keys are for management API calls only—do not store them in worker images or working directories, or pass them into tool subprocess environments.

## Troubleshooting

> [accordion] The worker exits after 60 seconds of inactivity
>
> By default, `kha worker poll` exits after 60 seconds without work. Add `--max-idle 0` to the command to keep the worker polling indefinitely.

> [accordion] How do I start a worker without a system keychain?
>
> In containers or CI environments without a system keychain, skip `kha worker login` and start the worker directly with a key file:
>
> ```bash theme={null}
> kha worker poll "$ENVIRONMENT_ID" \
>   --token-file /path/to/work-key \
>   --workdir "$PWD" \
>   --max-idle 0
> ```
>
> Replace `/path/to/work-key` with the path to your environment work key file. You can also read the key from standard input with `--token-stdin`. Do not pass the key directly as a command-line argument.

> [accordion] The worker does not respond after I send a task
>
> First confirm that the session is bound to your self-hosted environment and that you have sent a task (not just created the session). Then verify that the worker process is running, the environment ID matches the work key, the key has not been revoked, and the host can reach the platform API over outbound HTTPS.

## Related resources

> [card] [Create a Session](https://platform.kimi.ai/docs/hosted-agents/sessions.md)
>
> Create a session and send tasks via the console or API.

> [card] [Execution Environments](https://platform.kimi.ai/docs/hosted-agents/environments.md)
>
> Configure cloud environments and manage environments via the API.
