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 and outbound proxy rules).
Differences from cloud environments
The following differences are expected behavior in the current release.How it works
- When you create a session bound to a self-hosted environment, the agent loop continues to run in the cloud.
- 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.
- The worker reports tool outputs back to the session, and the agent loop resumes execution.
Prerequisites
- An available agent: Used when creating a session. If you do not have one yet, complete the Quickstart 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_KEYis 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, go to Hosted Agents > Environments, click New environment, and select Self-hosted.You can also create the environment via the management API: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):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 The installer is served globally from Start the worker from the directory where you want tasks to run:
kha CLI on the worker host: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:--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:Once the task completes, check the file in the working directory on your worker host: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_KEYkeys are for management API calls only—do not store them in worker images or working directories, or pass them into tool subprocess environments.
Troubleshooting
The worker exits after 60 seconds of inactivity
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.How do I start a worker without a system keychain?
How do I start a worker without a system keychain?
In containers or CI environments without a system keychain, skip Replace
kha worker login and start the worker directly with a key file:/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.The worker does not respond after I send a task
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
Create a Session
Create a session and send tasks via the console or API.
Execution Environments
Configure cloud environments and manage environments via the API.