HomeVideos

How To Save 90% of Claude Code Token Usage

Now Playing

How To Save 90% of Claude Code Token Usage

Transcript

366 segments

0:00

Everyone how's it going in this video i'm going to teach

0:02

you how to reduce your cloud code token

0:05

usage by up to 90

0:07

in

0:08

four different strategies it's easy and free to do and you

0:11

can do it right away and in this video i'm going to show

0:13

you step by step and also a quick comparison on like

0:16

visually being able to see difference in tokens that you'll

0:19

be ending up using now all of these strategies actually do

0:22

have some trade-offs and i'll also go over those trade-offs

0:26

as well so be sure to stick around through the entire video

0:29

so you don't miss out on exactly how to use these things

0:31

properly as usual i made some slides so there's some

0:34

visual representation of what i'm talking about so let's get

0:37

right into the four different strategies now the first

0:40

strategy is actually to index the code essentially we're

0:43

going to create an index of the code now if you don't

0:46

know what indexing is it's essentially like search actually is

0:52

a very famous usage of an index. Essentially what Google

0:56

does behind the scenes is it creates a map of like

0:59

keywords that point to different websites as a very

1:02

dumbed down example. So the core idea is that we want

1:06

to essentially create a graph map of our code base.

1:09

So here's a visual representation. Now on the left side,

1:12

it's kind of how clock code already does

1:15

the grep and reading and scanning of the code base.

1:18

It will go one by one and deep dive into the code base.

1:21

If you're asking about some particular, you know,

1:24

server implementation or some validations like file or an

1:28

auth file, we'll try to go and grep it using regular file grep

1:31

and they will go and find it and load those into the

1:34

token context. So it ends up spending a lot of time

1:37

because sometimes to find the files that it's looking for,

1:40

it would have read a lot of things that it doesn't need.

1:42

Now, when you index the entire code base into essentially

1:46

a curable graph, You can see this representation here.

1:51

You essentially use like natural language to try to find the

1:54

code base via a search. And then you essentially do this

1:57

indexing ahead of time once so that Kako can leverage

2:01

this code graph so that it can find a lot of these things

2:04

under the hood without having to read all of this other

2:08

files just to get to where it needs to go. Now the way to

2:11

get this to work is essentially use this repo

2:14

called CodeGraph. It's very easy to install. All you need to

2:18

do is just copy this

2:20

mpx command and then paste it in or just install using the

2:24

npm global and then you just init the project here.

2:27

So for example, we could do CodeGraph init-i and then it

2:30

would initialize it. I already initialized it over here.

2:34

And there's a bunch of things that you can do with this.

2:36

So for example, you could do CodeGraph status

2:39

and they will kind of give you a metadata about

2:42

the CodeGraph, like what is in here, what kind of nodes

2:45

are there, like import. Is there a component?

2:48

You know, the common things that you kind of

2:50

would need. And then as an example, we could do

2:53

Curie. So let's do CodeGraph Curie LLM, for example.

2:57

And then here it found two search results where it says it's

3:02

HTML to text and active session.

3:05

So essentially just use like semantic natural language to

3:08

be able to find the relative code.

3:11

Now you wouldn't normally

3:13

use like

3:14

this manually, but Clock Code essentially learns how to

3:18

use this CLI tool under the hood and then it will do all of

3:22

these like functions for you. Now, if you look at the docs,

3:26

this is the CLI reference right here. It has all of these

3:28

commands and then essentially Clock Code will read the

3:31

CLI tool,

3:32

understand how to use it and leverage the CLI to do

3:35

the Curies. So that's the first strategy is essentially to

3:38

create a graph.

3:40

So that instead

3:42

of Clock Code reading files one by one, kind of using grep,

3:46

you do some work ahead of time.

3:48

And then you index a bunch of things so you could quickly

3:50

get to the file that you're looking for more semantically.

3:54

Now this is a very common strategy and essentially how

3:56

like a lot of search based products work, right?

4:00

Or graph recommendations like in Meta. But there are

4:02

trade-offs to this strategy. So the number one trade-off

4:06

with this

4:07

graph strategies, this pre-indexing strategy

4:10

is that now there is two source of truth, which is that one,

4:15

you have your code base as a source of truth. But now

4:18

when Clock Code is looking and researching things,

4:21

there is the index that is also the source of truth.

4:24

That means that some system, ideally Clock Code,

4:27

will have to spend time syncing.

4:30

Now this repo actually has a method of syncing, but if you

4:33

forget to do it, or it just hasn't been done since some time

4:37

period that you've been coding, something could go out

4:40

of sync. And then now Clock Code will confidently be

