How to Use kimi-cli for Code Generation: A Complete Guide to AI-Assisted Programming

kimi-cli is a Python-based interactive agent that streams code generation requests directly to large language models via the command line, supporting one-shot prompts, interactive REPL sessions, and automatic file persistence through built-in tools.

The kimi-cli tool from MoonshotAI transforms your terminal into an AI-powered coding assistant capable of writing, refactoring, and explaining code in any programming language. By leveraging the KimiLLM wrapper and YAML-based agent specifications, you can execute complex kimi-cli code generation workflows without leaving your shell environment. This guide covers the complete workflow from installation to persisting generated files, based on the actual implementation in the MoonshotAI/kimi-cli repository.

Installing kimi-cli from Source

To begin using kimi-cli for code generation, you must install the tool directly from the GitHub repository. The project requires Python 3.12 or higher and uses uv for dependency management.

Run the following commands to clone and prepare the environment:

git clone https://github.com/MoonshotAI/kimi-cli.git
cd kimi-cli
make prepare

The make prepare command, defined in the root Makefile, synchronizes dependencies using the uv package manager and installs necessary git hooks. Once complete, the kimi binary becomes available in your environment.

One-Shot Code Generation

The fastest way to generate code with kimi-cli is through one-shot prompts passed directly as command arguments.

Basic Syntax

Execute a single generation request by wrapping your prompt in quotes:

kimi "Write a Python function that returns the nth Fibonacci number"

This command invokes the default "code" agent, which immediately forwards your text to the LLM through the KimiLLM.generate method (implemented in src/kimi_cli/llm.py, lines 66-102). The response streams directly to your terminal in real-time.

How the Generation Pipeline Works

When you submit a prompt, the CLI executes a specific call chain:

  1. Entry Point: Typer parses the command in src/kimi_cli/__main__.py and routes it through the lazy command group loader (src/kimi_cli/cli/_lazy_group.py).
  2. Application Bootstrap: KimiCLI.create (in src/kimi_cli/app.py) loads configuration and initializes the Agent based on YAML specifications.
  3. LLM Execution: The KimiLLM class abstracts provider-specific SDKs (via the kosong library) and streams tokens back through the generate method.

No additional tools are invoked for simple code generation; the LLM response prints directly to stdout.

Interactive Code Development with kimi shell

For multi-turn conversations or iterative refinement, use the interactive REPL mode.

Starting an Interactive Session

Launch the shell with:

kimi shell

Inside the REPL, you can chain requests to evolve code incrementally:


> Write a Rust struct representing a binary tree.
> Can you add a method to compute its depth?
> Show a unit test for the depth method.

Session Persistence and Context

Each line entered in the shell creates a separate turn in a Session object (defined in src/kimi_cli/session.py). The session preserves full conversation history in memory, enabling the LLM to reference previous code blocks when generating new content.

The CLI automatically generates a human-readable session title after your first turn. This logic resides in src/kimi_cli/web/api/sessions.py (lines 749-795), where the system analyzes the initial prompt to create descriptive metadata.

Persisting Generated Code to Disk

While one-shot generation prints to stdout, the interactive shell provides built-in tools to write files directly.

Using the File Write Tool

Invoke the file tool via slash commands:


> Write a Go program that prints "Hello, world!".
> /file:write path=hello.go content=<<CODE>>

The file:write tool implementation lives in src/kimi_cli/tools/file/write.py, accessed through the generic tool loader (src/kimi_cli/toolset.py). This tool receives the generated content and writes it to the specified path on your local filesystem without requiring manual copy-paste operations.

Configuring LLM Providers

kimi-cli supports multiple LLM providers through the kosong library, including OpenAI, Anthropic, and Gemini.

Environment Variable Configuration

Override the default LLM by setting the KIMI_LLMS environment variable:

KIMI_LLMS=openai kimi "Generate a Bash script that backs up /home"

Command-Line Provider Selection

Alternatively, pass the --provider flag during invocation:

kimi --provider=anthropic "Create a TypeScript interface for a user database"

Provider selection logic is handled in KimiLLM.__init__ (see src/kimi_cli/llm.py, lines 41-55), which initializes the appropriate client SDK based on your configuration.

Summary

  • kimi-cli provides both one-shot generation (kimi "prompt") and interactive REPL modes (kimi shell) for AI-assisted coding.
  • Code generation flows through KimiLLM.generate in src/kimi_cli/llm.py, streaming responses from any provider supported by the kosong library.
  • Interactive sessions maintain context using Session objects (src/kimi_cli/session.py) and auto-generate titles via logic in src/kimi_cli/web/api/sessions.py.
  • Use /file:write slash commands to persist generated code directly to disk through the tool system (src/kimi_cli/tools/file/write.py).
  • Configure alternative LLM providers using the KIMI_LLMS environment variable or --provider command-line flag.

Frequently Asked Questions

How does kimi-cli handle multi-file code generation?

The CLI handles multi-file generation through the interactive shell's tool system. While the LLM generates code in the conversation stream, you can use the /file:write command multiple times to create separate files. The agent specification in src/kimi_cli/agentspec.py defines which tools are available, and the tool loader in src/kimi_cli/toolset.py resolves these requests dynamically.

Can I use kimi-cli with my own OpenAI API key?

Yes. The KimiLLM class supports provider-specific authentication through environment variables or configuration files. Set KIMI_LLMS=openai and ensure your OPENAI_API_KEY is exported in your environment. The initialization logic in src/kimi_cli/llm.py (lines 41-55) handles the SDK client setup based on these parameters.

What is the difference between kimi and kimi shell commands?

The base kimi command executes one-shot code generation requests that print to stdout and exit immediately, using the default agent defined in the YAML specs under src/kimi_cli/agents/. The kimi shell command (registered via src/kimi_cli/cli/_lazy_group.py) starts a persistent REPL that maintains conversation history in a Session object, enabling iterative refinement and access to file system tools.

Where does kimi-cli store conversation history?

Conversation history is maintained in memory during the active session through the Session class (src/kimi_cli/session.py). The session tracks messages, metadata, and title generation flags. While the CLI does not automatically persist chat history to disk between invocations, the web API components in src/kimi_cli/web/api/sessions.py demonstrate how session data structures handle metadata storage for potential future retrieval.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →