268 points · by Areibman · 2y ago · 76 comments
Tokencost works by counting the number of tokens in prompt and completion messages and multiplying that number by the corresponding model cost. Under the hood, it’s really just a simple cost dictionary and some utility functions for getting the prices right. It also accounts for different tokenizers and float precision errors.
Surprisingly, most model providers don't actually report how much you spend until your bills arrive. We built Tokencost internally at AgentOps to help users track agent spend, and we decided to open source it to help developers avoid nasty bills.
As far as I know Anthropic haven't released the tokenizer for Claude - unlike OpenAI's tiktoken - but your tool lists the Claude 3 models as supported. How are you counting tokens for those?
At this moment, Tokencost uses the OpenAI tokenizer as a default tokenizer, but this would be a welcome PR!
I've been bugging Anthropic about this for a while, they said that releasing a new tokenizer is not on their current roadmap.
It looks like tiktoken is the default for most of the methods.
Disclaimer: I didn't fully trace which are being used in each case/model.
There are no cases for Claude models yet.
I wonder if anyone has run a bunch of messages through Anthropic's API and used the returned token count to approximate the tokenizer?
Maybe a small triage AI to decide how effectively models handle certain prompts to preserve spending for the difficult tasks.
Does anything like this exist yet?
In that case you might as well make ~20 API calls to each LLM under consideration, and evaluate the results yourself.
It's far easier to evaluate a model's performance on a given prompt by looking at the output than by looking at the input alone.
Would love to hear what you had in mind.
Most can do 2+2 = 4.
One test prompt I use on LLMs is asking it to produce a JavaScript function that takes an ImageData object and returns a new ImageData object with an all direction Sobel edge detection. Quite a lot of even quite small models can generate functions like this.
In general, I don't even think this is a question that needs to be answered. A lot of API providers have different quality/price tiers. The fact that people are using the different tiers should be sufficient to show that at least some people are finding cases where cheaper models are good enough.
Top story picker. Given a bunch of news stories, pick which one should be the lead story.
Data viz color picker. Given a list of categories for a chart, return a color for each one.
Windows Start menu. Given a list of installed programs and a query, select the five most likely programs that the user wants.
All in all this is something I was looking for or was roughly going to do to compare costs. Cool stuff.
Here's actual output from the GPT-4o tokenizer for English and Hindi:
>>> [enc.decode([x]) for x in enc.encode("proselytization")]
['pros', 'ely', 't', 'ization']
>>> [enc.decode([x]) for x in enc.encode("पर्यावरणवाद")]
['पर', '्य', 'ावरण', 'वाद']