Ported from claude-context-optimizer · MIT · local only
63%

Stop burning context.
Keep your tokens for the work.

KCO hooks into Kimi Code CLI to block redundant reads, guard your real context budget straight from the wire transcript, and coach weak prompts — before they cost you.

26 modules · 0 runtime deps · 77 tests · benchmarked across 7 scenarios

/plugins install https://github.com/egorfedorov/kimi-context-optimizer
kimi — with kimi-context-optimizer

The problem

Your context window is leaking.

In a typical agentic session, 30–40% of context is waste. Every turn resends the whole conversation — so unread, duplicated, and useless bytes are taxed on every single step.

30–40%

of a typical session's context is waste: files read twice, lockfiles opened in full, dead-end exploration.

2,000 lines

slurped to change one function. The model needed 50 — you paid for all of them, on every subsequent turn.

Every turn

the entire context goes back over the wire. Waste doesn't cost once — it compounds until you compact.

2× the price

once in tokens, once in quality: useful context gets crowded out and the model loses the plot.

How it works

Three steps, zero config.

KCO registers hooks in the Kimi Code lifecycle. They fire before the cost is incurred — not after.

How KCO works: hooks intercept tool calls, cache and track against wire.jsonl ground truth, and save 63% of tokens

Features

Fifteen hook rules, quietly working.

Automatic from the moment you install. Each one exists to stop a specific way sessions waste context.

read-cache

A second full read of the same file gets blocked and replaced with a compact structural file map — landmarks and line numbers, not 3,900 tokens again.

📊

tracker

Every tool call, file touch, and token figure is recorded per session, so reports, digests, and per-task costs are real — not vibes.

📈

budget monitor

Live real context % read from wire.jsonl ground truth. Warns at 50/70/85/95%, nudges /compact at 90%, warns about context rot near ~350K on 1M windows.

🎓

prompt-coach

Grades every prompt S–F (English + Russian) before it runs and suggests the fix — so tasks land on the first try instead of the fourth.

🛡️

context-shield

Warns before you read files that were waste in 3+ past sessions, and turns chronic waste into permanent .contextignore rules.

💥

failure & delegation tracking

PostToolUseFailure events counted as wasted turns; SubagentStart/Stop attributing what delegated work actually costs.

The /kco control center: real context percentage, cache hit rate, blocked reads and tokens saved

Why the Kimi version is better

Same idea. Honester numbers.

KCO is a port of claude-context-optimizer — but the Kimi Code surface exposes ground truth the Claude version could only estimate.

Claude versionKCO (Kimi Code)
Context sizeEstimated from tool payloads Real per-step token count from wire.jsonl; estimates only as self-calibrating fallback
Cache economicsAnthropic pricing math, modeled Measured inputCacheRead / inputCacheCreation per step — hit rate and cache breaks, not guesses
Failed tool callsNo failure events PostToolUseFailure tracked as wasted turns on the dashboard
Sub-agentsPPID guessing SubagentStart/Stop events attribute delegation cost precisely
Context windowHardcoded 200K assumption Read from your config.toml model — 256K on K2.7, 1M on K3 — plus a context-rot warning at ~350K
MetricsDollar-first billing math Tokens + % of your window first; $ only if you configure real prices — subscription-honest
Memory file AGENTS.md analysis for token bloat, with concrete trims

Commands

19 slash commands. One control center.

Thin wrappers over the hook data — every number comes from what your sessions actually did.

/kcoControl Center — budget, savings, waste, prompt grade, next actions
/kco-reportFull token ROI report across all sessions + file suggestions
/kco-roiMonthly savings projection — tokens-first, $ if priced
/kco-digestEfficiency digest with S–F grade and trends
/kco-exportExport the report as Markdown or an HTML dashboard
/kco-cleanDelete old tracking data / reset stats
/kco-doctorHealth check: versions, hooks, data dir, config, Node
/kco-replaySummaries of your last N sessions for quick context recovery
/kco-taskPer-task token attribution — see what each task cost
/kco-packBuild a minimal, ranked context pack (files + offset/limit) for a task
/kco-templatesSave and re-apply named context sets for recurring tasks
/kco-gitSuggest files to read from current git state + history
/kco-anatomyOne-file project map: every file with size, tokens, category
/kco-shieldWaste-protection status; grow .contextignore from real waste
/kco-agentsmdAudit AGENTS.md for token bloat, get concrete trims
/kco-coachGrade a prompt (or recent ones) and offer a rewrite
/kco-overheadAudit fixed per-session overhead from the real wire transcript
/kco-smart-loaderAuto-suggests files to preload when you describe a new task
/kco-budgetSet budget limits, auto-compact, optional pricing; window-aware

Install

Running in under a minute.

Requires Node.js ≥ 18 on your PATH. No accounts, no API keys, no config to get started.

1

Install the plugin

From Kimi Code CLI, point at a local path or a git URL.

/plugins install https://github.com/egorfedorov/kimi-context-optimizer
2

Reload

Hooks and skills register on reload. Updating later = reinstall + reload again.

/reload
3

Verify

Checks Node version, hook wiring, data dir, and config in one go.

/kco-doctor
// ~/.kimi-context-optimizer/config.json — all optional { "budgetTokens": 200000, // caps to your model's real window "warnAt": [50, 70, 85, 95], "autoCompactAt": 90, "pricePerMillionInput": null, // set real prices to see $ "pricePerMillionOutput": null, "quiet": false }
Local firstEverything lives under ~/.kimi-context-optimizer/ — sessions, patterns, config.
No telemetryNo network calls, nothing leaves your machine. /kco-clean --reset-all wipes it all.
Zero dependencies25 plain Node ≥18 ESM modules. Nothing to audit, nothing to break.
MIT licensedPorted from claude-context-optimizer with love. Fork it, ship it, own it.