- Automatic cost tracking: For supported integrations, Weave captures token usage from the API response and applies built-in pricing for the model, with no extra code required.
- Custom cost tracking: For fine-tuned models, self-hosted models, or models that Weave doesn’t automatically integrate with, configure Weave to track custom costs using available API methods.
The Weave TypeScript does not support cost tracking at this time.
Use automatic cost tracking
When you callweave.init() and use a supported LLM integration such as OpenAI, Anthropic, Cohere, or Mistral, Weave automatically records token usage and calculates cost for each call. Costs appear in the trace tree and in the calls table in the Weave UI, and are available programmatically under call.summary["weave"]["costs"] when you query calls with the include_costs parameter set to true.
Automatic cost tracking requires two conditions:
- The LLM provider is a supported integration.
- The API response includes token usage (most providers return this by default).
Add a custom cost
You can add a custom cost by using theadd_cost method.
The three required fields are llm_id, prompt_token_cost, and completion_token_cost.
llm_id is the name of the LLM (for example, gpt-4o). prompt_token_cost and completion_token_cost are cost per token for the LLM (if the LLM prices were specified in per million tokens, make sure to convert the value).
You can also set effective_date to a datetime, to make the cost effective at a specific date, this defaults to the current date.
Querying custom costs
You can query for costs by using thequery_costs method.
There are a few ways to query for costs, you can pass in a singular cost id, or a list of LLM model names.
Purging a custom cost
You can purge a custom cost by using thepurge_costs method. You pass in a list of cost ids, and the costs with those ids are purged.
Calculating custom costs for a project
You can calculate costs for a project by usingget_calls() with include_costs=True.