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
Release history Release notifications | RSS feed
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf48ecbcf3ccf9786af1222864e7d76d319deb70ade87c17f31811f4e9fccaf6
|
|
| MD5 |
940b24a0d9d29d5f87504fdd4c4b1268
|
|
| BLAKE2b-256 |
c561378a947036bb98caf24a9b748f04a352a3300746b086835ea1f889fafe36
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sotalab_llm_metrics-0.1.0.tar.gz -
Subject digest:
cf48ecbcf3ccf9786af1222864e7d76d319deb70ade87c17f31811f4e9fccaf6 - Sigstore transparency entry: 1316809922
- Sigstore integration time:
-
Permalink:
SoTALab-ai/llm-metrics@51ed1d0e17c56811454129bc2d03d5b0b616646c -
Branch / Tag:
refs/tags/py-v0.1.0 - Owner: https://github.com/SoTALab-ai
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@51ed1d0e17c56811454129bc2d03d5b0b616646c -
Trigger Event:
push
-
Statement type:
File details
Details for the file sotalab_llm_metrics-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sotalab_llm_metrics-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
825added62dc991920862e8177fbca0dd77470bd1f06ba5824d44897efc205e9
|
|
| MD5 |
188ac377a1fd6d45618c0bddbad4ea35
|
|
| BLAKE2b-256 |
c7fd77f4732c0deec3447985e7e41bef6ecaad9093c320d531cc418af8689ac8
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sotalab_llm_metrics-0.1.0-py3-none-any.whl -
Subject digest:
825added62dc991920862e8177fbca0dd77470bd1f06ba5824d44897efc205e9 - Sigstore transparency entry: 1316809973
- Sigstore integration time:
-
Permalink:
SoTALab-ai/llm-metrics@51ed1d0e17c56811454129bc2d03d5b0b616646c -
Branch / Tag:
refs/tags/py-v0.1.0 - Owner: https://github.com/SoTALab-ai
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@51ed1d0e17c56811454129bc2d03d5b0b616646c -
Trigger Event:
push
-
Statement type: