Skip to content

[V1.3.1] Prompt caching: low hit rate after mid-task provider switch #238

@zfoong

Description

@zfoong

What happend:
When the provider is switched to Anthropic mid-task, reinitialize() wipes _session_system_prompts, so has_session_cache() returns False and the action router falls back to the single-turn path. Only the system prompt gets cached for the rest of the task — token cache rate stays around 11–14% instead of 80%+.

Replicate:

  1. Start a task under any provider.
  2. Switch provider mid-task via model settings.
  3. Continue the task. Cache hit rate drops sharply for the rest of the task.

Cause: reinitialize() wipes _session_system_prompts, so has_session_cache() returns False and the action router falls back to the single-turn path instead of the multi-turn KV cache path. Affects all providers: Anthropic loses prefix caching beyond the system prompt, OpenAI/DeepSeek/Grok lose prompt_cache_key routing, BytePlus/Gemini lose session continuity.

Possible fix: Preserve _session_system_prompts across reinitialize() (still wipe provider-specific state like _anthropic_session_messages), and call context_engine.reset_event_stream_sync(call_type) for registered sessions so the next router call seeds the new provider's cache via the "first call" branch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions