HomeVideos

How to actually force Claude Code to use the right CLI (don't use CLAUDE.md)

Now Playing

How to actually force Claude Code to use the right CLI (don't use CLAUDE.md)

Transcript

203 segments

0:00

One of the most common questions I get

0:01

about agents and about claw code is how

0:03

do I force it to use the right CLI

0:06

command? How do I force it to adopt my

0:08

workflow instead of the workflow that it

0:10

knows best? For instance, how do I get

0:12

it to use uh PNPM instead of npm? How do

0:15

I get it to call this wrapper script

0:17

that we wrote instead of calling the

0:18

thing directly with npx? How do I

0:21

prevent it from using certain commands

0:23

and only allow it to use other commands?

0:25

If you're interested in these kind of

0:26

questions, this is the stuff that I

0:27

answer on my newsletter. And I'm working

0:29

on a Claude code course right now for

0:31

real engineers, not vibe coders. So, if

0:33

you dig that stuff, the link is down

0:34

below. The first thing that you probably

0:36

think about when you think about

0:38

steering Claude in one direction, but

0:40

not another, is to put information in a

0:43

clawed MD file. In other words, to

0:45

proactively tell it to use PNPM, not

0:48

npm. Now, this will be pretty effective

0:50

most of the time, especially if like

0:52

this you have a claw.md file that is

0:54

nearly empty apart from that one

0:55

instruction. However, in various places,

0:57

including in the previous video I just

0:59

posted, I've advocated almost always

1:01

deleting your claw.md because if you

1:04

think about it, this instruction here is

1:05

only relevant for certain tasks. And

1:07

putting it in claw.md makes it global

1:10

for every action that you're going to do

1:11

in the repo. LLMs have an instruction

1:14

budget that you can really only put

1:16

about 500 instructions in before it

1:19

starts getting less and less powerful

1:20

and more and more confused. And we want

1:22

our LLM thinking about the complicated

1:24

plans and the implementation that it's

1:26

going to put together, not having

1:27

irrelevant instructions about using npm

1:30

or PNPM. And the final thing that makes

1:32

me uncomfortable about this is the fact

1:34

that it's not a deterministic way of

1:36

preventing a command. For instance, we

1:38

may want to disallow it from running

1:40

destructive commands. for instance, we

1:42

never want it to get pushed. These kind

1:44

of negative instructions to the LLM are

1:46

really bad, I think, because they burn

1:48

instruction budget in something that

1:50

we're not actually using the instruction

1:52

budget to prevent deterministically. By

1:54

adding this, we're reducing our chances

1:56

of running git push or reducing our

1:58

chances of running npm, but we're not

2:01

preventing it. So, instead, I'm going to

2:02

show you an approach that prevents it

2:04

deterministically and guides the LLM

2:06

towards the correct solution. The way to

2:08

do this is via clawed code hooks. Hooks

2:11

allow you to run deterministic code at

2:13

certain points during claw code's

2:14

execution cycle. When one of these

2:16

events fire, all matching hooks run in

2:18

parallel. So for instance, you can run a

2:20

hook when the session starts. You can

2:22

run a hook when a permission dialogue

2:24

appears to automatically approve things.

2:26

The one that we're interested in is

2:27

pre-tool use before a tool call executes

2:30

can block it. Our plan here is to use

2:33

this pre-tool use hook to before it

2:35

calls uh npm to use PNPM instead. And

2:39

the cool thing about this is that Claude

2:41

code already knows how to do this. And

2:43

I've actually prepared a prompt inside

2:45

this file to take the claw.md, turn it

2:49

into deterministic hooks, and then put

2:51

those hooks into your project. Here's

2:52

what it looks like. Take the

2:53

instructions in your claw.md file, turn

2:55

them into deterministic claude code

2:57

hooks in this project directory. Not all

2:58

the instructions will be deterministic.

3:00

Only do the ones you can, such as

3:02

instruction to use one CLI command over

3:04

another or disallowing certain CLI

3:06

commands. use separate bash scripts for

3:08

running the hooks. In other words, this

3:09

is its own.sh file and it gives some

3:12

syntax here for how to construct that.

3:13

This is directly lifted from the claude

3:15

code docs. And then finally below here,

3:18

first confirm with the user which hooks

3:20

will be created. Second, implement the

3:22

hooks. Third, provide the user with

3:23

instructions on how to test the newly

3:25

created hooks. I'll include this in an

3:27

article which is linked below for you.

3:29

So, I'm going to copy and paste this.

3:30

I'm going to go into a fresh clawed code

3:32

session here. I'm going to zoom up the

3:34

terminal a bit. I'm going to paste it in

3:36

and see what happens. All right, it's

3:37

given me two options here. It says there

3:39

are two rules that can be enforced

3:40

deterministically. Block npm commands or

3:43

block git push. Now, I've actually

3:45

already got some git push blockers in my

3:47

global setup. So, I'm just going to say,

3:49

yeah, do the npm one only. So, that

3:52

sounds good. Let's run this and see what

3:53

happens. So, it's now given us something

3:55

to review. It's given us a nice version

3:58

of the bash command that we saw earlier,

4:00

except it just checks if it's npm.

4:02

Crucially, it's checking if the command

4:04

starts with npm as a command. It echoes

4:07

use PNPM, not npm. And then it exits

4:10

with an exit code of two. So that is

4:12

looking good to me. Let's accept that.

4:14

It's then going to make it ex uh

4:16

executable by running chimod plus x. So

4:19

I'm happy with that. It's then created

4:20

the configuration inside settings.json

4:23

here where we've got now hooks which is

4:25

um you know a property on the settings

4:27

pre-tool use which is the one we saw

4:29

earlier. It's mashing matching any bash

4:32

tools here. And then when it matches any

4:34

bash tool, it runs this command block

4:37

npm.sh. So that looks good to me. We're

4:39

in a good spot. And it's now appearing

4:41

to verify that the hook works in line.

4:44

I'm just going to try and run this to

4:46

see what happens. And you can see here

4:48

just up there, it said blocked use PNPM,

4:51

not npm. So we're definitely getting

4:53

somewhere. And finally, it's given us

4:54

some instructions here to test it. to

4:56

restart claw code in this project. Try

4:58

asking it to run something like npm

5:00

install fu. All right, the first thing

5:01

I'm going to do is actually remove the

5:03

instruction from claw.md here. And then

5:06

we're going to pull open a fresh clawed

5:07

code instance and we're going to say npm

5:10

install let's say react query. Okay,

5:13

let's give it something kind of juicy.

5:15

And we can see that the pre-tool use

5:17

here it said bash hook error blocked.

5:20

Use pnpm not npm. And now it's running

5:23

pnpm install tenstack react query

5:25

instead. So that's it. We have prevented

5:27

one command and we've automatically

5:29

steered it to use another command. And

5:31

crucially, we've managed to kill an

5:33

instruction inside our global claw.md

5:36

file. This is what I love so much about

5:38

this approach. You get to take

5:39

instructions out of your instruction

5:41

budget and enforce them

5:43

deterministically. This means that the

5:44

instructions that we have in claude.md,

5:46

they are available just in time. They

5:48

get to hover in the background until

5:50

Claude actually progressively kind of

5:52

discovers them. And this idea of these

5:54

rules just being enforced just in time.

5:57

You can take this and apply it to lots

5:58

of different things. For instance, I've

5:59

been turning my style rules, like I

6:01

really hate positional parameters in

6:03

functions and turning them into ESLint

6:06

rules so that I can lint Claude into the

6:09

right thing without needing to warn it

6:11

or instruct it. So creating these

6:12

feedback loops for Claude is so so

6:15

powerful. Now, if you dig this, then

6:16

this and all of my ideas are going to be

6:18

in my newsletter so you never miss a

6:20

trick. and the link for that is below if

6:22

you want to click it and sign up. But

6:23

thank you so much for watching. I've

6:25

been having a blast making these videos

6:27

and discovering lots of new ideas about

6:29

Claude. What else do you think you can

6:31

do with hooks? What else do you want to

6:32

see me cover with Claude code? And now,

6:34

of course, Claude is not the be all and

6:36

end all, right? Like I'm pretty keen to

6:37

try out other coding agents, too. And

6:39

the hooks idea is not exclusive to

6:41

Claude, too. So, you can take this

6:42

knowledge and probably take it to Codeex

6:45

or whatever you're using. But anyway,

6:46

that's enough rambling. Uh, thanks for

6:48

watching and I will see you in the next

Interactive Summary

The video addresses a common challenge in steering Claude agents to use specific CLI commands or prevent others. Initially, users often put instructions in `claude.md` files, but this approach is problematic because it makes instructions global, consumes the LLM's limited instruction budget, and doesn't offer deterministic control. The presenter advocates for using Claude code hooks, specifically the `pre-tool-use` hook, which allows for running deterministic code to block or modify tool calls. A demonstration shows how to prompt Claude to convert `claude.md` instructions into bash scripts for hooks and configure `settings.json`, effectively blocking `npm` commands and automatically substituting `pnpm`. This method frees up instruction budget, making Claude more efficient and allowing rules to be enforced just in time.

Suggested questions

6 ready-made prompts

Recently Distilled

Videos recently processed by our community