Skip to content

Commit f08c49f

Browse files
committed
feat: add /context command to display context information
1 parent e056888 commit f08c49f

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Only write entries that are worth mentioning to users.
1111

1212
## Unreleased
1313

14+
- Shell: Add `/context` command to display context information (messages, tokens, checkpoints)
15+
1416
## 1.5 (2026-01-30)
1517

1618
- Web: Add Git diff status bar showing uncommitted changes in session working directory

docs/en/reference/slash-commands.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ This command is only available when using the default configuration file. If a c
6565

6666
Reload the configuration file without exiting Kimi Code CLI.
6767

68+
### `/context`
69+
70+
Display context information for the current session.
71+
72+
Output includes:
73+
- Total number of messages
74+
- Number of checkpoints
75+
- Token usage (showing current usage, maximum limit, and percentage)
76+
- Message counts by role (user, assistant, tool, etc.)
77+
78+
```
79+
Context Info:
80+
Total messages: 42
81+
Checkpoints: 3
82+
Token usage: 15,234 / 131,072 (11.6%)
83+
Messages by role:
84+
assistant: 18
85+
tool: 12
86+
user: 12
87+
```
88+
6889
### `/debug`
6990

7091
Display debug information for the current context, including:

docs/en/release-notes/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This page documents the changes in each Kimi Code CLI release.
44

55
## Unreleased
66

7+
- Shell: Add `/context` command to display context information (messages, tokens, checkpoints)
8+
79
## 1.5 (2026-01-30)
810

911
- Web: Add Git diff status bar showing uncommitted changes in session working directory

docs/zh/reference/slash-commands.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@
6565

6666
重新加载配置文件,无需退出 Kimi Code CLI。
6767

68+
### `/context`
69+
70+
显示当前会话的上下文信息。
71+
72+
输出内容包括:
73+
- 消息总数
74+
- 检查点数量
75+
- Token 使用量(显示当前使用量、最大限制和百分比)
76+
- 按角色分类的消息统计(user、assistant、tool 等)
77+
78+
```
79+
Context Info:
80+
Total messages: 42
81+
Checkpoints: 3
82+
Token usage: 15,234 / 131,072 (11.6%)
83+
Messages by role:
84+
assistant: 18
85+
tool: 12
86+
user: 12
87+
```
88+
6889
### `/debug`
6990

7091
显示当前上下文的调试信息,包括:

docs/zh/release-notes/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## 未发布
66

7+
- Shell:添加 `/context` 命令用于显示上下文信息(消息数、token 数、检查点数)
8+
79
## 1.5 (2026-01-30)
810

911
- Web:添加 Git diff 状态栏,显示会话工作目录中的未提交更改

src/kimi_cli/soul/slash.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,41 @@ async def yolo(soul: KimiSoul, args: str):
7878
else:
7979
soul.runtime.approval.set_yolo(True)
8080
wire_send(TextPart(text="You only live once! All actions will be auto-approved."))
81+
82+
83+
@registry.command
84+
async def context(soul: KimiSoul, args: str):
85+
"""Display context information (messages, tokens, checkpoints)"""
86+
ctx = soul.context
87+
history = ctx.history
88+
89+
if not history:
90+
wire_send(TextPart(text="Context is empty - no messages yet."))
91+
return
92+
93+
token_count = ctx.token_count
94+
lines = [
95+
"Context Info:",
96+
f" Total messages: {len(history)}\n",
97+
f" Checkpoints: {ctx.n_checkpoints}\n",
98+
]
99+
100+
# Add token usage with percentage if LLM is available
101+
if soul.runtime.llm is not None:
102+
max_context = soul.runtime.llm.max_context_size
103+
usage_percent = (token_count / max_context * 100) if max_context > 0 else 0
104+
lines.append(f" Token usage: {token_count:,} / {max_context:,} ({usage_percent:.1f}%)\n")
105+
else:
106+
lines.append(f" Token count: {token_count:,}\n")
107+
108+
# Count messages by role
109+
role_counts: dict[str, int] = {}
110+
for msg in history:
111+
role_counts[msg.role] = role_counts.get(msg.role, 0) + 1
112+
113+
if role_counts:
114+
lines.append(" Messages by role:")
115+
for role, count in sorted(role_counts.items()):
116+
lines.append(f" {role}: {count}")
117+
118+
wire_send(TextPart(text="\n".join(lines)))

0 commit comments

Comments
 (0)