Skip to content

[FEATURE] Cache-stable prompts: move volatile context sections out of the system prompt #938

Description

@edenreich

Summary

The agent rebuilds its system prompt (message index 0) on every user turn (addSystemPrompt, internal/agent/agent_utils.go:185-197; call sites internal/agent/agent.go:427 sync and :669 streaming) and embeds dynamic context inside it. Local model servers (Ollama, llama.cpp) cache the KV state of the longest byte-identical prompt prefix - so any change at message[0] forces a full re-prefill of the entire ~8k-token prompt (base prompt + AGENTS.md + tool schemas + context) on the next request.

Of the ~16 sections assembled by contextSections (agent_utils.go:209-224), only five are volatile:

Section Builder Changes when
git_context buildGitContextInfo (agent_utils.go:788-840) every GitContextRefreshTurns (default 10) - branch/commits move as the agent works
project_structure buildProjectTreeInfo (agent_utils.go:852-879) same cadence - tree changes whenever files are created/deleted
memory buildMemoryInfo (agent_utils.go:556-608) effectively every turn
active_skill buildActiveSkillInfo (agent_utils.go:488-521) when the user invokes a skill
Current date line buildSystemPromptText (agent_utils.go:171-172) daily

Everything else (base prompt, custom instructions, AGENTS.md, plugins block, sandbox/os/cwd, bash allow-list per mode, tools roster, skills roster) is static per session/mode.

Measured impact: with qwen3:8b on native Ollama (M4 Pro), chat turns took 30-90s because each turn re-prefilled the full prompt; with a byte-stable prefix, follow-up turns drop to generation time (seconds). Other agent CLIs (e.g. Claude Code) keep their prompt byte-stable and get server-side cache hits on every follow-up turn.

Current workaround (PR #937, examples/working-offline): set INFER_AGENT_CONTEXT_GIT_CONTEXT_ENABLED=false and INFER_AGENT_CONTEXT_TREE_ENABLED=false. This stabilizes the prefix but loses the context entirely - the model no longer sees git status or the project tree.

Proposed direction

Keep the context fresh AND the prefix stable: split contextSections into static (stay in the system prompt) and volatile (the five above), and deliver the volatile ones as a request-only <system-reminder> block at the tail of the outbound message array.

The codebase already has both halves of the pattern:

  • Tail injection convention: injectDueReminders (agent_utils.go:1054-1110) appends hidden user-role <system-reminder> messages; the TUI skips them (internal/ui/components/conversation_view.go:335, via ConversationEntry.Hidden) and the mockgateway scenario matcher explicitly ignores them when anchoring the latest real user message (internal/mockgateway/scenario.go:247-259).
  • Request-only (never persisted) precedent: the system message itself - prepended per request, never written to the conversation store.

The new piece: unlike reminders (append-only and persisted), the volatile-context block must be rebuilt per request and not persisted, otherwise stale git/tree/memory copies accumulate in history. With a tail block, the cache-invalidation point moves from message[0] (invalidates everything) to the last few hundred tokens (invalidates almost nothing).

Test touchpoints: internal/agent/agent_utils_test.go (section builders and buildSystemPromptText ordering), internal/agent/agent_reminder_emission_test.go (tail-injection contract), mockgateway anchoring tests.

Related: workaround shipped in #937.

Acceptance Criteria

  • System prompt (message[0]) is byte-identical across user turns within a session when mode and config are unchanged (including across a day rollover)
  • Git context, project tree, memory, active skill, and current date still reach the model each turn, delivered at the tail of the request
  • The injected tail block is not persisted to the conversation store and is not rendered in the TUI
  • mockgateway scenario anchoring still resolves the latest real user message (existing <system-reminder> skip covers it)
  • Existing agent.context.* config flags keep working; disabling a section still removes it entirely
  • Manual verification with examples/working-offline: second-turn prefill against local Ollama is near zero (follow-up turns respond in roughly generation time)

Activity

  1. added
    enhancement
    on Jul 19, 2026
  2. moved this to Todo in 2026on Jul 19, 2026
  3. moved this from Todo to QA in 2026on Jul 19, 2026
  4. added a commit that references this issue on Jul 21, 2026
    ef0f69e
  5. inference-gateway-releaser commented on Jul 21, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.151.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  6. moved this from QA to Done in 2026on Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementreleased

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions