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,
output_file="logs.jsonl" # <-- This will create/append to logs.jsonl
)
Where Do Logs Go?
By default, logs are printed to stdout in newline-delimited JSON (NDJSON) format.
If you provide the output_file parameter (recommended), logs will also be appended to that file (e.g., logs.jsonl).
- If the file does not exist, it will be created automatically.
- If the file exists, each new log will be appended as a new line.
You can redirect output to a file as well:
python your_script.py > logs.jsonl
But using output_file="logs.jsonl" is the most robust and portable way to persist logs.
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.
- File not created: Make sure you are passing
output_file="logs.jsonl"tolog_eventand that your process has write permissions.
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
Metadata
Release files for crashlens-logger 1.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| crashlens_logger-1.0.6.tar.gz | 10.5 kB | Without provenance | Details for crashlens_logger-1.0.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| crashlens_logger-1.0.6-py3-none-any.whl | Python 3 | none | any | Details for crashlens_logger-1.0.6-py3-none-any.whl |
Total release size: 21.0 kB
Release files / crashlens_logger-1.0.6.tar.gz
| Download URL | crashlens_logger-1.0.6.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1eb2ea3b5a721fbee4bd9f03f4b12eca7002abe10b2a7c699b64fed43c432342
|
|
BLAKE2b-256 checksum How to use checksums |
6bd1edee9a0fccfe45b890793dc60f04db31e4aa9c13eedb676be754c0772c38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.12.10 Windows/11
|
Release files / crashlens_logger-1.0.6-py3-none-any.whl
| Download URL | crashlens_logger-1.0.6-py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
514731b1e93c8abc437b21cde0c8e593ce526b1599262a93bf4f56bd87af5f65
|
|
BLAKE2b-256 checksum How to use checksums |
65d49788ab983fbb601907a2abeec7c6e430e1db19e966ecae570434c066a365
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.12.10 Windows/11
|
Security for crashlens-logger 1.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.