Skip to main content

LLM Usage Framework

A generic, provider-independent Python framework for tracking LLM usage and collecting standardized telemetry.

Features

  • Multiple LLM provider support
  • Standardized LLMTracker interface
  • Input, output, and total token tracking
  • Cache usage tracking when reported by the provider
  • Token source tracking
  • Character count tracking
  • Latency tracking
  • Provider and model tracking
  • Project, service, feature, and metadata support
  • Request ID and session ID tracking
  • Optional tokenizer fallback
  • Optional on_usage callback
  • LLMConfig support
  • Provider-specific parameters through provider_kwargs
  • Storage-agnostic
  • No database dependency

Supported Providers

Provider Status
Ollama Supported
OpenAI Supported
Anthropic Supported
AWS Bedrock Supported
Google Gemini Planned

Installation

Install the core package:

pip install llm-usage-framework

Usage

Basic Example (Ollama)

Here is a simple example of how to use the LLMTracker with the Ollama provider:

from llm_usage.tracker import LLMTracker

# Initialize the tracker
tracker = LLMTracker(
    provider="ollama",
    model="qwen3:8b",
    project="my_project",
    service="my_service",
    provider_kwargs={
        "base_url": "http://localhost:11434",
    },
)

# Generate a response
result = tracker.generate(
    prompt="Say hello in one sentence.",
)

# The result contains standardized usage metrics
print(result.text)
print(f"Input tokens: {result.usage.input_tokens}")
print(f"Output tokens: {result.usage.output_tokens}")
print(f"Total tokens: {result.usage.total_tokens}")
print(f"Latency: {result.latency_ms} ms")

Usage with OpenAI

You can easily switch to a different provider by changing the initialization parameters:

from llm_usage.tracker import LLMTracker
import os

tracker = LLMTracker(
    provider="openai",
    model="gpt-4",
    project="my_project",
    service="my_service",
    provider_kwargs={
        "api_key": os.getenv("OPENAI_API_KEY"),
    },
)

result = tracker.generate(
    prompt="Explain quantum computing in simple terms.",
)

print(result.text)
print(f"Usage: {result.usage.total_tokens} tokens")

Usage with AWS Bedrock

You can also use AWS Bedrock by specifying the model and passing the region_name in the provider kwargs. Ensure you have your AWS credentials configured (e.g., via environment variables or the ~/.aws/credentials file):

from llm_usage.tracker import LLMTracker

tracker = LLMTracker(
    provider="bedrock",
    model="amazon.nova-micro-v1:0",
    project="my_project",
    service="my_service",
    provider_kwargs={
        "region_name": "us-east-1",
    },
)

result = tracker.generate(
    prompt="What is cloud computing?",
)

print(result.text)
print(f"Usage: {result.usage.total_tokens} tokens")

Usage with LLMConfig

You can also encapsulate your configuration using the LLMConfig object. This is especially useful when you need to pass the configuration across different parts of your application:

from llm_usage.tracker import LLMTracker
from llm_usage.config import LLMConfig

# Define the framework configuration
config = LLMConfig(
    provider="anthropic",
    model="claude-3-haiku-20240307",
    project="my_project",
    service="my_service",
    provider_kwargs={
        "api_key": "YOUR_ANTHROPIC_API_KEY",
    },
)

# Initialize the tracker with the config
tracker = LLMTracker(config=config)

result = tracker.generate(
    prompt="Write a short poem about the ocean.",
)

print(result.text)
print(f"Usage: {result.usage.total_tokens} tokens")

Metadata

Release files for llm-usage-framework 0.2.0

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

Source distribution (sdist)

Source distribution for llm-usage-framework 0.2.0
File Size Uploaded Provenance Actions
llm_usage_framework-0.2.0.tar.gz 21.9 kB Without provenance Details for llm_usage_framework-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llm-usage-framework 0.2.0
File Interpreter ABI Platform Actions
llm_usage_framework-0.2.0-py3-none-any.whl Python 3 none any Details for llm_usage_framework-0.2.0-py3-none-any.whl

Total release size: 40.4 kB

Release files / llm_usage_framework-0.2.0.tar.gz

Download URL llm_usage_framework-0.2.0.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0e55376bad1723864a377aefee4e3eeaff79f74ab79b9061308ef52f19baee72
BLAKE2b-256 checksum
How to use checksums
ba56f4b2cd5e9ce7fb932cda3d40786bb3801ccedf28c65947e0fec663e0e9cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / llm_usage_framework-0.2.0-py3-none-any.whl

Download URL llm_usage_framework-0.2.0-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff32a7d0c55c393b2612ebbacc57581c639f09685e1d1703bb1de021e34768f2
BLAKE2b-256 checksum
How to use checksums
2004fee4f9e3f73d3c4ad3754bc239649410fffd5fc6d0cc00079bfe90a1e83e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for llm-usage-framework 0.2.0

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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

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