Kimi Code Documentation
repository·main·Indexed 25 days ago
https://github.com/moonshotai/kimi-codeAn AI coding agent available as a CLI, VS Code extension, and web client. Kimi Code can read/edit code, execute shell commands, and search files. It supports the Model Context Protocol (MCP) for extending capabilities and provides a unified agent engine via @moonshot-ai/agent-core, including a dependency injection container and an ACP adapter for exposing agent sessions via the Agent Client Protocol.
What's inside Kimi Code
- @moonshot-ai/kaos is an execution environment abstraction used by Kimi Code. It provides a layer to abstract the underlying environment in which code or tasks are executed.
Overview of @moonshot-ai/agent-core Dependency Injection Container
mainThe
@moonshot-ai/agent-coreDI container is a VSCode-style dependency injection system designed for the agent-core/server stack. It provides service identifiers, registration, resolution, and lifecycle management with zero runtime dependencies.Key features include:
- Service Identifiers: Branded callable values created via
createDecorator<T>(). - Resolution:
InstantiationServicehandles singleton-per-container semantics, scoped child containers viacreateChild, and automatic constructor-parameter injection. - Delayed Instantiation: Services can be lazily materialized behind a
ProxyusingSyncDescriptor. - Cycle Detection: Uses both graph-based walks and construction stack tracking to prevent cyclic dependencies.
- Testing Support: A specialized
TestInstantiationServiceis available via the@moonshot-ai/agent-core/di/testsubpath for easy stubbing and resolution.
- Service Identifiers: Branded callable values created via
Overview of Kimi Code Built-in Tools
mainKimi Code CLI provides a set of built-in tools managed directly by the runtime. These tools do not require MCP server installation and are automatically selected by the Agent.
Approval Mechanism:
- Read-only tools (e.g.,
Read,Grep,Glob) are auto-allowed by default. - Write and execution tools (e.g.,
Write,Edit,Bash) require user approval by default. - In YOLO mode, approval for regular tool calls is skipped.
- Plan mode exit approval is always required regardless of mode.
- Read-only tools (e.g.,
Overview of @moonshot-ai/agent-core
main@moonshot-ai/agent-core is the unified agent engine for Kimi Code. It serves as a core component within the Kimi Code monorepo, providing the underlying engine capabilities for agentic workflows.Overview of @moonshot-ai/kosong
main@moonshot-ai/kosong is an LLM (Large Language Model) abstraction layer used by the Kimi Code ecosystem. It provides a unified interface for interacting with different language models, allowing the Kimi Code components to remain model-agnostic.Understand the Unified Read-Write Model Design for agent-core-v2
mainThe
agent-core-v2unified read-write model is designed to provide a single source of truth for views, topics, write operations, and subscriptions. It aims to resolve issues like event visibility confusion, inconsistent definition methods, and feedback loops.Key Design Constraints:
- Persistence is frozen: The design does not change the physical storage layout.
wire.jsonlpaths, the 'one physical log file per agent' rule, and thePersistedWireRecorddata structure remain unchanged. - API-level unification: Unification occurs only at the in-process API level, specifically for write entry points, read models, subscriptions, phases, and the type registry.
- Compatibility: The design maintains existing
protocol_versionand migration chain (1.0→1.5) mechanisms and does not introduce new log format migrations.
- Persistence is frozen: The design does not change the physical storage layout.
Understand the Kimi Code CLI Operating Environment
mainThe Kimi Code CLI operates in a non-sandboxed environment on the user's machine. Actions taken by the agent (such as file reads, writes, or command executions) immediately affect the host system.
Key Environment Variables & Context:
{{ KIMI_OS }}: The operating system being used.{{ KIMI_SHELL }}: The shell used for executingBashtool commands.{{ KIMI_WORK_DIR }}: The current working directory, which serves as the project root. Use absolute paths for tool parameters when required.{{ KIMI_NOW }}: The ISO format date/time captured at session start. Note that this value is static and may become stale; for time-sensitive tasks, use thedatecommand via a shell tool to get the current time.
Use Kimi Code features and slash commands
mainKimi Code provides several capabilities for autonomous coding and context management:
- Autonomous Actions: Explores codebases, reads/writes code, and runs terminal commands (requires permission).
- Thinking Controls: Toggle reasoning or select specific model-supported thinking effort levels.
- Provider-aware Models: Select specific models across different configured providers even if they share the same name.
- Native Editor Integration: Review AI changes directly using the VS Code diff viewer.
- MCP Support: Extend capabilities using Model Context Protocol servers.
- Slash Commands:
/init: Analyzes your current project./compact: Manages and optimizes the current context.
Understand the Kimi Code Agent System Instructions
mainThe Kimi Code agent is an interactive AI agent designed for software engineering tasks. It operates directly on the user's system (not in a sandbox) and uses tools to perform actions like reading files, searching code, and executing commands.
Key Operational Principles:
- Task-Oriented: If a request can be interpreted as a task (e.g., "change
methodNameto snake_case"), the agent defaults to using tools to perform the change rather than just describing it. - Language Consistency: The agent communicates in the user's language but maintains original forms for code, commands, identifiers, and file paths. Technical artifacts (comments, commits, PRs) follow project conventions.
- Tool Preference: The agent prefers dedicated tools (
Read,Glob,Grep) over raw shell commands to respect workspace access policies and manage output size. - Parallelism: For efficiency, the agent is encouraged to issue multiple non-interfering tool calls (like
Read,Grep, andGlob) in parallel, especially during investigation phases.
- Task-Oriented: If a request can be interpreted as a task (e.g., "change
Understand the Kimi Code Unified Read/Write Model
mainThe system is built on a unified model consisting of 5 Primitives, 1 Stream Structure, and 1 State Machine. This design separates decision-making (Commands) from state (Views) and persistent history (Facts).
The 5 Primitives
Primitive Definition Purpose Fact A persistent, replayable event that has occurred. "What changed the state" Command A stateless decision-making unit that produces 0..n Facts. "Who decided to change something" View A pure function that folds the Fact stream into a state. "What the current state is" Signal A transient, non-persistent, typed event. "Current progress/volatile status" Effect A strategy that reacts to Fact/View changes via Commands. "What happens after a fact occurs" Hook An interceptor for write operations. "Intercepting/vetoing an operation" Core Concepts
- Facts are committed to the stream and are essential for resuming state.
- Commands are never replayed; they only run in the
livephase. - Views are the sole carriers of state, derived by folding Facts.
- Signals are volatile and do not participate in state folding.
Understand the stateless agent loop architecture
mainThe
looppackage provides a stateless agent loop. It is designed to be decoupled from host-layer responsibilities. It does not manage:- Sessions
- Wire transport
- Compaction execution
- Permissions UI
- Durable protocol bridging
These responsibilities must be handled by the host layer implementing the loop.
Understand the VS Code Node SDK Migration (v0.6.0+)
mainStarting with version
0.6.0, the Kimi Code VS Code extension migrated from a Python-based CLI architecture to an in-process Node SDK architecture using@moonshot-ai/kimi-code-sdk.Key Changes:
- Runtime: Replaced the Python/stdio host with the TypeScript v1 engine running directly in the VS Code Extension Host.
- Identity: The extension identifies itself with the User-Agent
kimi-code-vscode/[version]. - Data Sharing: The extension and the TUI (Terminal User Interface) now share the same configuration, authentication, MCP configuration, and sessions, provided they resolve to the same Kimi Code home directory.
- Environment Variables: The legacy
kimi.environmentVariablessetting has been removed. Provider-specific variables must now be placed inconfig.toml, and MCP server variables inmcp.json.