Spend Log Cleanup
LiteLLM stores a log for every request. Over time, these logs can grow large and slow down your database. The Spend Log Cleanup feature helps manage database size by deleting old logs automatically.
Usageβ
Requirementsβ
- Postgres (for log storage)
- Redis (optional) β required only if you're running multiple proxy instances and want to enable distributed locking
Setupβ
Add this to your proxy_config.yaml
under general_settings
:
general_settings:
maximum_spend_logs_retention_period: "7d" # Keep logs for 7 days
# Optional: set how frequently cleanup should run
maximum_spend_logs_retention_interval: "1d" # Run cleanup every day
litellm_settings:
cache: true
cache_params:
type: redis
Configuration Optionsβ
maximum_spend_logs_retention_period
(required)β
How long logs should be kept before deletion. Supported formats:
"7d"
β 7 days"24h"
β 24 hours"60m"
β 60 minutes"3600s"
β 3600 seconds
maximum_spend_logs_retention_interval
(optional)β
How often the cleanup job should run. Uses the same format as above. If not set, cleanup will run every 24 hours if and only if maximum_spend_logs_retention_period
is set.
How it worksβ
Step 1. Lock Acquisition (Optional with Redis)β
If Redis is enabled, LiteLLM uses it to make sure only one instance runs the cleanup at a time.
- If the lock is acquired:
- This instance proceeds with cleanup
- Others skip it
- If no lock is present:
- Cleanup still runs (useful for single-node setups)
Working of spend log deletions
Step 2. Batch Deletionβ
Once cleanup starts:
- It calculates the cutoff date using the configured retention period
- Deletes logs older than the cutoff in batches of 1000
- Adds a short delay between batches to avoid overloading the database
Default settings:β
- Batch size: 1000 logs
- Max batches per run: 500
- Max deletions per run: 500,000 logs
You can change the number of batches using an environment variable:
SPEND_LOG_RUN_LOOPS=200
This would allow up to 200,000 logs to be deleted in one run.
Batch deletion of old logs
Summaryβ
Spend Log Cleanup helps keep your database fast by regularly deleting old logs. Itβs safe, customizable, and works well for both single-node and multi-node deployments.