CLI tool to generate structured LLM logs for CrashLens cost detection
Project description
CrashLens Logger 🧠💸
Structured Token & Cost Logs for OpenAI / Anthropic Usage
⚠️ Are you burning money on GPT calls without knowing where or why?
CrashLens Logger captures cost, tokens, and prompts in JSON logs — for FinOps, audits, or debugging.
Purpose
CrashLens Logger is a Python package for generating structured, machine-readable logs of LLM (Large Language Model) API usage.
It helps you:
- Track prompt, model, and token usage for every AI call
- Automatically calculate cost using standard model pricing
- Output logs in newline-delimited JSON (NDJSON) for easy analysis, monitoring, and cost tracking
Real Use Cases
- 🔍 Debug fallback loops by logging all model calls with prompt/token trace
- 💰 Auto-generate cost reports across agents & users
- 🧠 Analyze which prompts are burning tokens (and why)
- 🛡️ Audit LLM usage for compliance or security
Installation
pip install --upgrade crashlens_logger
This will install or upgrade to the latest version.
Quick Start
from crashlens_logger import CrashLensLogger
import uuid
from datetime import datetime
import openai
logger = CrashLensLogger()
def call_and_log():
trace_id = str(uuid.uuid4())
start_time = datetime.utcnow().isoformat() + "Z"
prompt = "What are the main tourist attractions in Rome?"
model = "gpt-3.5-turbo"
response = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": prompt}]
)
end_time = datetime.utcnow().isoformat() + "Z"
usage = response["usage"]
logger.log_event(
traceId=trace_id,
startTime=start_time,
endTime=end_time,
input={"model": model, "prompt": prompt},
usage=usage
)
Where Do Logs Go?
By default, logs are printed to stdout in newline-delimited JSON (NDJSON) format.
You can redirect output to a file:
python your_script.py > logs.jsonl
Example Output
{
"traceId": "trace_norm_01",
"startTime": "2025-07-22T10:30:05Z",
"input": {"model": "gpt-3.5-turbo", "prompt": "What are the main tourist attractions in Rome?"},
"usage": {"prompt_tokens": 10, "completion_tokens": 155, "total_tokens": 165},
"cost": 0.0002375
}
What Gets Calculated Automatically?
- total_tokens: If you provide
prompt_tokensandcompletion_tokensinusage, the logger addstotal_tokens. - cost: If you provide
model,prompt_tokens, andcompletion_tokens, the logger calculates cost using standard pricing.
Troubleshooting
- Cannot resolve host: Check your internet connection or DNS.
- pip cache issues: Try
pip install --no-cache-dir crashlens_logger - Permission errors: Use a virtual environment or add
--userto your pip command. - Module not found: Ensure you’re using the correct Python environment.
Roadmap
- Token pricing overrides
- File/DB exporters
- SDK instrumentation helpers
- Pydantic validation for log structure
Testing
Run tests with:
pytest
100% coverage on core logging logic.
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file crashlens_logger-1.0.3.tar.gz.
File metadata
- Download URL: crashlens_logger-1.0.3.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f79ca233d24e88b3dca00f7765ab28549e7dc3888d815a642ed2f1ad9799e78
|
|
| MD5 |
904943640abea4ae894dba7a14bdb491
|
|
| BLAKE2b-256 |
fea5b47fde188a9f40b04175d60d3d55ea012ef0de5dbe5184b2c1fda22fb8a5
|
File details
Details for the file crashlens_logger-1.0.3-py3-none-any.whl.
File metadata
- Download URL: crashlens_logger-1.0.3-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fffd3c2779e94c2b8a46b142d63d08b6aa082c7efb3eca280ef326873e619b6
|
|
| MD5 |
2b01b27d99051e893456edbba208f5ff
|
|
| BLAKE2b-256 |
7679ca5d5dd12bf7062c798e8c84293b5e12d858721661fe9cb01f82d979d945
|