feat: add xAI prompt caching via x-grok-conv-id header - #5548
Closed
Julientalbot wants to merge 1 commit into
Closed
feat: add xAI prompt caching via x-grok-conv-id header#5548Julientalbot wants to merge 1 commit into
Julientalbot wants to merge 1 commit into
Conversation
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
|
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) |
Contributor
|
Merged via PR #5604. Your commit was cherry-picked onto current main with your authorship preserved in git log. Thanks @Julientalbot! |
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.
What
When using xAI's API directly (
base_urlcontainsx.ai), send thex-grok-conv-idheader set to the Hermessession_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 existingsession_idas a stable conversation identifier. Zero impact on non-xAI providers.Related