📝 Tutorials
· 9 min read

Kimi Code CLI with K2.7: The Best Way to Use Moonshot's Coding Agent


Every model has a tool that brings out its best. For Claude, it’s Claude Code. For Kimi K2.7 Code, it’s Kimi Code CLI. This isn’t a generic wrapper — it’s Moonshot’s native agent framework, built from the ground up to exploit K2.7’s unique capabilities: preserve thinking, interleaved reasoning, and multi-step tool calling.

If you’ve been using K2.7 through Aider or the raw API and wondering if there’s a better way — there is. Let me show you why Kimi Code CLI + K2.7 is greater than the sum of its parts.

What Makes Kimi Code CLI Different

Before diving into setup, let’s understand what separates Kimi Code CLI from tools like Aider, Claude Code, or OpenCode:

Preserve Thinking (Forced)

Most coding tools treat each turn as somewhat independent. The model generates a response, the tool displays it, and for the next turn, the model has to reconstruct its understanding from the conversation history.

Kimi Code CLI forces preserve thinking. This means K2.7’s internal reasoning chain — its “thoughts” — are retained across every turn of the conversation. The model doesn’t just see your messages; it remembers why it made previous decisions.

In practice, this means:

  • Multi-file refactors are more coherent
  • The model catches its own mistakes across turns
  • Complex architectural changes maintain consistency
  • You spend less time re-explaining context

Interleaved Thinking + Tool Calling

Standard tool calling: model thinks → calls tool → receives result → thinks again → responds.

K2.7 via Kimi Code CLI: model thinks → calls tool → thinks about result while calling another tool → receives both results → reasons over everything → responds.

The interleaving means the model can plan multiple steps ahead and execute them in parallel where possible. A task that takes 5 round-trips in Aider might take 2 in Kimi Code CLI.

Multi-Step Tool Execution

K2.7 can chain multiple tool calls in a single turn. Read a file, analyze it, write a test, run the test, fix issues — all in one shot. This is native to the model architecture, not a framework hack.

Installation

Kimi Code CLI is available at kimi.com/code. Installation is straightforward:

macOS / Linux

curl -fsSL https://kimi.com/code/install.sh | bash

npm install -g @moonshot/kimi-code

pip install kimi-code

Verify Installation

kimi --version
kimi auth login

The auth login command opens your browser to authenticate with your Moonshot account. Once authenticated, your API credentials are stored locally.

Configuration

kimi config set model kimi-k2.7-code

kimi config set thinking.preserve true

kimi config set context.max-tokens 131072

Basic Usage

Starting a Session

cd your-project
kimi

kimi --files src/main.rs src/lib.rs

kimi "Add error handling to all database functions"

The Agent Loop

Once in a session, Kimi Code CLI operates as a full agent. You describe what you want, and it:

  1. Analyzes your project structure
  2. Reads relevant files
  3. Plans the changes (with visible reasoning)
  4. Implements changes across multiple files
  5. Optionally runs tests to verify
  6. Reports back with a summary
> kimi "Refactor the authentication module to use JWT instead of session tokens"

🤔 Thinking...
   - Need to identify current auth implementation
   - Check for session-based code in middleware, routes, and models
   - Plan migration to JWT without breaking existing tests

📁 Reading src/auth/middleware.ts...
📁 Reading src/auth/session.ts...
📁 Reading src/routes/login.ts...
📁 Reading tests/auth.test.ts...

🔧 Planning changes:
   1. Create src/auth/jwt.ts with token generation/verification
   2. Update middleware to validate JWT from Authorization header
   3. Modify login route to return JWT instead of setting session cookie
   4. Update tests to use JWT-based auth
   5. Remove session.ts (deprecated)

✏️ Writing src/auth/jwt.ts...
✏️ Modifying src/auth/middleware.ts...
✏️ Modifying src/routes/login.ts...
✏️ Modifying tests/auth.test.ts...
🗑️ Removing src/auth/session.ts...

✅ Running tests... 14/14 passed.

Done. Migrated authentication from sessions to JWT across 4 files.

Preserve Thinking in Action

