Skip to content

feat(cli): add /context slash command with context usage breakdown - #2306

Open
bowenliang123 wants to merge 3 commits into
MoonshotAI:mainfrom
bowenliang123:feat/context-command
Open

feat(cli): add /context slash command with context usage breakdown#2306
bowenliang123 wants to merge 3 commits into
MoonshotAI:mainfrom
bowenliang123:feat/context-command

Conversation

@bowenliang123

@bowenliang123 bowenliang123 commented Jul 28, 2026

Copy link
Copy Markdown

Related Issue

No linked issue — the problem is explained below.

Problem

Mainstream coding agent tools like Claude Code already ship a /context command that shows users what their context window is made of; Kimi Code does not.

/usage only shows the total contextTokens / maxContextTokens, so users cannot tell how much of that is the system prompt, tool schemas, MCP tools, memory files (AGENTS.md), skills, or the conversation itself — information that matters when deciding what to trim (e.g. disabling an MCP server vs. compacting the conversation). This PR adds the equivalent.

What changed

Adds a /context slash command that renders a Claude Code-style context usage panel: a block-grid visualization, the model + window summary, the estimated token cost per category (system prompt, system tools, MCP tools, memory files, skills, messages, free space), plus per-entry token detail for MCP servers, memory files, and skills.

  • agent-core: a new getContextBreakdown RPC on the agent API surface. It re-gathers the system-prompt context (AGENTS.md chain, skill listing) and attributes the rendered system prompt between the base template and those injected sections; tool schemas are estimated with the same character heuristic the compaction budget already uses (estimateTokens), split into builtin/user vs MCP buckets. Deferred tools under progressive disclosure are skipped so their schemas are not double-counted with the message history.
  • Attribution is honest about what lands in the request:
    • Messages is the residual: contextTokens (the status-bar total) is the last LLM-reported usage, which already covers system prompt + tool schemas + the last turn's output, so subtracting the estimated categories avoids double-counting and keeps the panel consistent with the header total. Before the first LLM round-trip the reported total is still 0 while the system overhead is real, so the header, grid, and free space use an effective total (max(reported, category sum)) — otherwise the panel would show non-zero categories against "100% free".
    • MCP tools are attributed per server; memory files per AGENTS.md file (the loader now returns its per-file split); skills per skill, using exactly the model listing's selection (invocable, non-sub-skills — so sub-skills and non-model-invocable skills correctly do not appear, unlike /skills).
  • node-sdk: exposes Session.getContextBreakdown().
  • TUI: /context dispatch + a context-panel report component, rendered in the same bordered panel as /usage and /status. Values are estimates, which the panel states ("Estimated usage by category"); per-entry numbers are marked with ~.
  • Docs: /context row in the slash-command reference (en + zh).

Sample output (colors stripped):

image
  ╭ Context ───────────────────────────────────────╮
  │ Context Usage                                  │
  │   ⛁ ⛁ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶   kimi-code/k3           │
  │   ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶   43.6k/1M tokens (4.3%) │
  │   ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶                          │
  │   ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶ ⛶                          │
  │                                                │
  │   Estimated usage by category                  │
  │   ⛁ System prompt: 5.4k tokens (0.5%)          │
  │   ⛁ System tools: 17.1k tokens (1.7%)          │
  │   ⛁ MCP tools: 5.2k tokens (0.5%) · 30 tools   │
  │   ⛁ Memory files: 5.2k tokens (0.5%)           │
  │   ⛁ Skills: 1.8k tokens (0.2%)                 │
  │   ⛁ Messages: 10.8k tokens (1.1%)              │
  │   ⛶ Free space: 980k tokens (95.7%)            │
  │                                                │
  │ MCP servers · /mcp                             │
  │   ├ ● chrome-devtools (29 tools) ~5k tokens    │
  │   └ ● codegraph (1 tool) ~200 tokens           │
  │                                                │
  │ Memory files                                   │
  │   ├ ~/.kimi-code/AGENTS.md ~4.2k tokens        │
  │   └ ~/dev/kimi-code/AGENTS.md ~1000 tokens     │
  │                                                │
  │ Skills · /skills                               │
  │   ├ agent-core-dev [project] ~120 tokens       │
  │   ├ code-review [user] ~140 tokens             │
  │   ├ diagnosing-bugs [user] ~60 tokens          │
  │   ├ tdd [user] ~20 tokens                      │
  │   └ write-tui [project] ~180 tokens            │
  ╰────────────────────────────────────────────────╯

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 95e3d71

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e1cb768b05

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core/src/agent/index.ts Outdated
- Messages is now the residual (contextTokens already includes system
  prompt, tool schemas, and the last turn's output)
- MCP tools are attributed per server, skills per skill (matching the
  model listing selection exactly), memory files per AGENTS.md file
…trip

contextTokens is 0 until the first LLM response, so the panel showed
non-zero category rows against a 100% free space. The header, grid, and
free space now use an effective total (max of the reported total and the
estimated category sum), and the messages residual follows it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant