Why AI Tokens are so Expensive - Computerphile
Transcript
Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.
I thought given how much it's been in the news about costs of AI and you know agentic coding and how much you pay per month and all this stuff I thought we could look at what is a token and why is it so unbelievably expensive. This is happening everywhere. So for example, Anthropic have more caps on for their premium users. You might hit a cap where you have to go and wait for a while sooner than you used to. for the the famous recent one was the GitHub co-pilot change which changed from a requestbased monthly cost to a AI token
credit monthly cost which has substantially reduced the amount you can do per month and we can talk about why that is and why essentially it was inevitable given how expensive it is to run those agentic coding models.
Well, you could call this video what is a token but we're going to answer that straight away. Right? The token is either a word or a piece of a word. That is it. Um it's not an interesting question. Right? So if I write uh the cat sat on the mat, you might
intuitively if you're a person split this up per word, but we don't incorporate spaces kind of in our head. We just use them to find the words. Large language models and modern AI will use the spaces as well. Full stops will be tokens. Curly brackets will be tokens. If you have Chinese characters, they might be one or two tokens, right? Because they're more complicated. So, you might have, you know, this might be a token and then this might be a token with the space included. And, you know, you might have that. I'm just making this up, right? You know, something like that. The exact split will depend on who's running the model and the tokenizer they're using. They're based around this idea of frequency. So, for example, because the is so common on its own, it would make sense for it to be a token. A modern language model will have access to let's say a 100,000 of these tokens which will include standard British things but also other languages and code symbols and special unic code characters and things like this. Right?
So most of the tokens will be in a small subset of a few thousand that get used most of the time but there'll be many more that it could produce if the if the need arose. >> But they haven't been decided they if they emerged out of >> No, they've been decided. been decided. The tokenizer is essentially a string parer that goes in front and at the end of a large language model. What happens when these go through a network is these get turned into a numerical representation called an embedding. Which means that this one will have some highdimensional vector of numbers that represents what the word the means, right? Not a lot. And cat will be different and at will be different and so on. That part is learned. So that part will be bespoke for whichever large language model is being used. But they probably use whichever tokenizer they've always used.
>> So we did a video a while back with Lewis where we did, I don't know, Mike in a Nike on a hike. >> Mike on a bike. >> Yeah. >> A bike on a mic. [laughter] >> Mike at a strike. >> Yeah. Different tokens. >> But they're different tokens, but they would be in some senses in similar space. >> Similar words will be in similar space probably, right? It will come out of a training process. it makes sense that they would be um the in some sense the
tokenization part is is almost a sort of system development issue. One thing it is interesting is you would think well why would you why would you have a 100,000 tokens when you're writing let's say JavaScript because JavaScript only uses a subset of the you know lots of them aren't valid tokens after a an if statement there's only a certain number of tokens you can you can predict otherwise it doesn't make sense that's true but part of the power of these models is their huge size and their massive pre-training and so it actually isn't particularly cost effective necessarily to have a model that's only trained on code what you tend to do is refine a model that's trained on everything else. So you will just have all the tokens included that could do JavaScript or they could write English poems and then later you will refine that model into a specific domain by giving it extra examples >> that presumably helps with people prompting it as well asking it to do things for them.
>> Yes. And actually you can ask these models to do things in B 64 and they will pretty much do it right because to it it's just input tokens. This isn't in some sense this isn't the interesting part. The interesting part is is when you talk to a model, how much does it cost? Because that's what we all want to know. And the answer is right, the answer is we're not sure. But actually, we can come up with some guesses. These models work in something called an auto reggressive way. And what that means is that they put they they take in this input. They look at the whole input as a big list. They make a lot of decisions and then they output a single next token. So if I gave it the input the cats at on the right these tokens here it would with high likelihood return as its next prediction this token here and that's true of code and all other kinds of things. If I write half an if statement and I ask it to complete it for me it will use the rest of a context and its pre-training to produce likely output tokens. The issue is and the thing that perhaps a lot of people haven't quite certainly you know in in the media hasn't grasped and in in sort of standard audience hasn't grasped is is how unbelievably inefficient that is [clears throat] right so when you ask me a question and I answer it what I don't do is quickly reread the whole transcript of this previous conversation in order to answer it I suppose I might if I've completely forgotten what I'm saying but in general I can hold some things in my head and then I can answer the question language models don't work that way everything goes in every single time. So when you want to put the next
statement in and the next statement you're putting in this then you're putting in this then you're putting in this and it gets bigger and bigger and bigger and and that comes at a cost. It
comes at a particular cost for coding agents where the size of this input goes up hugely and that's I think where it gets quite interesting and the costs become a little bit confusing. Let's imagine we we're we're just on a website talking to a to a um a large language model. In in some sense, it's not a very difficult question, right? I ask it 500 tokens worth of question. It produces some thousand tokens worth of thought process, which is the bit it writes before it answers, but may or may not be seen by the user. And then maybe it outputs a 500 token response. So the actual whole token cost in input tokens is is is my my input. Um, I mean, you
know, let's let's let's look at a concrete example. You might have a system prompt, which let's say is a thousand tokens. Conservatively, they'd be much bigger than this, right? >> And that's things like you are an AI agent. >> Yeah. Don't tell them what the system prompt is. Don't answer them rude, don't answer rude questions. This kind of stuff. Um, you know, and the current time and the operating system you're on and maybe some actual user specific stuff. You then have the user query, which let's say is 100 tokens. It's quite a short question. Then you have the thought which is the output tokens that it produced in response to these two things added together. So the actual input to the model was 1,100 tokens. And then for the first thought
word, the output then goes back in. So
now it's system plus user query plus the first word of the thought. So it's 1,11 tokens and then 1,12. And this loops over and over again. Oh, so like you said when it's predicting the word Matt on the cat sat on the after Matt, you
have to have the cat sat on the mat again to work out what comes. Yes. Yes. You don't just say you said Matt, what's the next word? You have to say don't forget all this other stuff that came in first. Right now, we'll talk in a minute about caching because that's a big thing for speed ups and efficiency. But you know that's basically what happens. So if this thought process is 10,000 tokens, I asked a complicated but brief question, right? and then its response is let's say 1,000 tokens. There's a lot going on because the actual output was what was 10,000 and another thousand. So 11,000 tokens, right? And the input was symbolically 1,100 tokens. But that's not how it works. It works on a loop.
And so actually it ran a forward pass through a GPU 11,000 times, right? And
each time the context in the GPU got slightly bigger, right, with the previous thing, which is why it costs so much electricity to run these models, right? Because you don't just say, "Here's your output sentence." You say, "Here's the first word of your output sentence. Let's go again." Now, it's not quite as inefficient as this because I'm being a bit unfair, right? Because actually what they do is something called KV caching, which I think probably we'll leave to another video. But ultimately what happens is if you're adding the word map to this sentence and then you add another word let's say the word to right new sentence. So this is a new token. When you actually put this through the network to predict the next word we don't have to calculate the relationship between cat and at and cat and on because we already did that the previous go. So we cache those values.
So a lot of the intermediate representations in the network are stored as you go so that when you add another one you don't have to completely do everything from scratch which makes a huge difference for very very big context windows for this sentence it makes almost no difference at all but if you've got a million documents you're putting in you're trying to find the best bit of each one and your context window is full up and it's all gone. This will this will help. Of course there's an implementation question of how long do you keep this cache live for? If I ask a long question of a large language model, it caches all of these values and then I go off for a coffee and come back later, it's got to have dropped that from the GPU because it's got other feed people to to talk to, right? And so in actual fact, it will then have to put all of those back in again and that's called pre-filling.
It's almost a system design issue, right? That the more aggressively you can cache some of these things, the better your performance will be to the point where it becomes it's not helpful, right? or the user is too slow asking questions and you've had to get rid of the cash cuz they have a very short time to life, time to live, right? >> So yeah, obviously cats sit on the mat all the time. >> Yeah, not a huge amount of um tokens in use in that example for the kind of and I'm imagining kind of a chatbot or something doesn't seem to be using a lot of tokens. Where does this you know what's the deal with all this cost there? Everyone's very cross about the cost, but they're mostly cross about the cost of coding agents, not them going online and chatting to a chatbot with simple questions like which what did the cat sit on? And the issue is that this scales up very very quickly, right? And we're going to talk about that with an a very simple example. But the thing to remember is whether you're caching it or not caching it, every time you go forward through a network, everything is in there. And if you then wait a few minutes and you go in again, right, and you you do or don't get cash hits, this gets longer. Suppose I ask a a follow-up question to this, right? Well, in order to answer that, it has to be able to read the original query, the thoughts, and the response, which is some 11,100 tokens, and my new query, which is another 200 tokens. So, that input is bigger than the one before. The input to the one before was 1,100 tokens and a bunch of output. Now, it's nearly 11,000 tokens. And then I ask another question, and now it's 20,000 tokens. And I ask another question, it's 30,000 tokens. or it goes and reads a file which itself is 50,000 tokens and now every time I ask a question it has to have this file in the context as well which adds 50,000 to every single iteration that's when the
price gets very very big very very quickly and agents coding agents are particularly good at scaling this cost up very very quickly right and so this is what's been so interesting in the last few months where the cost of these has kind of become more known and Some
companies are switching to what I might sort of call a slightly more realistic costing model. But let's see. Let's think about the difference between a chatbot and a coding agent. A coding agent has more autonomy. So what happens is it has access I can talk to it but it also has access to my files and it can make what we call tool calls. A tool call is just instead of outputting directly to me, it outputs to the system and says I would like to do this thing. I would like to read this file. I would like to read the specific lines of this file. I would like to delete this file.
The system then goes off and does that thing. Maybe it asks for permission first, maybe it doesn't. And then and then the model continues to output maybe to me directly or maybe a code change. And so the reason we call it an agent is because it's going off and kind of doing this thing. I'm not responsible for here are five files that I've pasted in the chat. Please can you give me this bug fix? It is there is a bug. Can you go off and find it in these five files and and action the response? Right? That's the different thing which as a programmer would be a lot easier, right?
You don't want to have to paste everything in the context window to begin with. So the problem the problem is that it's the same sense of scaling up as we had before. Most of the cost of these models are measured in input tokens and output tokens. So the most of
the modern providers will charge you somewhere around $2 to $3 for a million input tokens and something like $15 for a million output tokens. They are what have you sent into the model and what streaming text did it send out. So if I put in the cats at in the and it sends out Matt, I've only got I've only been charged for one output token which is kind of cheap. Unfortunately, coding models don't only write the word Matt. So let's imagine that I fire up a coding
agent. So there's loads of them, right? But I I um I was recent, we'll show you in a moment, but I was having a play around with GitHub Copilot on the command line and the I ask it a
question. Uh hang on a minute. I've gone into you know, Word and I've produced a bunch of boxes. I I basically I don't know if you remember back to our DRA video where we had little cards. I thought that might be quite a nice way to visualize this. So I've got myself a system prompt here of some unspecified number of tokens. This the the system prompt is, you know, the instructions that the the manufacturer has given it, right? you are a GitHub co-pilot coding agent and you must solve Mike's problems cuz he doesn't want to do it for himself. So, we put in the system prompt always and then I ask it a question. So, my question might be or a programmer's question might be, I've got a bug in my authentication code. When someone types a certain thing, it crashes. Please go and find this bug and fix it for me.
Right? So, the system prompt is, let's say, 4,000 tokens. I never see them but they are there and my my my query is let's say a nice modest 200 tokens so the input cost
to the model in terms of input tokens is 4,200 tokens right which I'm going to write uh let's go with another color right so 4,200 tokens now there's a very good chance I'm going to do my maths wrong but we'll keep going now what does what's the first thing that a coding agent will do is it will have a good think to itself right it will produce some thought text so it it outputs um it outputs its initial thoughts. The user has asked this. I've reminded myself I'm a coding agent. Maybe I should do this or perhaps I could do this. And it turns away for a while and maybe produces let's say 2,000 output tokens. So they cost my output, but I've only paid for 4,200 input tokens. So so far so good, right? This has not cost me a huge amount of money yet. You break brace yourself. Right now, in response to thought number one,
it decides it's going to make a tool call. If it's going to actually answer the question, it needs to look at the file. That's the problem. And so, what it does is it makes a tool call and it's going to say, I'd like to read file one. So, let's say index.html or login.php or something JavaScript, right? And that is a very short output. So, that's maybe 100 tokens. It's just a small bit of structured JSON between some special characters that tells the system, read that file for me. It then the system does that and dumps the file contents into the chat, right? Like this. You won't see that file contents because it will be hidden from the user, but it's in there. And that file is is not small.
So that file is maybe 5,000 tokens,
right? So now we have a problem because now the large language model has to read this file and decide where the bug is and what it has to do. So this is now an
input of 4 9 11 11,300 tokens, right?
11,300 tokens. Not too bad, right? But that's in addition to the 4,200 I've already paid for, right? Which was just its initial thinking and a tall call. Okay, let's keep going. So, it thinks for a while longer, right? Like this. And that's going to be another 2,000 tokens. Now, one of the interesting things about large language models is the thoughts can be very, very short or they can be incredibly long. And there are different models that do different things. It's difficult to know going in. They might get stuck in a loop and think for ages and achieve nothing or they might be very, very succinct. Right? One of the lovely unknowns. So in response to tool called thought number two, it's going to make another file read of something else.php or database.py or whatever. I'm literally making up file names. Um why they're all programmed in different languages is someone else's cuz I wrote it. It the system will return the file contents. So the tool call was maybe another 100 tokens and the file contents
is perhaps another uh 4,000 tokens. Now
this goes back into the model and it has to make a decision on what to do in terms of fixing the code. Right? So this is now 11,300 plus another 2,000. So 13,300 infinity
17,400 tokens, right? 17,400
input tokens. I And I've not even got my answer yet, right? Um, okay. It's getting a bit grim, but all right, let's keep going. So, now it thinks to itself again. Why wouldn't it, right? And I'm running out of page. I'm going to go like that. Look at that. Uh um, so this thought is another 2,000 tokens. It's just just a guess, right? It doesn't really matter for this example. It finally comes up with a solution. It's read the files. It knows what the code fix is. It knows what my query was. And it's done it in it's thought through the logic of it in the thinkings uh thinkings right and so it
finally makes a tool call to actually code patch the code which is done kind of using a diff it says take out these lines of code add these lines of code right and maybe that's 1500 tokens right maybe I'm being a bit harsh but let's say it's a big big code change my code is not good let's just recap so the initial query that went to the model was
these two >> right like this the next query that goes to the includes that information cuz it has to that's in the original query. You can't just read the file and then go well I forgotten what I was meant to do with it. So so that is this bit it takes this it it does some thinking it asks for a tool call. Those are output tokens and then it gets 5,000 tokens back. So these add up to 11,300 in addition to the 4,200 you already paid for. Then it thinks again, it makes another tool call and it outputs some file contents. And the next query is 17,400, right? In response to which it finally starts coming up with some answers. It says, "Okay, here's for three, that's 2,000 tokens. Here's the code patch, 1500 tokens." And that returns a result that's a success of, let's say, 50 tokens that goes, "Yeah, we've successfully successfully done that,
right?" And that means that the final
the final prompt that goes through this model is this one, which is 17,400 plus
another 2,000. So 19,400. 20,900 plus another 50 is 20,950
tokens, right? Input and some thousands of output tokens in the various thoughts, tool calls. And of course, what it also then does is produce a final response that says, "I've done a really good job. That's also why not another 500 tokens. I've run out of paper. There we are." So, we asked it to
do a bug fix. It read two files. It thought to itself a few times in between these steps. And then it made a patch, right? And it fixed my code, let's say. And in doing so, it's cost me 20,000
plus nearly 20,000 is 40,000 plus another 10 plus another five is somewhere around 55 to 60,000 tokens and a few thousand tokens of output as well. And this is a very very simple request where it's only read two files and just answered the question. Right now suppose this doesn't work. I say now you broke it again and I put in another user query down here. this gets shipped in and this is going to start getting big very very quickly. So let's look at a real example of a fun bit of code that I wrote using GitHub Copilot to show how big this can get. You know, I've always wanted to code and could never be bothered was was was a classic Windows 3.11 uh starfield
screen saver. So I I I went on to Copilot and I've had it produce this Starfield screen saver. It's quite fun. You know, I can go to warp and it does little effects for me. I this is completely coded by co-pilot and essentially Sonnet and I haven't done anything right. I just wanted to see how much it would it how many tokens it would use. So I asked about five or six prompts to get to this point. The first one was things like can you add a little bit of color to the stars? Can you make them go faster and some slower? Things like this. At one of time it introduced a bug. I had to get to fix his own bug.
Um and if I type uh so this is this is the conversation I've been having. It's it's over a few prompts and each time it outputs some text that it thinks to itself. Then it outputs um some code changes and then it outputs its response to me to say what it's done. I can see that my current token uses for this session which was I think six prompts in total. Uh one file initially now three files is 2 million input tokens and 47,000 output tokens. Right? It's a lot
a lot of tokens. It's a lot more than I would do if I was just chatting on the web interface. >> There are some companies that are measuring whether they're moving to AI by how many tokens people are using. I mean, this is this is a ludicrous if this >> it's like measuring my quality as a driver by how quickly I wear through my tires. That is an unsustainable practice. There's no doubt it was obvious it was unsustainable. I think to those of us who understand what a token is before, >> you know, if somebody was measuring my use of a broom, how many times I used it and I just swept it backwards and forwards >> and it turns out you've just created some pulley system but just automatically does it like that episode of the Simpsons where he's got that bird doing the yes to keep the the thing the when you have an incentive like that, of course people are going to ask really long-winded questions. They're going to ask things that cause the AI to get stuck in a loop where its thought process is much longer. They're going to use the more expensive models. They're going to use models that think for longer periods of time and have to look over more files and in a surprise to absolutely no one that is completely unsustainable in terms of cost. You cannot afford to do it, right? There will be some companies maybe that will give us a go. But for a normal company that has normal paid developers, right?
and not the big tech companies, just normal companies who are producing regular products. To be able to spend that amount of money on tokens, you have to be able to see immediate product return. So, you know, we've developed five new features that are making us this much more money or we have a new product that exists but 6 months ago didn't and it's good, right? That has not been answered yet. Yes, that might happen. I remain suspicious that the quality of code is good enough for it to do that across all companies, right? Um,
and there are there are other issues like the fact that you have to kind of know what you're doing in order to, you know, this is not a good example. My my example is not a good example of coding because I haven't read it, right? So, it could be doing all kinds of things. Um, I certainly wouldn't inflict it on someone else's computer. A more reasonable way to use these models would be small succinct questions, very quick fixes, or the kind of code completion thing we already see. So, you're in something like codeex and you say, "I've written half a for loop. Can you just finish this off for me? That's quite a quick, you only need to read a small amount of context. That's quite a quick thing. The agentic stuff I think will have its place, but I think it is worryingly costly and we've been seeing a lot of subsidization, right? GitHub copilot, people are only annoyed about that because it was being subsidized before. When you pay a flat fee per month and you can have an agent go off for hours and hours doing all these millions of tokens, of course, you're going to think that's absolutely fantastic. But of course, when it gets to the when it gets when the rug gets pulled out and they say, "Okay, you're paying per token now and you realize that that that file you just read in is 10,000 tokens being put in over and over again, suddenly you're kind of thinking, well, actually, could I have avoided that? Could I have done something else that meant it didn't cost quite so much money?" That's what we're going to have to answer over the next year and see how that goes.
So I can see for example that split 171
is something about exchange of information between internet identities, right? I don't you know who knows. The next thing for me to do is to embed every single one of these chunks into the embedded space and stick them in a a database.





