-
Notifications
You must be signed in to change notification settings - Fork 53
Add W&B Inference prefix caching docs #2372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
corbt
wants to merge
3
commits into
main
Choose a base branch
from
codex/prefix-caching-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| --- | ||
| title: "Prefix caching" | ||
| description: "Reduce latency for repeated prompts with prefix caching, and isolate cache reuse with cache_salt when needed." | ||
| --- | ||
|
|
||
| W&B Inference uses prefix caching on supported hosted models to speed up repeated requests with identical prompt prefixes. | ||
|
|
||
| When a request shares the same prompt prefix as an earlier request on the same backend, the model reuses the previously computed key-value (KV) cache instead of recomputing the entire prefix. This reduces latency for repeated prompts, long system prompts, and workloads with a stable shared prefix. | ||
|
|
||
| Prefix caching is automatic on supported models, so you don't need to enable it in your request. This page describes when prefix caching is most effective and how to control cache isolation with `cache_salt`. | ||
|
|
||
| ## When prefix caching helps | ||
|
|
||
| Prefix caching is most useful when you repeatedly send requests that share a long common prefix, such as: | ||
|
|
||
| - A large system prompt reused across many requests. | ||
| - A long shared document followed by different user questions. | ||
| - Repeated evaluation prompts with only small per-request changes. | ||
| - Multi-turn workloads where much of the conversation history stays the same. | ||
|
|
||
| ## Cache isolation | ||
|
|
||
| In some environments, you might need to prevent cache reuse across different users or applications. The `cache_salt` parameter provides this control. | ||
|
|
||
| By default, requests with identical prompt prefixes may reuse cache on shared infrastructure when the backend allows it. | ||
|
|
||
| To isolate cache reuse to a specific trust boundary, set the `cache_salt` request parameter. Requests only reuse prefix cache when both the prompt prefix and the `cache_salt` match. | ||
|
|
||
| Use `cache_salt` when you want cache reuse within a single user, tenant, session, or application boundary, but don't want reuse across other callers. | ||
|
|
||
| ### How it works | ||
|
|
||
| The presence and value of `cache_salt` affect cache reuse as follows: | ||
|
|
||
| - Same prompt prefix, no `cache_salt`: cache may be reused across matching requests. | ||
| - Same prompt prefix, same `cache_salt`: cache can be reused. | ||
| - Same prompt prefix, different `cache_salt`: cache is isolated and not reused across salts. | ||
|
|
||
| <Note> | ||
| `cache_salt` must be a non-empty string when provided. | ||
| </Note> | ||
|
|
||
| ## Examples | ||
|
|
||
| The following examples show how to send a chat completion request with `cache_salt` to isolate cache reuse. | ||
|
|
||
| <Tabs> | ||
| <Tab title="Python"> | ||
| ```python | ||
| import openai | ||
|
|
||
| client = openai.OpenAI( | ||
| base_url="https://api.inference.wandb.ai/v1", | ||
| api_key="<your-api-key>", | ||
| ) | ||
|
|
||
| response = client.chat.completions.create( | ||
| model="moonshotai/Kimi-K2.5", | ||
| messages=[ | ||
| { | ||
| "role": "system", | ||
| "content": "You are a careful assistant that answers concisely." | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": "Summarize this document in one sentence: <long shared prefix here>" | ||
| }, | ||
| ], | ||
| cache_salt="tenant-a-user-123-secret", | ||
| ) | ||
|
|
||
| print(response.choices[0].message.content) | ||
| ``` | ||
| </Tab> | ||
|
|
||
| <Tab title="Bash"> | ||
| ```bash | ||
| curl https://api.inference.wandb.ai/v1/chat/completions \ | ||
| -H "Content-Type: application/json" \ | ||
| -H "Authorization: Bearer <your-api-key>" \ | ||
| -d '{ | ||
| "model": "moonshotai/Kimi-K2.5", | ||
| "messages": [ | ||
| { "role": "system", "content": "You are a careful assistant that answers concisely." }, | ||
| { "role": "user", "content": "Summarize this document in one sentence: <long shared prefix here>" } | ||
| ], | ||
| "cache_salt": "tenant-a-user-123-secret" | ||
| }' | ||
| ``` | ||
| </Tab> | ||
| </Tabs> | ||
|
|
||
| ## Response behavior | ||
|
|
||
| To confirm that prefix caching is active for a request, check the response usage details. On some models, usage details may include cached token counts in `usage.prompt_tokens_details.cached_tokens` when prefix cache is reused. The availability of that field varies by model and backend. | ||
|
|
||
| ## Related pages | ||
|
|
||
| The following pages cover related topics: | ||
|
|
||
| - [Chat Completions](/inference/api-reference/chat-completions) | ||
| - [Enable streaming responses](/inference/response-settings/streaming) | ||
| - [Structured output](/inference/response-settings/structured-output) | ||
| - [JSON mode](/inference/response-settings/json-mode) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@corbt I don't think this works - gives you
TypeError: Completions.create() got an unexpected keyword argument 'cache_salt'I think you can replace with something like: