Skip to main content

ll2cz - LiteLLM to CloudZero ETL Tool

Transform LiteLLM database data into CloudZero AnyCost CBF format for cost tracking and analysis.

Features

  • Extract usage data from LiteLLM PostgreSQL or SQLite database
  • Transform data into CloudZero Billing Format (CBF)
  • Cost comparison analysis between SpendLogs and user tables (v0.4.0+)
  • Dual data source support with --source option: transaction-level SpendLogs or daily aggregated user tables
  • SQLite support for testing and offline analysis (v0.6.1+)
  • Analysis mode with beautiful terminal output using Rich
  • Multiple output options: CSV files or direct CloudZero API streaming
  • Built with modern Python tools: uv, ruff, pytest, polars, httpx

Installation

# Install with uv (recommended)
uv add ll2cz

# Or install with pip
pip install ll2cz

From Source

git clone https://github.com/Cloudzero/cloudzero-litellm-toolkit.git
cd cloudzero-litellm-toolkit
uv sync

Configuration

ll2cz supports configuration files to avoid repeating common settings. You can store your database connection, CloudZero API credentials, and other settings in ~/.ll2cz/config.yml.

Create Configuration File

# Create an example configuration file
ll2cz config example

# Check current configuration status
ll2cz config status

This creates ~/.ll2cz/config.yml with the following structure:

database_url: postgresql://user:password@host:5432/litellm_db
cz_api_key: your-cloudzero-api-key
cz_connection_id: your-connection-id

Configuration Priority

CLI arguments always take priority over configuration file values:

  1. CLI arguments (highest priority)
  2. Configuration file (~/.ll2cz/config.yml)
  3. Default values (lowest priority)

CLI Commands

# Show version
ll2cz --version

# Show help
ll2cz --help

# Configuration management
ll2cz config example     # Create example config file
ll2cz config status      # Show current config status
ll2cz config edit        # Interactively edit configuration

Usage

Transform Mode

Transform LiteLLM data to CloudZero CBF format:

# Display data on screen (formatted table) - PostgreSQL
ll2cz transform --input "postgresql://user:pass@host:5432/litellm_db" --screen

# Display data on screen - SQLite
ll2cz transform --input "sqlite://path/to/litellm.sqlite" --screen

# Export to CSV file
ll2cz transform --input "postgresql://user:pass@host:5432/litellm_db" --output data.csv

# Limit records for screen display
ll2cz transform --screen --limit 25

Transmit Mode

Send data directly to CloudZero AnyCost API:

# Send today's data
ll2cz transmit day

# Send specific day's data (DD-MM-YYYY format)
ll2cz transmit day 15-01-2024

# Send current month's data
ll2cz transmit month

# Send specific month's data (MM-YYYY format)
ll2cz transmit month 01-2024

# Send all available data (batched by day)
ll2cz transmit all

# Test mode - show payloads without sending (5 records only)
ll2cz transmit day --test

# Use append mode (sum operation instead of replace_hourly)
ll2cz transmit day --append

# Specify timezone for date handling
ll2cz transmit day --timezone "US/Eastern"

# Limit number of records to process
ll2cz transmit month --limit 1000

Analysis Mode

Analyze your LiteLLM database data:

# General data analysis
ll2cz analyze data --limit 10000

# Show raw table data
ll2cz analyze data --show-raw --table all

# Show specific table only
ll2cz analyze data --show-raw --table user

# CZRN (CloudZero Resource Name) analysis
ll2cz analyze czrn --limit 10000

# Spend analysis
ll2cz analyze spend --limit 10000

# Database schema analysis
ll2cz analyze schema --output schema_docs.md

# Refresh cache from server (use cache commands)
ll2cz cache refresh

# Save analysis to JSON
ll2cz analyze data --json analysis.json

Cache Management

Manage local data cache for offline operation:

# Check cache status (local only)
ll2cz cache status

# Check cache status with remote server verification
ll2cz cache status --remote-check

# Clear local cache
ll2cz cache clear

# Force refresh cache from server
ll2cz cache refresh

Data Transformation

The tool transforms LiteLLM usage logs into CloudZero's CBF format with the following mappings:

  • spend → cost
  • total_tokens → usage_quantity
  • model, user_id, call_type → dimensions
  • metadata fields → additional dimensions
  • Duration calculated from startTime and endTime

Technology Stack

This project follows modern Python best practices:

  • Python 3.12 - Latest Python version
  • uv - Fast Python package manager
  • Polars - High-performance DataFrames (instead of pandas)
  • httpx - Modern HTTP client (instead of requests)
  • Rich - Beautiful terminal output and formatting
  • Typer - Modern CLI framework with rich help formatting
  • PyYAML - YAML configuration file support
  • Pathlib - Modern filesystem path operations
  • pytest - Testing framework
  • ruff - Fast Python linter and formatter

Development

# Setup development environment
uv sync --dev

# Run tests
uv run pytest

# Run linting
uv run ruff check src/ tests/

# Fix linting issues
uv run ruff check --fix src/ tests/

# Build package
uv build

Testing with SQLite

For testing and development, you can use SQLite instead of PostgreSQL:

# Create a test SQLite database with sample data
python scripts/create_test_sqlite.py

# Use the test database
ll2cz transmit all --test --input "sqlite://test.sqlite"

The test database includes:

  • Sample organizations, teams, and users
  • API keys with realistic naming
  • 30 days of usage data
  • Multiple model providers (OpenAI, Anthropic, etc.)

Requirements

  • Python ≥ 3.12
  • PostgreSQL or SQLite database with LiteLLM data
  • CloudZero API key and connection ID (for streaming mode)

License

Apache 2.0 - see LICENSE file for details.

Metadata

Release files for ll2cz 0.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ll2cz 0.6.2
File Size Uploaded
ll2cz-0.6.2.tar.gz 73.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ll2cz 0.6.2
File Interpreter ABI Platform
ll2cz-0.6.2-py3-none-any.whl Python 3 none any Details

Total release size: 163.6 kB

Release files / ll2cz-0.6.2.tar.gz

Download URL ll2cz-0.6.2.tar.gz
Size 73.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9af8c022b6690dc7f79037e8350e90d11e96c1570117b23e5d1a779c3ce2ba64
BLAKE2b-256 checksum
How to use checksums
de3c140199e9479c1639a45796ebdc907e80e47190cb4866b71886f4743b4e2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / ll2cz-0.6.2-py3-none-any.whl

Download URL ll2cz-0.6.2-py3-none-any.whl
Size 90.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd8f8d030c02533600d1f627a9938dc8a5a917060542d42da58cb3aac7245fab
BLAKE2b-256 checksum
How to use checksums
341332a48d3d1a792d3c879d4748ceef9333f83cab8ad41b8b719c79b5780a28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.6.2 This release

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page