feat(gateway): cache AIAgent per session for prompt caching - #2361
Merged
Conversation
teknium1
force-pushed
the
hermes/hermes-5d6932ba
branch
3 times, most recently
from
March 21, 2026 23:13
cfdf604 to
206c3ef
Compare
The gateway created a fresh AIAgent per message, rebuilding the system prompt (including memory, skills, context files) every turn. This broke prompt prefix caching — providers like Anthropic charge ~10x more for uncached prefixes. Now caches AIAgent instances per session_key with a config signature. The cached agent is reused across messages in the same session, preserving the frozen system prompt and tool schemas. Cache is invalidated when: - Config changes (model, provider, toolsets, reasoning, ephemeral prompt) — detected via signature mismatch - /new, /reset, /clear — explicit session reset - /model — global model change clears all cached agents - /reasoning — global reasoning change clears all cached agents Per-message state (callbacks, stream consumers, progress queues) is set on the agent instance before each run_conversation() call. This matches CLI behavior where a single AIAgent lives across all turns in a session, with _cached_system_prompt built once and reused.
teknium1
force-pushed
the
hermes/hermes-5d6932ba
branch
from
March 21, 2026 23:21
206c3ef to
342096b
Compare
angelburgosrosado
pushed a commit
to angelburgosrosado/hermes-agent
that referenced
this pull request
Apr 27, 2026
…5d6932ba feat(gateway): cache AIAgent per session for prompt caching
02356abc
pushed a commit
to 02356abc/hermes-agent
that referenced
this pull request
May 14, 2026
…5d6932ba feat(gateway): cache AIAgent per session for prompt caching
gweeteve
pushed a commit
to gweeteve/hermes-agent
that referenced
this pull request
Jun 2, 2026
…5d6932ba feat(gateway): cache AIAgent per session for prompt caching
melon-xf
added a commit
to melon-xf/hermes-agent
that referenced
this pull request
Sep 3, 2026
…5d6932ba feat(gateway): cache AIAgent per session for prompt caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The gateway created a fresh
AIAgentper message, rebuilding the system prompt (including memory, skills, context files) every turn. This broke prompt prefix caching — providers like Anthropic charge ~10x more for uncached prefixes ($3/MTok vs $0.30/MTok).CLI didn't have this problem because it reuses a single
AIAgentacross all turns with_cached_system_promptbuilt once.Fix
Cache
AIAgentinstances persession_keywith a config signature. The cached agent is reused across messages in the same session, preserving the frozen system prompt and tool schemas.Cache invalidation:
/new,/reset,/clear— evicts session's cached agent/model— clears all cached agents (global config change)/reasoning— clears all cached agentsPer-message state (callbacks, stream consumers, progress queues) is set on the agent instance before each
run_conversation()call — these are not cached.What stays frozen (cached across turns)
_cached_system_prompt— system prompt with memory, skills, context filesself.tools— tool schemas resolved in__init__What's fresh each turn
5753 tests passing (1286 gateway tests).