Skip to content

feat: add xAI prompt caching via x-grok-conv-id header - #5548

Closed
Julientalbot wants to merge 1 commit into
NousResearch:mainfrom
Julientalbot:feat/xai-prompt-caching
Closed

feat: add xAI prompt caching via x-grok-conv-id header#5548
Julientalbot wants to merge 1 commit into
NousResearch:mainfrom
Julientalbot:feat/xai-prompt-caching

Conversation

@Julientalbot

Copy link
Copy Markdown
Contributor

What

When using xAI's API directly (base_url contains x.ai), send the x-grok-conv-id header set to the Hermes session_id. This routes consecutive requests to the same server, maximizing automatic prompt cache hits.

Why

xAI supports automatic prompt caching — repeated prefixes are cached server-side. However, requests must hit the same server. Without x-grok-conv-id, requests are load-balanced randomly, wasting the cache.

With this header, a multi-turn Hermes conversation with Grok stays on one server, giving significant latency and cost reductions on long conversations.

How

6-line addition in _build_api_kwargs() — detects xAI base URL, adds the header using the existing session_id as a stable conversation identifier. Zero impact on non-xAI providers.

Related

When using xAI's API directly (base_url contains x.ai), send the
x-grok-conv-id header set to the Hermes session_id. This routes
consecutive requests to the same server, maximizing automatic
prompt cache hits.

Ref: https://docs.x.ai/developers/advanced-api-usage/prompt-caching
@davidgovea

Copy link
Copy Markdown

yeah I'm also interested in this. I tried to lay some groundwork that would make it easier to plug in different providers' caching approaches

#5252 (doesn't include the grok header but covers more surfaces and grok's header would be a oneline add)

@teknium1

teknium1 commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Merged via PR #5604. Your commit was cherry-picked onto current main with your authorship preserved in git log. Thanks @Julientalbot!

@teknium1 teknium1 closed this Apr 6, 2026
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.

4 participants