This is an interactive notebook. You can run it locally or use the links below:
Set up a custom cost model
This guide shows you how to define a custom model in Weave that reports its own token usage, register a custom per-token cost for that model, and then retrieve calls with cost information attached. Use this when you’re working with a model that isn’t covered by Weave’s built-in cost data and you want to track spending alongside your traces. Weave calculates costs based on the number of tokens used and the model used. Weave grabs this usage and model from the output and associates them with the call. Set up a simple custom model that calculates its own token usage and stores that in Weave.Set up the environment
Install and import all needed packages. SetWANDB_API_KEY in your environment so that you can log in with wandb.login() (this should be given to the colab as a secret).
Set the project in W&B you want to log this into in name_of_wandb_project.
name_of_wandb_project can also be in the format of {team_name}/{project_name} to specify a team to log the traces into.weave.init().
Set up a model with Weave
Next, define a WeaveModel subclass that performs its own token accounting. Returning the usage counts and model name in the output dictionary is what lets Weave later associate the call with the custom cost defined in the next section.
Add a custom cost
Add a custom cost. Once you have added a custom cost and your calls have usage, you can fetch the calls withinclude_cost and review the costs under summary.weave.costs.
llm_id, and a way to retrieve calls with per-call cost data attached under summary.weave.costs.