claude-code - 💡(How to fix) Fix [BUG] uncacheable system prompt caused by `Primary working directory:` in git worktree [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#48236Fetched 2026-04-15 06:29:21
View on GitHub
Comments
1
Participants
2
Timeline
7
Reactions
0
Author
Timeline (top)
labeled ×5commented ×1cross-referenced ×1

Error Message

Error Messages/Logs

Fix Action

Fix / Workaround

Attention: currently this issue gets "masked" by two other issues that even break the in-same-directory cache, follow the reproduction above precisely to workaround them when testing for this issue.

Code Example

via ANTHROPIC_BASE_URL:

anthropicReq.system[2]:
[...]
# Environment
You have been invoked in the following environment:
 - Primary working directory: /tmp/the_project/feature 
  - Is a git repository: true
 - Platform: linux
[...]

---

mkdir the_project && cd the_project
git init --bare -b master .git
git worktree add master
pushd master && GIT_AUTHOR_NAME=dummy GIT_AUTHOR_EMAIL=dummy GIT_COMMITTER_NAME=dummy GIT_COMMITTER_EMAIL=dummy git commit --allow-empty -m "Dummy commit" && popd
git worktree add feature

pushd master && CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1 claude "Hello" && popd
> /exit
# 3 input, 12 output, 0 cache read, 15.9k cache write ($0.0597)
# ^^  expected initial warmup

pushd master && CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1 claude "Hello" && popd
> /exit
# 3 input, 12 output, 15.9k cache read, 0 cache write ($0.0050)
# ^^ great result

pushd feature && CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1 claude "Hello" && popd
> /exit
#  3 input, 12 output, 10.4k cache read, 5.4k cache write ($0.0237)
# ^^ 10k-cache-read fits the `tools` - 5k-cache-write fits systemprompt + skills + system-message - **what is terrible**
RAW_BUFFERClick to expand / collapse

What's Wrong?

asking the same question from two different worktrees costs thousands of tokens (only the tools-cache hits) resulting in a 5x-surcharge

This also seems to apply to subagent worktrees isolation: worktree

What Should Happen?

system-prompt, skills and most of the system-messages should also hit a cache. and even the user prompt could be a cache-hit for a lot of usecases... (imaginary 30k-token long 'please analyze the following...' launched on multiple featurebranches / git-bisect)

Error Messages/Logs

via ANTHROPIC_BASE_URL:

anthropicReq.system[2]:
[...]
# Environment
You have been invoked in the following environment:
 - Primary working directory: /tmp/the_project/feature 
  - Is a git repository: true
 - Platform: linux
[...]

Steps to Reproduce

mkdir the_project && cd the_project
git init --bare -b master .git
git worktree add master
pushd master && GIT_AUTHOR_NAME=dummy GIT_AUTHOR_EMAIL=dummy GIT_COMMITTER_NAME=dummy GIT_COMMITTER_EMAIL=dummy git commit --allow-empty -m "Dummy commit" && popd
git worktree add feature

pushd master && CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1 claude "Hello" && popd
> /exit
# 3 input, 12 output, 0 cache read, 15.9k cache write ($0.0597)
# ^^  expected initial warmup

pushd master && CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1 claude "Hello" && popd
> /exit
# 3 input, 12 output, 15.9k cache read, 0 cache write ($0.0050)
# ^^ great result

pushd feature && CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS=1 claude "Hello" && popd
> /exit
#  3 input, 12 output, 10.4k cache read, 5.4k cache write ($0.0237)
# ^^ 10k-cache-read fits the `tools` - 5k-cache-write fits systemprompt + skills + system-message - **what is terrible**

observe a 5x cost inflation that rly should not be needed...

Claude Model

Sonnet (default)

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

2.1.108

Platform

Anthropic API

Operating System

Ubuntu/Debian Linux

Terminal/Shell

Xterm

Additional Information

the rest of the # Environment block seems to be rather stable, but the - Primary working directory: ... (and its little friend Is a git repository: true|false) lines should either come a lot later or not even be there at all...

  • 'not there at all' is likely iffy - if not an impossible thing... -> in many tools.description´s claude gets instructed to specifically use absolute paths

  • but it doesnt rly go well into system-messages either: -- for two clones of the same repository skills & project-claude.md could also be a cache-hit... -- same for git-worktrees not agent-worktrees, but a user launching claude in a worktree -- but also for agent-worktrees: having multiple agents not hit each others cache on launch gets expensive when only the tools-block gets a hit

To the extend that it almost needs to go after not into the last system-message -> and then cache_control tags on the system-message with claude.md, not this directory-line


Notable: agents seem to have there own trouble with this...


Also note that aboves demo uses a worktree on purpose:

  • there is also a problematic You have a persistent, file-based memory system at /home/you/.claude/projects/.../memory/. line in the system prompt for worktrees this appears to be getting normalized so it is currently not causing an extra cache miss, but in turn is causing the [BUG] Auto-memory above - what when fixed in place will make this cache miss again!


Attention: currently this issue gets "masked" by two other issues that even break the in-same-directory cache, follow the reproduction above precisely to workaround them when testing for this issue.

extent analysis

TL;DR

The issue can be mitigated by reordering or removing the Primary working directory line in the system-message to allow for better cache hits across different worktrees.

Guidance

  • Investigate the impact of moving the Primary working directory line to a later position in the system-message or removing it altogether to reduce cache misses.
  • Verify the effect of this change on cache hits for system-prompt, skills, and system-messages in different worktrees.
  • Consider adding cache control tags to the system-message to improve cache hits for specific components like claude.md.
  • Test the changes with the provided reproduction steps to ensure the issue is resolved without introducing new problems.

Example

No code snippet is provided as the issue is related to the Anthropic API and Claude Code configuration.

Notes

The issue is complex and intertwined with other problems (https://github.com/anthropics/claude-code/issues/47107, https://github.com/anthropics/claude-code/issues/47098), which need to be worked around during testing. The proposed solution may have unintended consequences and should be thoroughly tested.

Recommendation

Apply the workaround of reordering or removing the Primary working directory line, as it is a targeted approach to address the specific issue of cache misses in worktrees, and monitor the results to determine if further adjustments are needed.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix [BUG] uncacheable system prompt caused by `Primary working directory:` in git worktree [1 comments, 2 participants]