> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-wbdocs-1882.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# カスタムコストをトラッキングする

> Weave で LLM の操作コストをトラッキングして管理する

W\&B Weave は、トークン使用量をトラッキングし、使用したモデルの料金設定を適用することで、各 LLM call のコストを自動的に計算します。これにより、トレースや評価でアプリケーションの支出を直接監視および分析できます。

また、異なる料金設定、社内のコストモデル、または Weave が自動で料金を設定しない操作のコストが必要な場合は、カスタムコストをトラッキングすることもできます。

<div id="adding-a-custom-cost">
  ## カスタムコストの追加
</div>

<Tabs>
  <Tab title="Python">
    [`add_cost`](/ja/weave/reference/python-sdk/trace/weave_client#method-add-cost) methodを使用して、カスタムコストを追加できます。
    必須フィールドは `llm_id`、`prompt_token_cost`、`completion_token_cost` の 3 つです。
    `llm_id` は LLM の名前です (例: `gpt-4o`) 。`prompt_token_cost` と `completion_token_cost` は、その LLM のトークンあたりのコストです (LLM の料金が 100 万トークン単位で指定されている場合は、必ず値を換算してください) 。
    また、`effective_date` に日時を設定すると、そのコストを特定の日付から有効にできます。デフォルトでは現在の日付が使用されます。

    ```python lines theme={null}
    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),
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```plaintext theme={null}
    この機能はまだ TypeScript では利用できません。続報をお待ちください
    ```
  </Tab>
</Tabs>

<div id="querying-for-costs">
  ## コストをクエリする
</div>

<Tabs>
  <Tab title="Python">
    [`query_costs`](/ja/weave/reference/python-sdk/trace/weave_client#method-query-costs) method を使用して、コストをクエリできます。
    コストをクエリする方法はいくつかあり、単一のコスト ID または LLM モデル名のリストを渡せます。

    ```python lines theme={null}
    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)
    ```
  </Tab>

  <Tab title="TypeScript">
    ```plaintext theme={null}
    この機能は、TypeScript ではまだ利用できません。続報をお待ちください
    ```
  </Tab>
</Tabs>

<div id="purging-a-custom-cost">
  ## カスタムコストをパージする
</div>

<Tabs>
  <Tab title="Python">
    [`purge_costs`](/ja/weave/reference/python-sdk/trace/weave_client#method-purge-costs) method を使用すると、カスタムコストをパージできます。コスト ID のリストを渡すと、それらの ID を持つコストがパージされます。

    ```python lines theme={null}
    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])
    ```
  </Tab>

  <Tab title="TypeScript">
    ```plaintext theme={null}
    この機能はまだ TypeScript では利用できません。続報をお待ちください。
    ```
  </Tab>
</Tabs>

<div id="calculating-costs-for-a-project">
  ## プロジェクトのコストを計算する
</div>

<Tabs>
  <Tab title="Python">
    少しセットアップしたうえで `calls_query` を使い、`include_costs=True` を追加すると、プロジェクトのコストを計算できます。

    ```python lines theme={null}
    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")
    ```
  </Tab>

  <Tab title="TypeScript">
    ```plaintext theme={null}
    この機能はまだ TypeScript では利用できません。続報をお待ちください！
    ```
  </Tab>
</Tabs>

<div id="setting-up-a-custom-model-with-custom-costs">
  ## カスタムコスト付きカスタムモデルの設定
</div>

詳しくは、[カスタムモデルでコストを設定する](/ja/weave/cookbooks/custom_model_cost) の cookbook をお試しください。

<Card title="Colabで試す" href="https://colab.research.google.com/github/wandb/weave/blob/master/docs/./notebooks/custom_model_cost.ipynb" icon="python" />