Let me show you why preserve thinking matters with a concrete example. Imagine a multi-turn refactoring session:

Turn 1:

> "I want to split this monolithic Express app into a clean layered architecture"

K2.7 thinks: “The app has routes mixed with business logic and direct database calls. I’ll identify the layers: routes → controllers → services → repositories. Need to check the test structure too.”

Turn 2:

> "Start with the user module"

Without preserve thinking, the model would need to re-analyze the entire app. With it, K2.7 immediately recalls its architectural plan and jumps into splitting the user-related code into the identified layers.

Turn 3:

> "Now do the same for orders, but share the base repository pattern"

The model remembers the exact pattern it established for users — the repository interface, the service abstraction, the controller structure — and applies it consistently to orders. No drift, no inconsistency.

This is fundamentally different from tools that just pass conversation history. History gives the model what was said. Preserve thinking gives it what it was reasoning about.

Multi-Step Tool Calls

Here’s where K2.7 + Kimi Code CLI really shines compared to Aider or generic tools:

> "Find all API endpoints without rate limiting and add it"

In a single turn, K2.7 will:

  1. Search for route definitions across your codebase
  2. Identify which routes have rate limiting middleware
  3. Identify which routes are missing it
  4. Read the existing rate limiting implementation
  5. Apply rate limiting to all unprotected routes
  6. Run the test suite

This entire flow happens in one model turn — not six separate exchanges. The multi-step tool calling is native to K2.7’s architecture, and Kimi Code CLI exposes it fully.

Compare this to Aider, where each file read/write is a separate turn with a separate model call. For complex tasks, you’re looking at 3-5x fewer API calls with Kimi Code CLI.

Agent Workflows

Kimi Code CLI supports predefined workflows that chain multiple agent actions:

Code Review

kimi review --diff HEAD~3..HEAD

Reviews the last 3 commits, identifies issues, suggests fixes, and can apply them automatically.

Test Generation

kimi test src/services/payment.ts

Reads the implementation, understands the logic, generates comprehensive tests including edge cases.

Bug Investigation

kimi debug "TypeError: Cannot read property 'id' of undefined in OrderService.process"

Traces the error through the code, identifies the root cause, and proposes (or applies) a fix.

Documentation

kimi docs src/lib/

Generates or updates documentation for all exported functions and classes in a directory.

Configuration Deep Dive

Project-Level Config

Create .kimi/config.yaml in your project root:

model: kimi-k2.7-code
thinking:
  preserve: true
  visible: true  # Show reasoning in output

context:
  max-tokens: 131072
  include:
    - src/**/*.ts
    - tests/**/*.ts
  exclude:
    - node_modules/
    - dist/
    - "*.generated.ts"

tools:
  file-read: true
  file-write: true
  terminal: true
  search: true
  git: true

safety:
  auto-commit: false
  confirm-destructive: true
  test-before-commit: true

Customizing the Agent

system-prompt: |
  You are working on a TypeScript microservices project.
  We use NestJS, Prisma, and PostgreSQL.
  All code must have unit tests with >80% coverage.
  Follow our ADR conventions in docs/adr/.

rules:
  - Never modify migration files
  - Always run `npm test` before declaring a task complete
  - Use dependency injection, never import services directly

Why K2.7 + Kimi Code > K2.7 + Aider

I want to be specific about when Kimi Code CLI wins and when you might still prefer Aider:

Kimi Code CLI Wins

FeatureKimi Code CLIAider with K2.7
Preserve thinkingNative, forcedNot supported
Multi-step tool callsSingle turnMultiple turns
Interleaved reasoningYesNo
Token efficiency3-5x fewer calls for complex tasksStandard
Project understandingDeep, persistentPer-turn
Agent workflowsBuilt-inManual

Aider Still Wins

  • Git workflow: Aider’s auto-commit and git integration is more mature
  • Multi-model: Easily switch between models mid-session
  • Editor integration: Better editor/IDE connectivity
  • Community: Larger community, more docs, more troubleshooting resources
  • Model flexibility: Works with any OpenAI-compatible model equally well

