[BUG] `/context` do not respect `--system-prompt` flag

Status Closed — not planned
Maintainer reply None cached
Activity 9 comments · opened Nov 3, 2025 · closed Jan 25, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

Description

When I run claude --system-prompt "Answer with ALL CAPS" the /context command shows that System Prompt is still usual 2.4K token.

What Should Happen?

/context should show actual size when --system-prompt is used.

Steps to Reproduce

  1. Run claude --system-prompt "Answer with ALL CAPS"
  2. Run /context in TUI

Claude Model

Sonnet (default)

Is this a regression?

I don't know

Claude Code Version

2.0.35

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

View original on GitHub ↗

9 Comments

github-actions[bot] · 11 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/9733
  2. https://github.com/anthropics/claude-code/issues/4523
  3. https://github.com/anthropics/claude-code/issues/6537

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

Elijas · 11 months ago

same issue - /context are broken when using --append-system-prompt

Elijas · 11 months ago

Repro

Without --append-system-prompt it shows correct counts

<img width="945" height="460" alt="Image" src="https://github.com/user-attachments/assets/088d9114-b8d5-40fb-bb13-764bedb18483" />

✅ Correct:62k (total from /context) minus buffer 45k equals the real token count 18011 (from bottom right)

After adding ~16k context with --append-system-prompt it shows wrong counts

<img width="945" height="460" alt="Image" src="https://github.com/user-attachments/assets/328da78b-3e15-4578-81b3-70fbcbdc60af" />

❌Wrong: 64k (total from /context) minus buffer 45k means /context thinks current token usage is ~20k

while the actual usage is 36k (as seen in bottom right)

this proves that the 16k appended system prompt context was not included in the /context count

36k (correct) - 20k (incorrect) = 16k (appended system context) as expected

alex-on-java · 10 months ago

@bcherny I don't know whether this will be useful, but I asked Claude Code to fix system prompt calculation for /context slash command in the minified source code of Claude Code. Here is its report:

---

Fix: /context Command Ignoring --system-prompt Flag

Note: Variable/function names in this document are human-readable approximations inferred from context. The actual decompiled code uses minified names like l3A, Mg2, xs5, etc.

The Bug

When using --system-prompt "custom text", the /context command showed 2.8k tokens (the default system prompt size) instead of the actual custom prompt's token count. The API correctly used the custom prompt, but /context always calculated tokens for the default prompt.

Root Cause

The --system-prompt value lived only in the interactive session component's closure, inaccessible to slash commands:

Before:

CLI flags ──> InteractiveSessionComponent ──> API calls ✓
              (systemPromptProp)               (uses custom prompt)
                   │
                   X (blocked)
                   │
            /context command ──> calculateContextMetrics()
                                      ↓
                                 calculateSystemPromptTokens()
                                      ↓
                                 generateDefaultSystemPrompt()
                                 (always uses default)

Slash commands accessed app state, but systemPrompt wasn't stored there.

Solution

Thread the custom prompt through app state into the token calculation chain:

After:

CLI flags ──> InteractiveSessionComponent ──> API calls ✓
                   │                          (uses custom prompt)
                   ↓
              appState.systemPrompt ──┬──> API calls ✓
                                      │
                                      └──> /context
                                              ↓
                                         calculateContextMetrics()
                                              ↓
                                         calculateSystemPromptTokens()
                                         (uses custom if present,
                                          else default)

Changes

1. Store in app state

  let initialAppState = {
      backgroundTasks: {},
      verbose: ...,
      mainLoopModel: ...,
      mainLoopModelForSession: null,
      showExpandedTodos: ...,
+     systemPrompt: systemPromptCliFlag,
      toolPermissionContext: ...,

2. Sync component prop to state

  ..., React.useEffect(() => {
      ...
  }, [setAppState]);
+ React.useEffect(() => {
+     setAppState((prevState) => ({
+         ...prevState,
+         systemPrompt: systemPromptProp
+     }))
+ }, [systemPromptProp, setAppState]);
  let tools = ...,

3. Pass to token calculator

  terminalWidth = process.stdout.columns || 80,
  appState = await getAppState(),
- metrics = await calculateContextMetrics(messages, model,
-     async () => appState.toolPermissionContext, tools,
-     appState.agentDefinitions, terminalWidth);
+ metrics = await calculateContextMetrics(messages, model,
+     async () => appState.toolPermissionContext, tools,
+     appState.agentDefinitions, terminalWidth, appState.systemPrompt);

4. Thread through metrics calculator

- async function calculateContextMetrics(messages, model, getPermissions,
-     tools, agents, terminalWidth) {
+ async function calculateContextMetrics(messages, model, getPermissions,
+     tools, agents, terminalWidth, customSystemPrompt) {
      let permissionModel = ...,
          maxTokens = ...,
-         [systemPromptTokens, {...}] = await Promise.all([
-             calculateSystemPromptTokens(tools, permissionModel), ...
+         [systemPromptTokens, {...}] = await Promise.all([
+             calculateSystemPromptTokens(tools, permissionModel, customSystemPrompt), ...

5. Use custom prompt when calculating tokens

- async function calculateSystemPromptTokens(tools, permissionModel) {
+ async function calculateSystemPromptTokens(tools, permissionModel,
+     customSystemPrompt) {
+     if (customSystemPrompt) {
+         return await countTokens([{
+             role: "user",
+             content: customSystemPrompt
+         }], []) || 0;
+     }
      let [defaultPromptParts, gitStatus] = await Promise.all([
          generateDefaultSystemPrompt(tools, permissionModel), getGitStatus()]),

Result

Running --system-prompt "Your name is Waka-Waka" now shows ~17 tokens in /context instead of 2.8k tokens.

github-actions[bot] · 9 months ago

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

alex-on-java · 9 months ago

I just double-checked with the latest (2.0.76) version, the issue is still present.

github-actions[bot] · 8 months ago

This issue has been automatically closed due to 60 days of inactivity. If you're still experiencing this issue, please open a new issue with updated information.

marcindulak · 8 months ago

This issue was closed incorrectly despite recent human comments. This behavior of the bot is reported at https://github.com/anthropics/claude-code/issues/16497. Please upvote that issue, so maybe it gets noticed.

github-actions[bot] · 8 months ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.