カスタムコストを追加する

以下のメソッドを使用してカスタムコストを追加できますadd_cost必要な3つのフィールドはllm_idprompt_token_cost、およびcompletion_token_costです。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_costs(
    llm_id="your_model_name",
    prompt_token_cost=10,
    completion_token_cost=20,
    # If for example I want to raise the price of the model after a certain date
    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)
    # Fetch all the calls in the project
    calls = list(
        client.get_calls(filter={"trace_roots_only": True}, include_costs=True)
    )

    for call in calls:
        # If the call has costs, we add them to the total cost
        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"]

    # We return the total cost, requests, and calls
    return {
        "total_cost": total_cost,
        "requests": requests,
        "calls": len(calls),
    }

# Since we decorated our function with @weave.op(),
# our totals are stored in weave for historic cost total calculations
get_costs_for_project("my_custom_cost_model")

カスタムコストでカスタムモデルを設定する

私たちのクックブックを試してくださいカスタムモデルでコストを設定するまたは
Open In Colab
Open in Colab