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。
Metadata
Release files for sotalab-llm-metrics 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sotalab_llm_metrics-0.1.0.tar.gz | 8.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sotalab_llm_metrics-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.6 kB
Release files / sotalab_llm_metrics-0.1.0.tar.gz
| Download URL | sotalab_llm_metrics-0.1.0.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cf48ecbcf3ccf9786af1222864e7d76d319deb70ade87c17f31811f4e9fccaf6
|
|
BLAKE2b-256 checksum How to use checksums |
c561378a947036bb98caf24a9b748f04a352a3300746b086835ea1f889fafe36
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 16, 2026.
Transparency logRelease files / sotalab_llm_metrics-0.1.0-py3-none-any.whl
| Download URL | sotalab_llm_metrics-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
825added62dc991920862e8177fbca0dd77470bd1f06ba5824d44897efc205e9
|
|
BLAKE2b-256 checksum How to use checksums |
c7fd77f4732c0deec3447985e7e41bef6ecaad9093c320d531cc418af8689ac8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 16, 2026.
Transparency log