What is Kimi Code? A Deep Dive into the Terminal AI Coding Agent Architecture

Kimi Code is a next-generation AI coding agent that runs directly in your terminal, capable of reading and editing source files, executing shell commands, searching the filesystem, and making autonomous decisions through a language model interface. Built as a TypeScript monorepo by MoonshotAI, it combines a terminal UI with a dependency-injection-driven agent engine and a public SDK for IDE integration.

Core Architecture of Kimi Code

Kimi Code follows a layered architecture with clear separation between user interface, agent logic, and runtime services. Understanding these layers is essential for both users and developers extending the platform.

The Five Architectural Layers

Layer Purpose Key Source Path
CLI / TUI Interactive terminal entry point that drives sessions, renders transcripts, and forwards input to the agent engine apps/kimi-code/README.md
Unified Agent Engine (v1) Original core providing sessions, scopes, services, and DI container packages/agent-core/README.md
Agent Engine v2 Refactored DI × Scope architecture powering kap-server and the SDK with four lifecycle scopes and unit-based features packages/agent-core-v2/AGENTS.md
Public TypeScript SDK Contract-driven façade (@moonshot-ai/kimi-code-sdk) exposing the engine over RPC for external processes packages/node-sdk/README.md
Runtime Support Packages Utilities for telemetry, OAuth, filesystem abstraction (kosong), MiniDb persistence, and pluggable skills Various packages/* directories

The v2 engine represents a significant architectural evolution, introducing four nested lifecycle scopes: App → Workspace → Session → Agent. Each scope inherits services from its parent while maintaining isolation appropriate to its level.

How Kimi Code Executes: A Typical Workflow

When you launch Kimi Code, the following sequence occurs according to the source implementation in packages/agent-core-v2/AGENTS.md:

  1. Start the CLI (kimi) → the TUI creates a Session.
  2. The Session runs inside the Session scope, inheriting services from Workspace and App scopes.
  3. The Agent Engine v2 resolves requested sub-agents (e.g., coder, explore, plan) as Feature units. Each sub-agent operates in its own Agent scope, isolated from the main conversation transcript.
  4. The engine communicates with a Model Context Protocol (MCP) server to generate actions—shell commands, file edits, or API calls.
  5. Results stream back to the TUI where you approve, reject, or modify them. Lifecycle hooks enable custom scripts at key points (e.g., pre-tool-call).

This scope isolation enables parallel sub-agents working on separate tasks without transcript contamination—a critical capability for complex multi-file operations.

Key Design Principles

Dependency Injection (DI) Driven Extensibility

The Kimi Code engine is fully DI-driven. Adding capabilities requires only registering a contribution token; the engine auto-wires it into the appropriate scope. This design powers:

  • Parallel sub-agents with transcript isolation
  • Fine-grained persistence via MiniDb for session indexing and queries
  • Extensible plugin ecosystem installable from marketplace or GitHub repos

Model Context Protocol (MCP) Integration

Kimi Code communicates with LLM providers through MCP, a standardized protocol for model context exchange. Configure MCP servers via the TUI:

/mcp-config add

Configuration persists in the Workspace scope, available to all sessions launched from that workspace. Source: apps/kimi-code/README.md

Practical Usage Examples

Launching the Terminal UI


# From any project directory

cd your-project
kimi                    # launches the interactive terminal UI

Source: apps/kimi-code/README.md

Programmatic Control via Node.js SDK

The @moonshot-ai/kimi-code-sdk package exposes Kimi Code functionality over RPC for automation and IDE integration:

import { createClient } from '@moonshot-ai/kimi-code-sdk';

const client = createClient({ url: 'http://localhost:8080' });

async function analyzeRepo() {
  const session = await client.session.create();
  const result = await session.ask('Explain the main directories of this repo.');
  console.log(result.message);
}

analyzeRepo().catch(console.error);

Source: packages/node-sdk/README.md

Creating Custom Skills

Register new capabilities as TypeScript plugins using the v2 API:

// my-skill.ts
import { registerSkill } from '@moonshot-ai/agent-core-v2';

registerSkill('time', async () => {
  return `The current time is ${new Date().toLocaleTimeString()}`;
});

Install via the Kimi UI after publishing:

/kinstall my-skill

Source: packages/agent-core-v2/AGENTS.md

Critical Source Files and Their Roles

Path Purpose
apps/kimi-code/README.md CLI/TUI entry point documentation
packages/agent-core/README.md Original unified agent engine overview
packages/agent-core-v2/AGENTS.md DI × Scope engine design: scopes, units, contribution points
packages/node-sdk/README.md Public TypeScript SDK usage
packages/kap-server/ REST/WebSocket server exposing engine at /api/v1
packages/telemetry/ Analytics and debugging telemetry
packages/oauth/ MoonshotAI service authentication
packages/kosong/ Provider-agnostic LLM abstraction layer
packages/minidb/ JSON document store for session indexing

Summary

  • Kimi Code is a terminal-native AI coding agent from MoonshotAI with autonomous file editing, shell execution, and decision-making capabilities.
  • Architecture follows a TypeScript monorepo structure with CLI/TUI, dual agent engine versions, public SDK, and runtime utility packages.
  • Agent Engine v2 introduces DI × Scope architecture with four lifecycle levels (App → Workspace → Session → Agent) enabling isolated parallel sub-agents.
  • Extensibility is DI-driven: register contribution tokens to add skills, tools, or configuration without modifying core code.
  • Integration occurs via MCP protocol configuration and the @moonshot-ai/kimi-code-sdk for programmatic control.

Frequently Asked Questions

What programming language is Kimi Code built with?

Kimi Code is implemented in TypeScript as a monorepo structure. All core components—including the CLI, agent engines, SDK, and supporting utilities—are TypeScript packages under apps/ and packages/ directories.

Can Kimi Code integrate with my IDE instead of the terminal?

Yes. Kimi Code exposes its functionality through kap-server (REST/WebSocket at /api/v1) and the @moonshot-ai/kimi-code-sdk package. Any IDE or external process can drive Kimi Code sessions via these RPC interfaces, as documented in packages/node-sdk/README.md and packages/kap-server/.

How does Kimi Code handle multiple concurrent tasks?

Through sub-agents running in isolated Agent scopes. Each sub-agent (e.g., coder, explore, plan) operates within its own scope nested under the Session scope, preventing transcript contamination while enabling parallel execution. The DI × Scope architecture in Agent Engine v2 manages this isolation automatically.

What is the difference between Agent Engine v1 and v2?

v1 (packages/agent-core/) provides the original unified engine with sessions, scopes, and DI container. v2 (packages/agent-core-v2/) refactors this into a more sophisticated DI × Scope architecture with four explicit lifecycle scopes, unit-based features, and direct support for the server (kap-server) and SDK implementations. New development should target v2 APIs.

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 →