4:43

wrong that, hey, this function may not exist, or this file

4:47

doesn't exist. Or it does exist, even though it

4:49

doesn't exist.

4:51

So there's this tax of like having to keep it synced.

4:54

The other kind of major trade-off, which is not as big of

4:58

a problem, but essentially for compound engineering,

5:01

when you're adding context files into big modules,

5:04

for example, the way it systematically does code search,

5:08

it may miss some of that. It might find the actual file that it

5:13

needs to, but it might miss that module's Cloud.md

5:17

that it's supposed to load normally. Because the way

5:20

normal Cloud.md works

5:23

is if you put one in some directory, if the code search

5:27

goes into that file and reads that file, it'll actually go up

5:30

and read that Cloud.md in that module. But because

5:33

you're using a query version,

5:35

it may miss it.

5:36

So there is this inherent issue. And there's also

5:38

probably some

5:40

issue with accuracy, especially for

5:43

deep dives. And at the end of the day, you still need to

5:46

read the accuracy. Actual file once it files where the file is,

5:50

right? So it does save a lot of tokens in the retrieval of

5:54

the file, but you still will have to spend tokens on reading

5:57

the file. But yeah, so those are kind of the

5:59

high-level trade-offs. All right, strategy number two is to

6:03

compress the outputs.

6:05

So what does this mean? And I think this is a really

6:08

good example. So if you take a look at this

6:10

video animation,

6:12

on the left side is essentially an NPM run. And in the

6:15

NPM run, anyone who is programmed for a while knows

6:19

that server logs or just CLI logs or just logs in general is

6:23

very noisy. And oftentimes it's information that

6:27

ClockCode doesn't actually need. So there's this open

6:30

source library called RTK that actually takes a lot of these

6:34

noisy logs and then compresses them. So on the right

6:37

side is something like, oh, 43 test paths, rather than listing

6:41

out all the 43 tests. And then like, it'll also like bundle all

6:44

the warnings if it's suppressed them, for example.

6:47

So there could be huge savings here. And before that,

6:50

all of the tokens get read back by ClockCode, it just like

6:53

shrinks all of it. So you can imagine how much savings it

6:56

would have, especially

6:58

if your workflow is heavy in logs or CLI usage, or like a

7:01

bunch of like noisy things that ClockCode doesn't actually

7:04

need to know. This open source library will save a ton

7:08

of tokens. So as you can see, this is the open source

7:11

library right here. And it's very easy to install as well. I just

7:15

use homebrew and then you could just have it installed

7:18

into like a specific directory or globally. And again,

7:22

there's a bunch of ways you can like manually just trigger it

7:25

using the CLI commands. And this is pretty

7:27

common pattern. A lot of people have been realizing that

7:30

CLIs are just kind of a better form factor than MCPs.

7:34

And if they can help it, they would build a CLI over like an

7:38

MCP because ClockCode is very good at juggling CLIs.

7:41

Okay, so we'll do like a very simple example of doing like a

7:45

Git log, for

7:46

example. Git log.

7:47

And you can see like, if you actually do Git log,

7:50

there's so much, you know,

7:52

information about the Git log, it's just it goes on for

7:55

a while.

7:56

So let's use RTK now.

7:59

And then you can see that all of these things

8:01

were compressed, and it shows how many lines it

8:03

was omitted. And it just saves a ton of tokens. And again,

8:07

like CodeGraph, it's ClockCode will leverage this

8:10

tooling for you on your behalf. So you will get a ton

8:13

of savings. But

8:14

there is definitely a trade off here. And it's pretty obvious.

8:18

And essentially, the trade off is accuracy, because the

8:22

compression is lossy. By doing this method, you definitely

8:25

have a chance of like dropping a very important

8:29

log or some message that you are hoping for. And the

8:32

system tries its best to, you know, compress

8:35

and just remove things that it doesn't need.

8:37

But if you're trying to debug bunch of server logs, it might

8:41

be worth to turn this off when you're doing that

8:44

specific work. So like, you should really know when to turn

8:47

it on and when to turn it off. And at what point do you

8:50

need the extra logs for the improved like self healing,

8:54

for example, if you want to self correct, you may want to

8:57

read the system logs to make sure that everything is

8:59

happening step by step in the way that you want it. But in

9:02

most of the time, you don't actually need all of the logs.

9:05

So this is a really good strategy to compress a lot of the

9:09

output before it comes to you. All right, strategy number

9:12

three is,

9:13

in fact, my favorite strategy.

9:16

And honestly, I think they misnamed this, but essentially is

9:19

to use caveman, which is another open source library.