My recommendation: use Kimi Code CLI for complex, multi-file tasks where reasoning coherence matters. Use Aider for quick edits, model comparisons, and when you need its git workflow.

For an even broader comparison of coding CLI tools, see our CLI tools comparison guide.

Using with Local K2.7

If you’re running K2.7 locally, you can point Kimi Code CLI at your local instance:

kimi config set api.base-url http://localhost:8000/v1
kimi config set api.key dummy

This gives you the full Kimi Code CLI experience with zero API costs and complete privacy. The preserve thinking and multi-step tool calling work identically with a local instance.

Tips for Maximum Productivity

  1. Be specific about scope: “Refactor the auth module” is better than “improve the code.” K2.7’s multi-step execution works best with clear objectives.

  2. Let it read first: Don’t pre-explain your codebase. Start with your task and let the agent explore. Its file reading + reasoning is usually faster than you describing the architecture.

  3. Use visible thinking: Keep thinking.visible: true in config. Watching the model reason helps you catch misunderstandings early.

  4. Trust the multi-step: Don’t break tasks into tiny pieces. Give it the full task and let multi-step tool calling handle the decomposition.

  5. Project config is your friend: A good .kimi/agent.yaml with your conventions saves countless corrections across sessions.

MCP Integration

Kimi Code CLI supports MCP (Model Context Protocol) for connecting to external tools and data sources. This means you can give K2.7 access to databases, APIs, documentation servers, and more through standardized tool definitions.

servers:
  - name: postgres
    command: mcp-server-postgres
    args: ["postgresql://localhost:5432/mydb"]
  - name: github
    command: mcp-server-github
    env:
      GITHUB_TOKEN: ${GITHUB_TOKEN}

With MCP, K2.7 can query your database, check GitHub issues, read documentation — all as part of its multi-step reasoning.

FAQ

How does Kimi Code CLI compare to Claude Code?

They’re philosophically similar — both are native agent frameworks for their respective models. Claude Code is optimized for Claude Sonnet/Opus, Kimi Code CLI is optimized for K2.7 Code. Key difference: Kimi Code CLI has forced preserve thinking and interleaved multi-step tool calls, which Claude Code doesn’t expose in the same way. Cost-wise, K2.7 is significantly cheaper. See our comparison guide for details.

Do I need a paid Moonshot account?

The free tier gives you limited API credits which are enough for testing. For daily development use, you’ll want a paid plan. Alternatively, self-host K2.7 and point Kimi Code CLI at your local instance for unlimited usage.

Can I use Kimi Code CLI with models other than K2.7?

Technically yes — it supports OpenAI-compatible endpoints. But the preserve thinking and interleaved tool calling features are specific to K2.7. With other models, Kimi Code CLI degrades to a standard agent framework without its signature advantages. You’re better off using Aider or OpenCode for non-K2.7 models.

What’s the difference between Kimi Code CLI and the Kimi CLI?

Kimi CLI is the general-purpose Moonshot command-line interface for chatting with Kimi models. Kimi Code CLI is the specialized agent framework for coding tasks — it includes file system tools, terminal access, git integration, and agentic workflows. Think of it as the difference between a chat window and a full IDE assistant. For more on the general CLI, see our Kimi CLI guide.

How much faster is multi-step tool calling versus Aider’s approach?

For simple single-file edits, there’s no meaningful difference. For complex tasks involving 5+ files — refactors, feature additions, bug investigations — K2.7’s multi-step execution typically completes in 2-3 model turns versus 8-12 in Aider. That translates to both faster wall-clock time (fewer round-trips) and lower cost (fewer total tokens processed). The savings compound on larger tasks.

Getting Started

If you’re sold on trying Kimi Code CLI:

  1. Install from kimi.com/code
  2. Authenticate with kimi auth login
  3. Navigate to a project and run kimi
  4. Start with a moderately complex task to see the multi-step reasoning in action

For the full picture on K2.7 Code’s capabilities, read our complete K2.7 Code guide. And if you’re migrating from K2.6 workflows, our K2.6 guide has comparison notes.