Kimi Code
Install context-mode in the Kimi Code CLI through its full TOML hook surface and MCP server.
The Kimi Code CLI exposes a full hook surface, so context-mode integrates through TOML-declared hooks plus an MCP server. Together they keep the bulk of raw payload bytes out of your window by routing data-heavy output into the sandbox and answering from the FTS5 knowledge base.
context-mode registers seven hooks on Kimi Code. The wire protocol is JSON stdin/stdout, the same one Codex CLI uses.
PreToolUsePostToolUseSessionStartSessionEndPreCompactUserPromptSubmitStop
SessionEnd is distinctive on Kimi Code. It is a genuine session-close event,
separate from Stop, so context-mode can finalize and index a session when it
actually ends rather than at every turn boundary.
On Kimi Code, PreToolUse is deny-only, so routing enforcement relies on deny:
- Works:
permissionDecision: "deny"(or exit code 2) blocks a tool. - Silently dropped by the host runner:
permissionDecision: "ask", argument rewrites, andadditionalContext. - Session-context injection is also dropped: context-mode emits
additionalContextonSessionStartandPostToolUse, but Kimi's runner does not surface it to the model.
Install
Kimi Code has no plugin marketplace, so install the package globally first, then
declare the hooks and register the server. Hooks live in
~/.kimi-code/config.toml (TOML) and the MCP server is registered in
~/.kimi-code/mcp.json (JSON).
Check the prerequisites
Install Node 22.5 or newer, or Bun. The sandbox and FTS5 knowledge base depend on a current runtime.
node --versionInstall the package
This installs the hook dispatcher, the MCP server, and the context-mode
command line.
npm install -g context-modeRegister the MCP server
Register the context-mode server in ~/.kimi-code/mcp.json, which is JSON. This
backs the ctx_* tools the agent calls to run code and search the knowledge
base. The bare binary with no arguments starts the MCP server.
{
"mcpServers": {
"context-mode": {
"command": "context-mode",
"args": []
}
}
}Add the hooks
The hook config lives in ~/.kimi-code/config.toml and is TOML. Hooks are
declared as a TOML array of tables, so add one [[hooks]] entry per event. Each
one invokes the context-mode dispatcher, which reads the event on stdin and
returns its decision on stdout. The dispatcher takes the platform first, then a
lowercase event name: context-mode hook kimi <event>.
[[hooks]]
event = "PreToolUse"
matcher = "Bash|Shell|Read|Edit|Write|WebFetch|Agent|ctx_execute|ctx_execute_file|ctx_batch_execute|ctx_fetch_and_index|ctx_search|ctx_index|mcp__"
command = "context-mode hook kimi pretooluse"
timeout = 30
[[hooks]]
event = "PostToolUse"
command = "context-mode hook kimi posttooluse"
timeout = 30
[[hooks]]
event = "SessionStart"
command = "context-mode hook kimi sessionstart"
timeout = 30
[[hooks]]
event = "SessionEnd"
command = "context-mode hook kimi sessionend"
timeout = 30
[[hooks]]
event = "PreCompact"
command = "context-mode hook kimi precompact"
timeout = 30
[[hooks]]
event = "UserPromptSubmit"
command = "context-mode hook kimi userpromptsubmit"
timeout = 30
[[hooks]]
event = "Stop"
command = "context-mode hook kimi stop"
timeout = 30Restart Kimi Code after editing both files so the hooks and the server load.
Copy the routing instructions (optional)
context-mode reads project routing rules from AGENTS.md. Copy the shipped file
so the model knows to send data-heavy work into the sandbox.
cp "$(npm root -g)/context-mode/configs/codex/AGENTS.md" ./AGENTS.mdFor global use, copy it into the Kimi Code config directory instead:
CM_ROOT="$(npm root -g)/context-mode"; cp "$CM_ROOT/configs/codex/AGENTS.md" ~/.kimi-code/AGENTS.mdHow detection works
context-mode identifies Kimi Code from its config directory and resolves everything relative to that root.
| Path | Role |
|---|---|
~/.kimi-code/ | Config directory that identifies Kimi Code |
~/.kimi-code/context-mode/sessions/ | Where captured sessions are stored |
~/.kimi-code/config.toml | Hooks |
~/.kimi-code/mcp.json | MCP server registration |
Set KIMI_CODE_HOME to relocate the config directory; context-mode then
resolves the hooks, mcp.json, and session storage from that root instead of
~/.kimi-code/.
Verify your savings
Confirm context-mode is actually saving you tokens, not just installed:
-
In a Kimi Code session, type
ctx statsin chat to see the tokens saved and the context-window savings ratio for the session, plus the per-tool breakdown. Run a few data-heavy commands first so the numbers reflect real routing. -
Run
context-mode doctorin your terminal to confirm the hooks and MCP server are registered. The Kimi-specific checks cover:- the
kimibinary on yourPATH - each
[[hooks]]entry in~/.kimi-code/config.toml - the
context-modeentry in~/.kimi-code/mcp.json
The same run also reports the global checks: language runtimes and the FTS5 knowledge base.
- the
context-mode doctorA missing PreCompact entry is a warning, not a failure — it requires a Kimi
build that emits PreCompact, so don't treat that warning as a broken install.
To isolate a single hook, pipe a JSON event into the dispatcher and inspect what it writes to stdout:
echo '{}' | context-mode hook kimi pretooluseA clean doctor run plus a climbing ctx stats savings ratio means the hooks are
registered and context-mode is routing your data-heavy commands into the sandbox.