LiteLLM - 로컬 캐싱
LiteLLM - 로컬 캐싱 (Local Caching)
켰을 때 completion()과 embedding() 호출 캐싱
LiteLLM은 정확 매칭(exact match) 캐싱을 구현하며 다음 캐싱을 지원해요:
- 인메모리 캐싱 (In-Memory Caching) [기본]
- Redis 캐싱 로컬 (Redis Caching Local)
- Redis 캐싱 호스티드 (Redis Caching Hosted)
빠른 시작 사용법 - Completion
캐시의 키는 model 이에요. 다음 예시는 캐시 히트로 이어집니다.
import litellm
from litellm import completion
from litellm.caching.caching import Cache
litellm.cache = Cache()
# Make completion calls
response1 = completion(
model="gpt-5.6-luna",
messages=[{"role": "user", "content": "Tell me a joke."}],
caching=True
)
response2 = completion(
model="gpt-5.6-luna",
messages=[{"role": "user", "content": "Tell me a joke."}],
caching=True
)
# response1 == response2, response 1 is cached
커스텀 키-값 쌍
캐시에 커스텀 키-값 쌍을 추가해요.
from litellm.caching.caching import Cache
cache = Cache()
cache.add_cache(cache_key="test-key", result="1234")
cache.get_cache(cache_key="test-key")
스트리밍과 함께 캐싱
LiteLLM은 스트리밍된 응답도 캐시할 수 있어요.
사용법
import litellm
from litellm import completion
from litellm.caching.caching import Cache
litellm.cache = Cache()
# Make completion calls
response1 = completion(
model="gpt-5.6-luna",
messages=[{"role": "user", "content": "Tell me a joke."}],
stream=True,
caching=True)
for chunk in response1:
print(chunk)
response2 = completion(
model="gpt-5.6-luna",
messages=[{"role": "user", "content": "Tell me a joke."}],
stream=True,
caching=True)
for chunk in response2:
print(chunk)
사용법 - Embedding()
캐시의 키는 model 이에요. 다음 예시는 캐시 히트로 이어집니다.
import time
import litellm
from litellm import embedding
from litellm.caching.caching import Cache
litellm.cache = Cache()
start_time = time.time()
embedding1 = embedding(model="text-embedding-ada-002", input=["hello from litellm"*5], caching=True)
end_time = time.time()
print(f"Embedding 1 response time: {end_time - start_time} seconds")
start_time = time.time()
embedding2 = embedding(model="text-embedding-ada-002", input=["hello from litellm"*5], caching=True)
end_time = time.time()
print(f"Embedding 2 response time: {end_time - start_time} seconds")
출처: 문서