9:23

Now at a high level,

9:25

it's to get cloud code to talk

9:28

less.

9:29

So in this diagram, you can see on the left side, there's a

9:32

bunch of like long outputs on the right side is a very like

9:35

aggressive caveman example.

9:38

And

9:38

it will just compress everything. And I want to show this

9:42

video because I think it's the most accurate assessment

9:45

to what this is doing.

9:47

I waste time say lot word when few word do track.

9:50

That's probably one of my favorite clips from the office.

9:53

The office was ahead of its times, honestly.

9:57

And you know, Kevin was ahead of this time, and definitely

10:01

a clock code Maxer. But yeah, caveman, like I said,

10:04

probably should have been named Kevin, in my opinion,

10:06

but at a high level, it's really that it's just to shorten the

10:10

amount of words that clock code says to basically say the

10:14

same thing. It's very easy to install cave man, you can just

10:17

do NPM install here, just copy this command and paste it

10:20

into your bash command. There's many different ways.

10:23

And essentially, it's a skill that sets

10:26

your clock code session into a specific mode. So there's

10:29

like light full ultra, and then when

10:33

essentially it's per session, so you could remove it at any

10:36

time or like at it. So it really depends. So let's give it a shot

10:40

real quick. Tell me about this project. Alright, so on the left

10:43

is without caveman. That's the caveman Ultra. And then

10:46

here is came in and saw and it's ready for the next project.

10:49

Tell me about this project. Alright, so as you can see

10:52

right here, it's about like half of the output. And I would

10:56

say you still get about like the same quality.

10:59

It's pretty accurate. So that's caveman. I use it quite a bit.

11:02

I keep it on ultra

11:04

until I like need to get away from a specific trade off.

11:07

And we're gonna go into the trade off now. So the obvious

11:10

trade off here is that yes, you save tokens, but the

11:14

correctness is at risk here. Because essentially, clock code

11:18

really needs to leverage this like feedback loop of

11:21

messages in and out to get as much context that it needs

11:25

to answer things questions better. But when you

11:28

essentially shrink the outputs dramatically,

11:30

those messages get resent back to clock code, right?

11:34

The way the context window works is the context that you

11:37

have essentially clock code leverages it in this

11:40

agentic loop. And that's how it knows what you've been

11:43

working on. So your context history that session if the

11:46

quality is bad, then the output may lead to like

11:50

bad answers. Essentially, I'm working on a video right now

11:53

on system design for a clock code. If you're interested in

11:56

that subscribe,

11:57

I think it's very important to understand like

12:00

system design, system design is probably the most

12:02

important thing in this new world of AI. So I'm doing a

12:06

whole series

12:08

on system design for like agentic tools agentic,

12:11

like building chat GPT, building clock code, building like a

12:14

rack system, like all of that kind of stuff. So if

12:17

you're interested, don't forget to subscribe

12:19

to this channel and my newsletter. Alright, the final

12:21

strategy is kind of boring, but it's good old just managing

12:26

the clock code session properly using like compact,

12:29

changing the models, the docs have pretty good

12:32

recommendations on managing it. Here is kind of the

12:36

main ways to main built in ways to save your

12:41

tokens. Alright, so obviously, there is

12:44

such context, right here. And this kind of shows you

12:47

what's in the current context. And I look at this every now

12:51

and then to make sure that my

12:52

usage just makes sense. You know, sometimes if you have

12:56

like a really large cloud.md, you won't realize it until you

13:00

look at it. And every time you turn on the quad code,

13:03

it's using like 40k tokens, and you're like, what is going on?

13:06

How is it that every time. I just opened my session is

13:09

using 4%? Well, it could just be that you have a 50k

13:13

token cloud.md.

13:15

It's like a pretty small cloud.md that like just is already

13:19

indexed and like points to different

13:21

things. So this is a really good way to kind of audit what's

13:25

using the most tokens. And sometimes you'll find that like

13:29

MCPs end up using a lot in this case, it's not there's a

13:32

bunch of things that's loaded, it's ready to go. And these

13:35

are my MCPs that I have. But essentially, this is a way to

13:39

debug the current context. There's also slash clear,

13:42

which clears

13:43

the context. And.

13:45

I will say that if you're working on like new tasks, let's say

13:48

you have a you just finished a task and you want to do

13:51

another new task, then it's good to clear the task if you

13:54

need a previous context or not. There's also slash model

13:57

to switch the models. And some people ask me if like

14:01

haiku or sauna is ever worth using. And my answer is

14:04

100% Yes, I think one of the best use cases of haiku is

14:09

