Skip to content

Write agent guidance as a prompt section to keep the prompt cache - #118

Merged
mavam merged 2 commits into
mavam:mainfrom
Haichiu:fix/prompt-cache-section
Oct 30, 2026
Merged

mavam merged 2 commits into
mavam:mainfrom
Haichiu:fix/prompt-cache-section

Conversation

@Haichiu

@Haichiu Haichiu commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #117.

Change

before_agent_start now writes the agent guidance to event.systemPromptOptions.sections["pi-agents"] instead of returning a full systemPrompt.

A returned systemPrompt forces Pi to resend the whole leading system prompt on every run. When any other section changes in the middle of a session (for example, MCP servers that finish connecting), the leading prompt changes, and Anthropic re-writes the system block and the full conversation history to the prompt cache. With a section, Pi keeps the leading prompt and appends a small transcript delta.

Verification

  • bun run check: typecheck passes, 254 pass / 0 fail. Biome reports the same 2 warnings as main.
  • Manual test (Pi 1.4.0, claude-opus-5-5, RPC session that spawns one subagent and gets its result). I logged a hash of each system block per request:
    • Before: the leading system block changes on run 2. cacheRead drops to about 5.8k (tools + first block only).
    • After: the leading system block stays the same across all 5 requests. cacheRead covers the full prefix (19.3k). The subagent result still arrives, and the <pi-agents> guidance is still in the prompt.

A changelog entry is in changelog/unreleased/.

A returned systemPrompt makes Pi resend the whole leading prompt on every
run. When another prompt section changes mid-session, the leading prompt
changes too, and the provider re-writes the whole conversation to its
prompt cache. A prompt section lets Pi append a small delta instead.

Fixes mavam#117
Copilot AI balanced review requested due to automatic review settings October 26, 2026 13:21

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@mavam
mavam merged commit 8967d1e into mavam:main Oct 30, 2026
3 checks passed
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.

before_agent_start returns a full systemPrompt, which breaks the prompt cache on mid-session prompt changes

3 participants