メインコンテンツへスキップ
W&B Weave は、トークン使用量をトラッキングし、使用したモデルの料金設定を適用することで、各 LLM call のコストを自動的に計算します。これにより、トレースや評価でアプリケーションの支出を直接監視および分析できます。 また、異なる料金設定、社内のコストモデル、または Weave が自動で料金を設定しない操作のコストが必要な場合は、カスタムコストをトラッキングすることもできます。

カスタムコストの追加

add_cost methodを使用して、カスタムコストを追加できます。 必須フィールドは llm_idprompt_token_costcompletion_token_cost の 3 つです。 llm_id は LLM の名前です (例: gpt-4o) 。prompt_token_costcompletion_token_cost は、その LLM のトークンあたりのコストです (LLM の料金が 100 万トークン単位で指定されている場合は、必ず値を換算してください) 。 また、effective_date に日時を設定すると、そのコストを特定の日付から有効にできます。デフォルトでは現在の日付が使用されます。
import weave
from datetime import datetime

client = weave.init("my_custom_cost_model")

client.add_cost(
    llm_id="your_model_name",
    prompt_token_cost=0.01,
    completion_token_cost=0.02
)

client.add_cost(
    llm_id="your_model_name",
    prompt_token_cost=10,
    completion_token_cost=20,
    # たとえば、ある日付以降にモデルの価格を引き上げたい場合
    effective_date=datetime(2025, 4, 22),
)

コストをクエリする

query_costs method を使用して、コストをクエリできます。 コストをクエリする方法はいくつかあり、単一のコスト ID または LLM モデル名のリストを渡せます。
import weave

client = weave.init("my_custom_cost_model")

costs = client.query_costs(llm_ids=["your_model_name"])

cost = client.query_costs(costs[0].id)

カスタムコストをパージする

purge_costs method を使用すると、カスタムコストをパージできます。コスト ID のリストを渡すと、それらの ID を持つコストがパージされます。
import weave

client = weave.init("my_custom_cost_model")

costs = client.query_costs(llm_ids=["your_model_name"])
client.purge_costs([cost.id for cost in costs])

プロジェクトのコストを計算する

少しセットアップしたうえで calls_query を使い、include_costs=True を追加すると、プロジェクトのコストを計算できます。
import weave

weave.init("project_costs")
@weave.op()
def get_costs_for_project(project_name: str):
    total_cost = 0
    requests = 0

    client = weave.init(project_name)
    # プロジェクト内のすべての call を取得します
    calls = list(
        client.get_calls(filter={"trace_roots_only": True}, include_costs=True)
    )

    for call in calls:
        # call にコストがある場合は合計コストに加算します
        if call.summary["weave"] is not None and call.summary["weave"].get("costs", None) is not None:
            for k, cost in call.summary["weave"]["costs"].items():
                requests += cost["requests"]
                total_cost += cost["prompt_tokens_total_cost"]
                total_cost += cost["completion_tokens_total_cost"]

    # 合計コスト、requests、calls を返します
    return {
        "total_cost": total_cost,
        "requests": requests,
        "calls": len(calls),
    }

# 関数を @weave.op() でデコレートしているため、
# 過去のコスト合計を計算できるように、合計値は weave に保存されます
get_costs_for_project("my_custom_cost_model")

カスタムコスト付きカスタムモデルの設定

詳しくは、カスタムモデルでコストを設定する の cookbook をお試しください。

Colabで試す