Skip to main content

Thin wrapper around posthog-python's AI integrations. Auto-tags LLM calls with project/service/env for unified cost dashboards.

Project description

sotalab-llm-metrics

SoTALab 内部 LLM 成本追踪 SDK(Python)。基于 posthog-python,自动按 project/service/env/model 维度上报调用成本到 PostHog,内置价格表(含 cache tokens、Gemini 分段定价)。

安装

pip install sotalab-llm-metrics
# 或按需带上 provider extras
pip install 'sotalab-llm-metrics[openai,anthropic]'

快速使用

Provider 包装(适合单一 SDK client 生命周期)

from llm_metrics import create_anthropic, create_openai, shutdown

anthropic = create_anthropic()
openai = create_openai()

anthropic.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "hi"}],
)

shutdown()

track_generation(适合多账号轮转、自定义重试等场景)

from llm_metrics import init, track_generation

init()

# 自己调完 LLM 后手动上报
track_generation(
    model="gpt-4o",
    provider="azure",
    input_tokens=150,
    output_tokens=250,
    latency_ms=1200,
    properties={"account": "azure-02", "job_type": "generate-response"},
)

配置(环境变量)

变量 必填 说明
POSTHOG_HOST PostHog 实例 URL
POSTHOG_PROJECT_API_KEY PostHog Project API Key
LLM_METRICS_PROJECT 项目名
LLM_METRICS_SERVICE 服务名
LLM_METRICS_ENV 环境标签(默认 development)
LLM_METRICS_GIT_SHA 部署版本,便于回归排查

文档

完整文档、架构说明、事件字段契约见 仓库根目录 README

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

sotalab_llm_metrics-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

sotalab_llm_metrics-0.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file sotalab_llm_metrics-0.1.0.tar.gz.

File metadata

  • Download URL: sotalab_llm_metrics-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sotalab_llm_metrics-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf48ecbcf3ccf9786af1222864e7d76d319deb70ade87c17f31811f4e9fccaf6
MD5 940b24a0d9d29d5f87504fdd4c4b1268
BLAKE2b-256 c561378a947036bb98caf24a9b748f04a352a3300746b086835ea1f889fafe36

See more details on using hashes here.

Provenance

The following attestation bundles were made for sotalab_llm_metrics-0.1.0.tar.gz:

Publisher: publish-pypi.yml on SoTALab-ai/llm-metrics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sotalab_llm_metrics-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sotalab_llm_metrics-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 825added62dc991920862e8177fbca0dd77470bd1f06ba5824d44897efc205e9
MD5 188ac377a1fd6d45618c0bddbad4ea35
BLAKE2b-256 c7fd77f4732c0deec3447985e7e41bef6ecaad9093c320d531cc418af8689ac8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sotalab_llm_metrics-0.1.0-py3-none-any.whl:

Publisher: publish-pypi.yml on SoTALab-ai/llm-metrics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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