Never pay for the same LLM call twice.
A high-performance API caching proxy built with FastAPI and SQLAlchemy that provides intelligent caching for HTTP requests. This service helps reduce API costs and improve response times by caching API responses and serving them when appropriate. Optimized for caching responses from Large Language Models (LLMs) and other usage-based API services to reduce costs and improve performance.
Set up the env file. You can use the .env.example file as a template:
API_SECRET_TOKEN=my-secret-token
DATABASE_URL=postgresql://postgres:postgres@postgres:5432/proxy_cache
Start LLMCache with docker compose locally:
docker compose -f docker-compose.local.yml up -d
You can now use it with any API that you need to cache, and even with the most common LLM modules. Example for the Openai python module:
import openai
openai.api_base = "http://localhost:8001"
openai.default_headers = {
"x-proxy-auth": "my-secret-token",
"x-proxy-base-url": "https://api.openai.com/v1",
}
Example for the Anthropic python module:
import anthropic
client = anthropic.Anthropic(
base_url="http://0.0.0.0:8001",
http_client=httpx.Client(
transport=transport,
headers={
"x-proxy-auth": "my-secret-token",
"x-proxy-base-url": "https://api.anthropic.com",
},
),
)
- Incoming requests are authenticated using the
X-Proxy-Auth
header - A unique cache key is generated based on:
- HTTP method
- Path
- Headers (filtered)
- Query parameters/POST data
- If a valid cached response exists, it's returned immediately
- If no cache exists or it's expired, the request is forwarded to the target API
- The response is cached for future use
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
To test a local version of the LLMCache, you can use the following command:
docker compose -f docker-compose.test.yml up -d
To properly test, you will need to set up the additional envs you find in the .env.example file.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with FastAPI
- Database support via SQLAlchemy
- HTTP client powered by HTTPX