メインコンテンツへスキップ

カスタムコストの追加

add_cost メソッドを使ってカスタムコストを追加できます。 必須フィールドは 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 メソッドを使用してコストを取得できます。 コストを問い合わせる方法はいくつかあり、単一のコスト 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 メソッドを使用してカスタムコストを削除できます。コスト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)
    # プロジェクト内のすべてのコールを取得
    calls = list(
        client.get_calls(filter={"trace_roots_only": True}, include_costs=True)
    )

    for call in calls:
        # コールにコスト情報があれば、合計コストに加算する
        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"]

    # 合計コスト、リクエスト数、コール数を返す
    return {
        "total_cost": total_cost,
        "requests": requests,
        "calls": len(calls),
    }

# 関数に @weave.op() を付けているので、
# これらの合計値は履歴コストの集計に使えるように weave に保存される
get_costs_for_project("my_custom_cost_model")

カスタムコストを設定したカスタムモデルのセットアップ

カスタムモデルでコストを設定するためのクックブックを参照してください。

Colab で試す