Skip to main content

CLI tool to generate structured LLM logs for CrashLens cost detection

Project description

CrashLens Logger 🧠💸

Structured Token & Cost Logs for OpenAI / Anthropic Usage

PyPI version MIT License

⚠️ 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_tokens and completion_tokens in usage, the logger adds total_tokens.
  • cost: If you provide model, prompt_tokens, and completion_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 --user to 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

crashlens_logger-1.0.2.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crashlens_logger-1.0.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file crashlens_logger-1.0.2.tar.gz.

File metadata

  • Download URL: crashlens_logger-1.0.2.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for crashlens_logger-1.0.2.tar.gz
Algorithm Hash digest
SHA256 4e8b25d7c118aa7f47cd3aa6f792ca35b39d7d3b02356c327d2e72a7022a73eb
MD5 aa31eb0af2eb67579ecce23f70d9d79b
BLAKE2b-256 5ec3e007e771e90c9b9e46f867b68fc996555f6314bf13e416324df98e044c82

See more details on using hashes here.

File details

Details for the file crashlens_logger-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for crashlens_logger-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9f8ea8de000994bba9b54ffabc670d08bb37dd7f81f768e7f285e10001845019
MD5 31b6bf96a1eceffb6e77441016e27d36
BLAKE2b-256 6550850eeca26e24f9452f62686e70cecba16d66ce1b140d529b452d42418b18

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page