- 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 doesn’t support cost tracking.
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. They’re also 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
Use custom cost tracking when automatic cost tracking isn’t available, such as with fine-tuned models, self-hosted models, or providers that Weave doesn’t integrate with. The following sections describe how to add, query, purge, and aggregate custom costs. Add a custom cost with 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 the cost per token for the LLM. If the LLM prices are specified per million tokens, 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.
Query custom costs
After adding custom costs, you can retrieve them to verify their values or to look up the cost ID needed for other operations such as purging. Query for costs with thequery_costs method.
You can query for costs in a few ways: pass in a single cost ID, or a list of LLM model names.
Purge a custom cost
Purge a custom cost with thepurge_costs method. Pass in a list of cost IDs, and Weave purges the costs with those IDs.
Calculate custom costs for a project
To understand the total spend across a project, aggregate the per-call costs returned by Weave. Calculate costs for a project withget_calls() and include_costs=True.