Skip to content

feat(gateway): cache AIAgent per session for prompt caching - #2361

Merged
teknium1 merged 1 commit into
mainfrom
hermes/hermes-5d6932ba
Mar 21, 2026
Merged

feat(gateway): cache AIAgent per session for prompt caching#2361
teknium1 merged 1 commit into
mainfrom
hermes/hermes-5d6932ba

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Problem

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 ($3/MTok vs $0.30/MTok).

CLI didn't have this problem because it reuses a single AIAgent across all turns with _cached_system_prompt built once.

Fix

Cache 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 invalidation:

  • Config changes (model, provider, toolsets, reasoning, ephemeral prompt) — automatic via signature mismatch
  • /new, /reset, /clear — evicts session's cached agent
  • /model — clears all cached agents (global config change)
  • /reasoning — clears all cached agents

Per-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 files
  • self.tools — tool schemas resolved in __init__
  • Model, provider, base_url — all config that affects the API call shape

What's fresh each turn

  • Conversation history (from session transcript)
  • Callbacks (progress, streaming, hooks)
  • Honcho context
  • Todo store hydration

5753 tests passing (1286 gateway tests).

@teknium1
teknium1 force-pushed the hermes/hermes-5d6932ba branch 3 times, most recently from cfdf604 to 206c3ef Compare March 21, 2026 23:13
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
teknium1 force-pushed the hermes/hermes-5d6932ba branch from 206c3ef to 342096b Compare March 21, 2026 23:21
@teknium1
teknium1 merged commit 52dd479 into main Mar 21, 2026
1 check passed
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
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.

1 participant