litellm 计费模型处理
以下简单说明下litellm 对于计费的处理(使用了内置模式,当然litellm 也是可以通过callback 集成外部系统进行计费的)
参考日志信息 (默认配置)
- 比如集成internlm2 的日志信息
18:05:05 - LiteLLM:ERROR: litellm_logging.py:1265 - Model=internlm2 not found in completion cost map. Setting 'response_cost' to None
Run time of job "ProxyConfig.add_deployment (trigger: interval[0:00:10], next run at: 2024-07-04 18:05:09 CST)" was missed by 0:00:05.679351
Run time of job "update_spend (trigger: interval[0:00:07], next run at: 2024-07-04 18:05:07 CST)" was missed by 0:00:04.679686
解决方法
快速的解决方法就是自定义计费模式,可以通过ui 配置
- 如下图
之后的请求就会使用自定义的计费模型
调用计费效果
接口调用
litellm 的功能还是很方便了,简化了不少llm api 的处理,提供了统一的openai api ,但是我们还是需要进行计费处理api 的调用,目前swagger 接口中只显示了全局的,对于特定api key 以及模型上的统计的目前看不到,实际上接口调用与实际的是一样的
- api key 计费统计
参考调用
curl 'http://localhost:4000/global/spend/keys?limit=5' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json'
效果
[
{
"api_key": "4a232c5264640946b61ec32fb3304278af1a34cc26482066aa66e250cddde75e",
"key_alias": "internlm2",
"key_name": "sk-...ZeoQ",
"total_spend": 28.4
}
]
- 直接基于数据库的查询
select * from "MonthlyGlobalSpendPerKey" a join "LiteLLM_VerificationToken" b on a.api_key=b.token
注意
MonthlyGlobalSpendPerKey 是一个视图,ddl 如下
CREATE VIEW "MonthlyGlobalSpendPerKey" AS SELECT date("LiteLLM_SpendLogs"."startTime") AS date,
sum("LiteLLM_SpendLogs".spend) AS spend,
"LiteLLM_SpendLogs".api_key
FROM "LiteLLM_SpendLogs"
WHERE "LiteLLM_SpendLogs"."startTime" >= (CURRENT_DATE - '30 days'::interval)
GROUP BY (date("LiteLLM_SpendLogs"."startTime")), "LiteLLM_SpendLogs".api_key;
还有一个LiteLLM_VerificationTokenView的视图可以关联其他信息的基于了LiteLLM_VerificationToken 表
CREATE VIEW "LiteLLM_VerificationTokenView" AS SELECT v.token,
v.key_name,
v.key_alias,
v.soft_budget_cooldown,
v.spend,
v.expires,
v.models,
v.aliases,
v.config,
v.user_id,
v.team_id,
v.permissions,
v.max_parallel_requests,
v.metadata,
v.tpm_limit,
v.rpm_limit,
v.max_budget,
v.budget_duration,
v.budget_reset_at,
v.allowed_cache_controls,
v.model_spend,
v.model_max_budget,
v.budget_id,
t.spend AS team_spend,
t.max_budget AS team_max_budget,
t.tpm_limit AS team_tpm_limit,
t.rpm_limit AS team_rpm_limit
FROM "LiteLLM_VerificationToken" v
LEFT JOIN "LiteLLM_TeamTable" t ON v.team_id = t.team_id;
说明
使用好litellm 的确可以减少不少大模型开发的一些问题,还是很值得使用下的
参考资料
litellm/litellm_core_utils/litellm_logging.py
litellm/cost_calculator.py
litellm/proxy/spend_tracking/spend_management_endpoints.py
https://github.com/BerriAI/litellm/issues/4372