context-mode
Installation

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.

  • PreToolUse
  • PostToolUse
  • SessionStart
  • SessionEnd
  • PreCompact
  • UserPromptSubmit
  • Stop

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, and additionalContext.
  • Session-context injection is also dropped: context-mode emits additionalContext on SessionStart and PostToolUse, 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.

Check your runtime
node --version

Install the package

This installs the hook dispatcher, the MCP server, and the context-mode command line.

Global install
npm install -g context-mode

Register 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.

~/.kimi-code/mcp.json
{
  "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>.

~/.kimi-code/config.toml
[[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 = 30

Restart 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.

Per-project routing rules
cp "$(npm root -g)/context-mode/configs/codex/AGENTS.md" ./AGENTS.md

For global use, copy it into the Kimi Code config directory instead:

Global routing rules
CM_ROOT="$(npm root -g)/context-mode"; cp "$CM_ROOT/configs/codex/AGENTS.md" ~/.kimi-code/AGENTS.md

How detection works

context-mode identifies Kimi Code from its config directory and resolves everything relative to that root.

PathRole
~/.kimi-code/Config directory that identifies Kimi Code
~/.kimi-code/context-mode/sessions/Where captured sessions are stored
~/.kimi-code/config.tomlHooks
~/.kimi-code/mcp.jsonMCP 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 stats in 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 doctor in your terminal to confirm the hooks and MCP server are registered. The Kimi-specific checks cover:

    • the kimi binary on your PATH
    • each [[hooks]] entry in ~/.kimi-code/config.toml
    • the context-mode entry in ~/.kimi-code/mcp.json

    The same run also reports the global checks: language runtimes and the FTS5 knowledge base.

Verify the setup
context-mode doctor

A 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:

Test one hook directly
echo '{}' | context-mode hook kimi pretooluse

A 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.

On this page