actually using it in slash Chrome, or just navigating like

14:13

Chrome haiku does a really good job. And it's actually

14:15

better in most use cases, because it's faster to navigate

14:19

because the model itself is fast. For sauna, I actually

14:22

leverage sauna for a lot of like my scheduling jobs. If I have

14:26

like a slash schedule, I will try to see if that schedule job

14:31

that I want to run every like day or whatever, let's say I,

14:35

I have one that like just cleans up my desktop, like it's just

14:40

like looks at my desktop and moves everything into

14:43

folders that I could probably just use sauna or haiku.

14:46

Because I know that that specific thing can be done

14:49

with sauna. If I know that that task

14:52

is repeatedly successful, we're using sauna or haiku,

14:55

I'll always end up using that. So don't forget, try switching

14:59

the models. And you don't have to be on the million

15:02

4.7 context. Now, pro tip here is that if you're doing any

15:06

significant programming tasks,

15:08

I do recommend just being on

15:10

the the most, like reliable, especially for planning and

15:14

doing deep dives. The quality self, in my opinion, is a token

15:17

savings versus like trying to save money with sauna and

15:21

getting a bad quality result. And the last few things is I

15:25

would recommend using plan

15:27

mode first to plan out an execution rather than just

15:31

like having

15:32

clock code and go do just a bunch of things. And then for

15:35

programming in general, I would say use like

15:38

pencil or Figma

15:40

to design something, run off designs,

15:43

rather than just like coding something right.

15:45

So yeah, so those are kind of the high level tips, I would

15:48

say that I have for just managing your

15:51

session. Now, a lot of what I said actually is in this stock,

15:56

I'll link this below. But you know, clock code also has like

15:59

advanced strategies. Most of this is already like kind of

16:02

covered in my talk, not the repos, they won't cover the

16:06

open source projects, but more about like the

16:08

strategies that. I just mentioned about managing

16:10

your session. So feel free to take a look at that.

16:13

For example,

16:15

they said agent teams cost a lot,

16:17

which makes sense. So should you use all of these things?

16:21

And in my opinion, I think the answer is yes.

16:24

But again, there's trade offs.

16:27

And in my opinion,

16:28

the most the largest trade off here is really costs

16:33

versus

16:33

quality. And also just like the

16:38

quality and and and also the complexity, there's a

16:40

complexity aspect, right? There's

16:43

the code graph, it can get stale.

16:45

The RTX proxy is lossy for sure. So you're straight up just

16:48

missing messages. And sometimes you'll miss a log or

16:51

something that you need. And caveman may be over

16:54

trimming things so that

16:56

the message history is not great. But

16:59

it all of these things dramatically save the tokens.

17:03

So really, it's you understanding the how to use these

17:07

tools and essentially being like, oh, okay, in this

17:10

particular application, I may not need all of the logs. So I

17:14

could just have RTX be on. And then oh, in this case,

17:17

caveman is great, because I don't really need it right now.

17:21

I don't need this. I'm not doing like a crazy plan mode

17:24

right now or something like that.

17:26

So yeah, but all of these things add complexities,

17:28

add multiple layers, multiple things for you to juggle

17:31

multiple failure points, essentially for clock code, right?

17:35

So yeah, you got your own risk,

17:38

but I guarantee you, you will save

17:40

tokens

17:41

using these strategies. So yeah, I hope you enjoy

17:44

this video. I make a ton of videos on clock code and AI

17:48

coding agent decoding. So feel free to check them

17:51

out here.

17:52

And don't forget to subscribe to my newsletter, I

17:55

have a bunch of content there that I don't really post here,

17:59

I actually also just launched a new course with bye bye go.

18:02

And I'll be teaching you over there. So if you're interested,

18:05

it is a paid live course. It's a two day thing, you could build

18:09

a bunch of portfolio projects.

18:11

And I teach you basically everything I know about clock

18:14

code. So if you're interested, sign up to a bye bye goes

18:17

newsletter or look at this website, the course may or may

18:20

not have already happened. So if you missed it, you might

18:24

have to join the next course. But yeah, I hope you guys

18:26

enjoyed this video. And until I see you guys on the

18:29

next one.

Interactive Summary

This video outlines four actionable strategies to reduce cloud code token usage by up to 90%. The presenter introduces techniques such as indexing the codebase with CodeGraph, compressing noisy output logs using RTK, utilizing the Caveman library to shorten AI responses, and managing sessions effectively through model switching and context audits. Each method is accompanied by specific implementation steps, tools, and a discussion of the potential trade-offs regarding accuracy and system complexity.

Suggested questions

4 ready-made prompts