快取
關於 OpenAI/Anthropic Prompt Caching,請前往 這裡
快取 LLM 回應。LiteLLM 的快取系統會儲存並重用 LLM 回應,以節省成本並降低延遲。當您兩次送出相同的請求時,系統會回傳快取的回應,而不是再次呼叫 LLM API。
支援的快取
| 快取 | cache_params.type | 設定 |
|---|---|---|
| Redis、Valkey、ElastiCache、Memorystore | redis | Redis 與 Valkey |
| Redis 語意快取 | redis-semantic | 語意快取 |
| Valkey 語意快取 | valkey-semantic | 語意快取 |
| Qdrant 語意快取 | qdrant-semantic | 語意快取 |
| S3 儲存貯體 | s3 | S3 與 GCS |
| GCS 儲存貯體 | gcs | S3 與 GCS |
| 記憶體中 | local | 下方 |
| 磁碟 | disk | 下方 |
Redis 是超過單一 worker 時的正確預設選擇。記憶體中快取存在於單一 worker 程序內,因此執行四個 workers 的 proxy 會保有四個彼此獨立的快取,命中率大約會隨 worker 數量而下降。其餘 Redis 帶來的好處,請參閱 需要 Redis 的項目。
完全匹配快取(redis、s3、gcs、local、disk)會以整個請求的雜湊作為鍵,因此對對話的任何變更都會造成未命中。語意快取會為提示詞建立嵌入,並提供相似度門檻以上最接近的匹配結果,這適合單輪提示詞,但在 agent 流量上很容易出問題;在啟用之前,請先閱讀 語意快取。
快速開始
步驟 1:將 cache 加入 config.yaml
只要將 cache key 加到 config.yaml 即可啟用快取
model_list:
- model_name: gpt-5.6-luna
litellm_params:
model: gpt-5.6-luna
- model_name: text-embedding-ada-002
litellm_params:
model: text-embedding-ada-002
litellm_settings:
set_verbose: True
cache: True # set cache responses to True, litellm defaults to using a redis cache
步驟 2:將 Redis 憑證加入 .env
REDIS_URL = "" # REDIS_URL='redis://username:password@hostname:port/database'
## OR ##
REDIS_HOST = "" # REDIS_HOST='redis-18841.c274.us-east-1-3.ec2.cloud.redislabs.com'
REDIS_PORT = "" # REDIS_PORT='18841'
REDIS_PASSWORD = "" # REDIS_PASSWORD='liteLlmIsAmazing'
關於命名空間、ACL 使用者、叢集與 Sentinel 拓撲、TLS、IAM 驗證,以及完整的
REDIS_* 環境變數清單,請參閱 Redis 與 Valkey。快取用戶端的每個命令逾時為
cache_params.socket_timeout(預設 5 秒),不是 REDIS_SOCKET_TIMEOUT;
請參閱 Redis socket_timeout。
步驟 3:使用設定啟動 proxy
$ litellm --config /path/to/config.yaml
步驟 4:測試它
- /chat/completions
- /v1/responses
- /embeddings
兩次送出相同的請求:
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.6-luna",
"messages": [{"role": "user", "content": "write a poem about litellm!"}],
"temperature": 0.7
}'
curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.6-luna",
"messages": [{"role": "user", "content": "write a poem about litellm!"}],
"temperature": 0.7
}'
兩次送出相同的請求:
curl http://0.0.0.0:4000/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.6-luna",
"input": "write a poem about litellm!"
}'
curl http://0.0.0.0:4000/v1/responses \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.6-luna",
"input": "write a poem about litellm!"
}'
兩次送出相同的請求:
curl --location 'http://0.0.0.0:4000/embeddings' \
--header 'Content-Type: application/json' \
--data ' {
"model": "text-embedding-ada-002",
"input": ["write a litellm poem"]
}'
curl --location 'http://0.0.0.0:4000/embeddings' \
--header 'Content-Type: application/json' \
--data ' {
"model": "text-embedding-ada-002",
"input": ["write a litellm poem"]
}'
第二個回應會從快取提供。它會帶有 x-litellm-cache-key 回應標頭,
您可以將其提供給 /cache/delete。
在 cache: True 且沒有 supported_call_types 的情況下,快取會對 /chat/completions、
/completions、/embeddings、/audio/transcriptions、/rerank、/v1/responses 和
/v1/messages 啟用。若要將其限制於其中部分,請參閱
支援的呼叫類型。
在 /v1/responses 上,完全匹配快取會以請求本文作為鍵,因此帶有
previous_response_id 的請求會與將對話內嵌的請求使用不同的鍵,而多輪對話的每一輪都會是自己的項目。
記憶體中與磁碟快取
兩者都不需要外部基礎架構,而且兩者都不會在 workers 或 replicas 之間共享,因此請將它們用於本機開發,而非正式環境。
- In Memory Cache
- Disk Cache
litellm_settings:
cache: True
cache_params:
type: local
litellm_settings:
cache: True
cache_params:
type: disk
disk_cache_dir: /tmp/litellm-cache # OPTIONAL, default to ./.litellm_cache
快取除錯 - /cache/ping
LiteLLM Proxy 提供一個 /cache/ping 端點,用來測試快取是否如預期運作
使用方式
curl --location 'http://0.0.0.0:4000/cache/ping' -H "Authorization: Bearer $LITELLM_API_KEY"
預期回應 - 當快取正常時
{
"status": "healthy",
"cache_type": "redis",
"ping_response": true,
"set_cache_response": "success",
"litellm_cache_params": {
"supported_call_types": "['completion', 'acompletion', 'embedding', 'aembedding', 'atranscription', 'transcription']",
"type": "redis",
"namespace": "None"
},
"redis_cache_params": {
"redis_client": "Redis<ConnectionPool<Connection<host=redis-16337.c322.us-east-1-2.ec2.cloud.redislabs.com,port=16337,db=0>>>",
"redis_kwargs": "{'url': 'redis://:******@redis-16337.c322.us-east-1-2.ec2.cloud.redislabs.com:16337'}",
"async_redis_conn_pool": "BlockingConnectionPool<Connection<host=redis-16337.c322.us-east-1-2.ec2.cloud.redislabs.com,port=16337,db=0>>",
"redis_version": "7.2.0"
}
}
下一步
您可以透過 快取控制 調整哪些內容會被快取以及快取多久,透過 cache_params 參考資料 查詢任何設定,或從上方表格設定特定後端。