Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/tutorials/enable-response-caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ You end with one enabled `CachePolicy` and a reproducible header-level proof tha

## How It Works

The proxy keys each request on a fingerprint built from the resolved model alias, the caller key, and the request body. When an enabled `CachePolicy` matches the request, the proxy:
The proxy keys each request on a fingerprint built from the resolved model alias and the normalized request body — `messages`, `temperature`, `top_p`, `max_tokens`, and any other top-level request fields (tool-calling fields, future OpenAI-shape fields, anything the proxy doesn't pre-process).

**The caller key is intentionally excluded from the fingerprint.** Two callers asking the same prompt to the same model alias hit the same cache entry, so the second caller sees the first caller's cached response.
Comment thread
janiussyafiq marked this conversation as resolved.

Treat prompts as crossing caller boundaries: don't cache content that shouldn't be visible to other callers in your deployment.
Comment thread
janiussyafiq marked this conversation as resolved.

When an enabled `CachePolicy` matches the request, the proxy:

1. computes the fingerprint
2. looks up the cache
Expand Down Expand Up @@ -78,7 +84,7 @@ x-aisix-cache: miss

## Step 3: Send The Same Request Again — Cache Hit

Repeat the **exact same** request body, model alias, and bearer:
Repeat the request with the same body and model alias:

```bash title="Second call — should report cache hit"
curl -sSi -X POST http://127.0.0.1:3000/v1/chat/completions \
Expand Down Expand Up @@ -116,7 +122,7 @@ curl -sSi -X POST http://127.0.0.1:3000/v1/chat/completions \

## What Just Happened

The proxy hashed the resolved model alias, caller key, and request body for each request. The first call missed the cache and went to the upstream; the second call had the same fingerprint and hit the cache; the third call had a different prompt and therefore a different fingerprint, so it missed again. The upstream saw only two calls instead of three.
The proxy hashed the resolved model alias and the request body for each request. The first call missed the cache and went to the upstream; the second call had the same fingerprint and hit the cache; the third call had a different prompt and therefore a different fingerprint, so it missed again. The upstream saw only two calls instead of three.

This contract is exercised by `tests/e2e/src/cases/cache-policy-e2e.test.ts` (identical-request hit) and `tests/e2e/src/cases/cache-scenarios-e2e.test.ts` (different-prompt miss). Both tests assert the `x-aisix-cache` header values and the upstream `receivedRequests.length` count — the cache header is a published contract that cp-api and the dashboard's `/logs` view depend on.

Expand Down
Loading