kha is the command-line interface for Kimi Hosted Agents, providing the same resource management and session operations. Once installed, you can use natural language in AI coding assistants such as Kimi Code, Claude Code, and Codex to create and configure agents and execution environments, and to create sessions. You can also submit and continue tasks, inspect run status, and download artifacts.
Installation and authentication
Before you begin, prepare an API key with access to Hosted Agents.Install kha CLI
Run the one-line installer in your terminal:- macOS / Linux
- Windows (PowerShell)
~/.local/bin by default. Set KHA_INSTALL_DIR to customize the directory, or set KHA_VERSION=0.X.Y to install a specific version (defaults to the latest stable release).ha-static.moonshot.cn, which serves all regions. To download from GitHub Releases instead, set KHA_RELEASE_BASE_URL=https://github.com/MoonshotAI/kha/releases/download and pass an explicit version (KHA_VERSION=0.X.Y on macOS/Linux, or -Version 0.X.Y on Windows).
Verify the installation:
Install the kha skill package
When usingkha inside an AI coding assistant, install the companion kha skill package so the assistant invokes the CLI following built-in best practices. The skill package ships with the CLI binaryâno network download is needed:
--dir to install into a custom directory. Once installed, reload skills or open a new session in your coding assistant to confirm the skill is active. If you prefer running CLI commands directly without an assistant, see Check CLI command usage.
Upgrade kha CLI
The CLI checks for updates once per day and prints a notice tostderr when a newer version is available, without affecting command output on stdout. Set KHA_NO_UPDATE_CHECK=1 to disable update checks (skipped automatically in CI environments). To upgrade manually:
Configure authentication
Create an API key on Kimi API Platform, then run the following command in your local terminal:--base-url https://api.moonshot.ai to connect to the Global API endpoint; without it (or KHA_BASE_URL), the CLI defaults to the CN endpoint. Paste your API key at the hidden prompt and press Enter. In scripts or non-interactive environments, run kha login --base-url https://api.moonshot.ai --api-key-stdin to read the key from standard input instead.
Always enter your API key directly in a local terminalânever pass it as a command-line argument or paste it into an AI coding assistant chat.
login only saves credentials locally; run whoami to call the API and verify your identity:
KIMI_API_KEY is already set in your terminal environment, the CLI can also use it directlyâset KHA_BASE_URL=https://api.moonshot.ai alongside it so requests go to the Global endpoint. To manage multiple accounts or environments, store separate configurations in named profiles:
Complete your first task in natural language
Send the following prompt in your AI coding assistant:- Inspect existing agents and execution environments, creating new ones only if none fit the task.
- Wait until the execution environment status reaches
ready. - Create a session and send the sales data and aggregation instructions.
- Check the agentâs response and artifacts to confirm the output is complete.
- Download
sales-report.mdand return the key takeaways, session ID, and local file path.
Common management workflows
You can use the example prompts below directly in your AI coding assistantâreplace resource names, IDs, and file paths with your own values.
Vaults, memory stores, and GitHub repositories must be bound when creating a session. After updating an agent, create a new session to verify the changeâexisting sessions continue using the agent version pinned at creation. When a trigger reports that its initial event was accepted, inspect the corresponding sessionâs responses and artifacts to verify completion.
Work across the Console and API
Agents, sessions, and artifacts created with the kha CLI remain fully accessible in both the Console and the REST API. You can continue issuing natural-language instructions in your AI coding assistant, open the session in the Console, or call the API to send follow-up messages, inspect events, or cancel the current turn. The Console works well for visual configuration, OAuth authorization, run observability, and artifact inspection, while the API is well suited for integrating follow-up operations into your own applications. You can send the initial task message directly through the kha CLI without opening the Console.Troubleshooting
Check CLI command usage
You can also run the kha CLI directly without an AI coding assistant. View the full command reference matching your installed CLI version:kha docs <resource> <command>.
Request body input
When creating or updating resources directly with the CLI, you can pass request parameters in two ways:- Field flags: Pass top-level scalar fields as command options, such as
kha sessions create --agent "$AGENT_ID" --environment "$ENVIRONMENT_ID" --title "Weekly report". - Full request body: Pass nested structures (such as
resources,tools,mcp_servers, ormetadata) as a single payload via--filein JSON or YAML format;--file -reads from standard input.
--file. Because request body fields match the REST API contract directly, you can pass any JSON body from cURL examples in these docs straight to --file, or write the equivalent payload in YAML:
--text-file, --payload-file, and --content-file to read the text verbatim without structured parsing.
Next steps
Manage running sessions
Inspect session status, check queued inputs, and cancel the current turn.
Continue in the Console
Configure resources, observe runs, and inspect artifacts in your browser.