[BUG] uncacheable system prompt caused by `Primary working directory:` in git worktree
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
- even with includeGitInstructions=false to mitigate https://github.com/anthropics/claude-code/issues/47107
- even with 'the exact same question' to mitigate https://github.com/anthropics/claude-code/issues/47098
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...
-> appears claude does not make a difference if its a worktree "you created as its own project"
or "claude agent launched this" what would have different expectations about where memory would live...
---
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.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