Full Transcript

·YouTLDR

Stanford CS146S: How always-on agents work

45:17EnglishBy leerobTranscribed Oct 13, 2026
Analyze another video with Pro30-day money-back guarantee
0:00

So, as mentioned, I'm Lee. We're going

0:02

to talk a little bit today about how

0:03

this new category of always on proactive

0:07

persistent agents work and kind of give

0:10

you some of the understandings of the

0:11

building blocks for how these are made.

0:13

Um, just as a, you know, fun statement

0:16

up here, some of the things we'll talk

0:17

about are in progress. We're working on

0:19

at SpaceX. Um, and in week one, you all

0:23

built this very kind of simple harness

0:27

that helps you understand the core idea,

0:29

which is that you have a model. It can

0:30

call tools in a loop. And that's

0:32

actually very powerful. It unlocks a lot

0:34

of new capabilities for the models. Um,

0:37

but it does come with some drawbacks.

0:38

Notably, you close your laptop and the

0:41

process stops. It's no longer running.

0:43

Uh, and I want to talk about how you can

0:45

take this simple idea, it's very

0:47

powerful for how to get more out of

0:48

models and extend it to this new type of

0:51

agent which is an always on agent. So

0:54

rather than just running in your

0:55

terminal, this can run on a server, it

0:57

can run in the cloud, you can close your

0:58

computer, uh, it can restart, it can

1:00

survive crashes, for example. This agent

1:03

has its own computer. So not just your

1:06

laptop, but a Linux machine in the cloud

1:08

where it has its own desktop files,

1:10

browser, and more. maybe some 3D apps.

1:13

Um, it can save files on the machine. It

1:16

can understand more about your progress

1:19

over time and build up a memory of how

1:21

you work with it. Um, unlike a harness

1:25

in the terminal that is responding to

1:27

every message that you send, this type

1:29

of always on agent is more like a

1:31

colleague where it doesn't always

1:33

immediately send you a message. It knows

1:34

when to be quiet. Sometimes it might

1:36

just want to work in the background a

1:37

little bit and only come back to you

1:39

when there's an important update. Uh,

1:41

and it can be triggered more than just

1:43

you typing. So, it's not just you

1:45

prompting it in the terminal. Maybe

1:46

there was a Slack message or an email or

1:49

some other type of event that wakes up

1:51

this agent to go do work. Um, and

1:54

ideally

1:56

the hope with these type of always on

1:57

agents is that they can do work over

1:59

very long periods of time. Not just

2:01

hours or days, but maybe even weeks or

2:03

months. And that just requires a little

2:05

bit of a different architecture, which

2:07

we'll get into. So, we're going to go uh

2:10

decently in depth on how we built this

2:12

architecture, how we built the harness,

2:15

um kind of what's got us here today and

2:17

where we're going in the future. And

2:19

we'll get into some of the specifics of

2:21

how we've engineered this system to be

2:24

proactive, to be persistent, and to

2:26

follow instructions very well. And of

2:28

course, time at the end for questions if

2:30

you all want to ask anything about this

2:31

or uh anything outside of uh what we've

2:34

covered today. So, first, how we got

2:37

here.

2:39

There's kind of been four eras of

2:42

developers working with AI so far. I

2:44

would say the first one was uh copy

2:47

pasting code out of chat GBT and putting

2:49

it into your editor of choice which

2:51

feels like you know decades ago at this

2:54

point but it was really only a few years

2:55

ago. Uh and then once the model started

2:59

to get better at being able to have kind

3:01

of that simplified harness we talked

3:03

about uh in you know in the last uh

3:05

lecture uh they were able to edit files,

3:08

run shell commands, kind of have this

3:10

very simple interface for how you work

3:12

with them in the terminal. And this

3:14

really took off because all of a sudden

3:16

the model had this new capability where

3:18

it could go do more proactive work for

3:20

you and be more you know productive.

3:22

Then this third era was taking the

3:25

coding agents and where they were kind

3:28

of simple to start and giving them these

3:29

dedicated apps. And at this point,

3:31

people were starting to run many agents

3:33

in parallel. Maybe they're running them

3:35

in the cloud, not just on their machine.

3:37

Uh but still at this point, you were

3:39

kind of the bottleneck in that you were

3:41

reviewing every command that they were

3:43

running. You wanted to make sure every

3:44

shell command was just right. You're

3:46

kind of really watching them and being

3:47

in the loop on everything. And now with

3:50

improvements in models, in harnesses, in

3:52

products, we're now in this new era

3:54

where you're starting to have these

3:56

always on agents and they can kind of

3:57

run ambiently in the background where

3:59

they have their own computer. They have

4:01

their own memory. They learn new skills

4:03

on the job and they keep working after

4:05

you go, you know, go to bed or go do

4:07

other work. A good example of this is

4:10

we've kind of moved away from this world

4:12

of prompting an agent every single day.

4:14

Although we still do that sometimes, but

4:16

you can set up agents where it will kind

4:18

of just run in the background. So maybe

4:20

in the the before times it was you would

4:22

prompt an agent, you would wait. You

4:23

prompt an agent, you would wait, and if

4:25

it asked you to run a shell command,

4:27

you'd have to approve or, you know,

4:29

disallow that command. Uh and now

4:31

instead of kind of queuing commands, you

4:34

can steer the agent and you might say,

4:36

"Hey, go check this thread." and then

4:37

immediately after, oh, by the way, you

4:39

know, actually go do that on Thursday.

4:41

And prior generations of models would

4:43

get kind of confused when you would

4:45

immediately interrupt it and give it

4:46

some other command. But now they kind of

4:48

understand just like how you might

4:50

ramble on talking to a friend and they

4:51

can kind of parse that these two things

4:53

are related. The models and the

4:55

harnesses can now handle that. And if

4:57

you say, "Hey, go send this email, go

4:59

send this Slack message." Um there are

5:02

additional safety checks in place which

5:04

a lot of companies are calling

5:05

autoreview which is essentially a

5:08

separate AI model that's checking all of

5:10

the commands that get run. So you know

5:13

kind of funnily enough that's actually

5:15

more secure than the fatigue of having

5:18

to watch hundreds of shell commands and

5:20

approve each one. We've kind of reached

5:21

the point where if you did a blind test

5:23

with what the AI model's checking on the

5:25

commands and what a human's checking on

5:26

the commands. Most of the time the the

5:28

model's actually doing a better job.

5:30

um kind of paradoxically, but as the

5:34

models have gotten better, the interface

5:36

has gotten quite a bit more simple

5:38

actually. And this type of always on

5:42

agent we're calling a bot. But as you'll

5:44

notice, there's actually quite a few of

5:46

these new types of products popping up

5:48

right now and gaining a lot of traction.

5:50

And I think the main reason is because

5:52

we all know how to text. You know,

5:54

everyone knows how to text. Everyone is

5:55

pretty proficient with using their

5:57

phones. And we've kind of distilled down

5:59

this way of working with AI that feels

6:01

like you're just texting a friend, but

6:03

it can actually go and do real work for

6:05

you. It's not just answering questions

6:06

in a Q&A style setting anymore. It's

6:09

actually going and doing longer tasks

6:11

like you would hand off to a co-orker,

6:13

for example. Um, so there's been a few

6:15

changes specifically in the models that

6:18

have enabled this to happen. Um, I'll go

6:21

over six, but there's, you know, quite a

6:23

bit more. The first one is that the

6:25

models just over the past year or two

6:27

years have really gotten better at being

6:29

able to follow instructions over a

6:30

longer period of time where they don't

6:32

get confused or make mistakes and now

6:35

models can pretty confidently run for

6:37

hours or days with the right uh

6:39

compression and compaction which we'll

6:41

talk about in a little bit. Two, models

6:44

now can essentially hand off to helpers

6:47

and those helpers are called sub aents.

6:49

And the reason why a sub aent is helpful

6:51

is the the model has kind of its working

6:54

memory of the process that it's on and

6:57

that's going to fill up eventually. Uh

6:59

so it's nice if it can hand off to these

7:01

helpers that have a fresh set of working

7:03

memory and that effectively can give it

7:06

unlimited context basically where it can

7:08

go and do work that might be expensive

7:10

in terms of the tokens it's using or the

7:12

tools that it's calling and then come

7:13

back to the main agent and report its

7:15

updates. And I'll have some diagrams of

7:17

that later. Um, models now not only are

7:20

just better at calling tools. I mean,

7:22

just a few years ago, they basically

7:23

couldn't call tools very well and they

7:25

would hallucinate all the time. It's

7:26

funny, you don't really hear people talk

7:27

about hallucinations anymore because

7:29

it's kind of a solved problem mostly.

7:31

Um, but not only can they call tools,

7:34

but you can give them very powerful

7:36

tools the same way as if you onboarded

7:38

somebody onto your team. So, obviously

7:40

running shell commands and files, but

7:42

also you give them a full computer, a

7:43

full Linux uh computer. uh you allow

7:46

them to make demo videos and such. Um

7:48

they can click around on a screen just

7:51

like a human would and they're getting

7:52

increasingly better at doing that,

7:54

otherwise known as computer use, which

7:56

basically means that any SAS product or

7:59

any website that didn't expose an API or

8:02

an MCP server can be used headlessly

8:05

basically so autonomously through uh

8:07

computer use by these models and these

8:09

bots. um you can store all of the

8:12

information in files and the reason you

8:14

can do that is because it's actually

8:15

very easy for the models then to recall

8:17

and search this information which we'll

8:18

talk about and then finally you can just

8:20

kind of show the bot how to do some work

8:23

and it will kind of turn that into

8:25

skills or memories or routines or things

8:28

that it can reuse later. So on the file

8:31

thing for example, this is kind of a

8:32

simplified example, but you might

8:34

remember or you might uh imagine that

8:37

when you're working with a bot under the

8:38

hood, it might look something like this.

8:40

You build up this profile or this

8:42

understanding of the user and that's

8:44

going to be included in every single

8:45

prompt. Maybe you like, you know,

8:48

lowercase text like messaging and every

8:50

single message should have that. Also,

8:53

as you're working with these bots over

8:54

time, you're building up a kind of like

8:56

a working log, just like if you were

8:58

working with a colleague and they were

8:59

writing down notes every day of the work

9:01

that they've done. That's all being put

9:03

into files that the model can then

9:04

decide to search or use later. If you

9:07

say, "Hey, what did I do last week?" It

9:08

just can can go and look that up. And

9:11

the models are very good at using Linux

9:13

commands, running shell commands to go

9:15

grap and search and look through these

9:17

files. Um, similarly, um, with skills,

9:20

you are essentially

9:22

taking your knowledge of how to do a

9:24

specific task and encoding it into a

9:26

markdown file. The very specific process

9:28

you need to do the things that you

9:30

believe make that uh you know successful

9:32

or unsuccessful. And that's not just

9:34

your skills. There's a whole ecosystem

9:36

now of companies and developers um

9:39

writing these skills for how to send a

9:41

great email or you know how to build a

9:43

great API or many different things. Uh

9:45

and and for example in Grockbot there is

9:48

a whole system of plugins. So basically

9:49

any tool that you want to use there's a

9:51

plugin you can install which uses skills

9:53

or servers and then also routines which

9:55

if you're familiar with cron jobs you

9:56

know you're running some prompt

9:59

basically on some kind of schedule which

10:01

is pretty useful as well. So basically

10:03

everything is computer um part three. So

10:06

let's go a little bit further into the

10:09

kind of the mind of this always on agent

10:11

or going behind the scenes. Um what does

10:14

it mean for the agent to be always on?

10:17

You can kind of read this chart from

10:19

left to right, but it starts out with

10:22

the server being asleep and also this

10:24

computer that the agent can use being

10:26

asleep. And so whether you send in a

10:29

message or some kind of trigger uh

10:31

decides to make the agent start up,

10:33

which we'll talk about in a second,

10:35

that's basically loading up and booting

10:37

up all of the state for this agent. And

10:39

it's running on a server. if it decides

10:41

that it needs to use a computer, maybe

10:43

it wants to go use the computer's

10:44

browser or, you know, go click around in

10:46

the computer, it can start up the

10:48

computer as well. And then once you're

10:50

done using it, it also has the ability

10:52

then to basically put those to sleep.

10:54

Why is this important? It would be, you

10:55

know, expensive and also uh there's not

10:59

enough computers in the world to just

11:00

have those computers running all of the

11:02

time uh for everyone as well as that's a

11:04

lot of server usage too. And so this

11:06

state can then be persisted to you know

11:08

a database the the conversation logs the

11:11

memories the routines all of that. Um

11:14

how does the agent get woken up then?

11:16

Well obviously you can prompt it

11:17

directly but I think what's increasingly

11:19

interesting is having other things

11:22

running in the background that can then

11:24

decide when you want to go reach out to

11:25

your bots. Uh maybe that is somebody

11:27

pinging you on Slack and your bots can

11:29

live in Slack. Maybe that's a phone call

11:31

or a meeting that you put your bot in.

11:33

It's increasingly common now at SpaceX

11:36

where we see bots joining meetings for

11:38

employees where they couldn't make it to

11:39

the meeting, but they want their bot to

11:41

go and just take notes and come back to

11:43

them, which is kind of funny. Um, maybe

11:46

you had some routine or some background

11:48

work that was taking, you know, hours or

11:50

days to do and then you want to come

11:51

back and give you a summary. That all

11:53

happens. Then we have basically a router

11:55

that ddupes that and figures out how to

11:57

put it into the conversation. And then

12:00

it's has this heristic of do I need to

12:03

bug the user about this or not? Because

12:04

not everything is actually important.

12:06

Just like if you have a co a co-orker

12:08

working on something, it would be pretty

12:09

annoying if every single time they made

12:11

progress on something they came and

12:12

bugged me like, "Hey, check it out. Like

12:13

I made progress on this thing." You

12:14

really only need a status update maybe,

12:16

I don't know, every day depending on the

12:18

task.

12:20

Um, so I talked about how the agent has

12:22

this computer, this Linux box. Um, this

12:24

is running a firecracker virtual

12:27

machine. And the reason this is really

12:28

helpful is because it's it's very easy

12:30

then to essentially snapshot the state

12:32

of the machine and shut it down and

12:34

start it back up. And on this machine

12:36

you of course have files, but you can

12:39

kind of connect into the machine and

12:41

have a live view of the machine and

12:42

actually control it yourself if you

12:44

want. Um the machine can of course run

12:46

commands which is great if you're going

12:48

to run some shell commands. Maybe you

12:49

don't want it on your personal laptop.

12:50

You want it in this other environment

12:52

that is you know secured and locked

12:54

down. And then another interesting thing

12:56

is of course it has Chrome and it can

12:58

use a browser but you can also make it

13:00

essentially have like virtual desktops.

13:02

So if you're running five bots, 10 bots

13:05

and each one of them needs their own

13:06

browser you can essentially let them

13:08

have different screens. And all of this

13:10

again is kind of shared infrastructure

13:12

between these [clears throat] bots where

13:13

they can go call on these computers.

13:15

Um so we talked about kind of the the

13:17

triggers that will wake up the agent

13:19

that's kind of on the left. So different

13:21

apps or Slack messages or emails. Um

13:23

then you have this middle piece which is

13:25

the server and then on the right is the

13:27

computer that we just talked about where

13:28

you can run files and commands and other

13:31

things. So let's talk a little bit more

13:32

about the server. I already mentioned

13:34

you know commands will come in they get

13:36

put into this queue and then it calls

13:38

the purple box which is the agent loop

13:40

and this is what we covered last week

13:42

like this is a model calling tools in a

13:46

loop and then when you shut it down you

13:48

can kind of save the state to the

13:50

database or storage. Um, so it's like

13:52

it's it's interesting, I think, to look

13:53

at this and realize there's this one

13:55

kind of small piece that can then have

13:57

all of these abstractions built on top

13:58

to create uh a very powerful product uh

14:01

when you give it all of these helpful

14:03

tools and you connect it to all of these

14:04

different services.

14:07

The reason agent runs on the server is

14:10

uh a pretty common practice in computing

14:12

where you want to have kind of a thin

14:14

client and a thick server. And computing

14:16

has kind of oscillated between these

14:18

things over the years. But for example,

14:20

a few good reasons for this is let's say

14:22

you need to push an update to the, you

14:24

know, the agent code. It would, you

14:26

know, kind of suck if you had to push an

14:27

app store update just to get that code

14:29

out there versus just pushing a bunch of

14:30

server updates very quickly. Um, also

14:33

sometimes you need to do heavy

14:34

computation work, you know, like

14:36

transcription or image generation or

14:38

something. And that's probably going to

14:39

be faster on a server than on device

14:41

depending on uh what the devices are.

14:44

Um, and you know, it's there's very

14:46

established ways that we know how to

14:48

scale servers, especially with variable

14:50

traffic and variable growth. Um, but the

14:53

most critical thing here is that if you

14:56

put the agent loop on the virtual

14:59

machine, then you can't shut down the

15:01

virtual machine independently from the

15:04

uh from the agent server. So yeah, you

15:06

want to save everything on the server.

15:08

Um, so you can always kind of rebuild

15:10

that virtual machine. For example, let's

15:12

say you need to do some critical zero

15:15

day Linux uh update on the on the VM.

15:18

Well, it would be great to be able to

15:20

rebuild that and not, you know, blow

15:21

away the entire agent loop and have

15:23

downtime in the application. Um

15:27

um when you hit send, this is maybe a

15:29

little in the weeds, so I'll just kind

15:31

of gloss through it, but basically um if

15:33

you want to dig more into learning more

15:35

about distributed systems and thinking

15:37

about um you know, strong versus

15:40

eventual consistency, which are just fun

15:42

topics to get into, you know, you send a

15:44

message and you kind of want the client

15:46

to immediately identify that and

15:48

acknowledge it. Then when you go into

15:50

the turn, you're obviously putting these

15:52

um tools in a loop with the model,

15:54

saving that to the database, and then

15:56

the clients are essentially putting

15:59

subscriptions with all of the servers.

16:01

So they know if something changed and

16:02

they're listening for these signals, and

16:04

if they get a signal, then they can go

16:05

and essentially fetch the latest

16:07

information from the server. And there's

16:09

some fancy dduping you can do to make

16:11

sure if you know if there were some kind

16:12

of retries um that you would not show,

16:15

you know, multiple messages in the the

16:17

list. And so, uh, the like underlying

16:20

infrastructure primitives to think about

16:21

here, um, is a workflow versus a job

16:25

queue. And the main difference here is

16:28

with a job queue, let's say you're kind

16:29

of going along on steps, you ask your

16:31

agent to go and do some kind of

16:33

complicated task, uh, and it's making

16:35

its way from step one to step two to

16:37

step three, but then for some reason

16:39

there was an infrastructure issue. Um,

16:41

there was some problem with a normal

16:44

queue. If that crashes and you go to

16:46

restart it, well, now you have to replay

16:48

steps one, two, and three, which is

16:50

obviously that that could be pretty

16:51

problematic. So, what you really want

16:53

here is you want a durable workflow,

16:54

which means that it's safe for you to

16:56

actually retry that. And when you retry

16:59

after a failure, it picks right up on

17:01

step three. And this is like kind of a

17:04

solved problem in some ways in

17:06

distributed systems and in

17:08

infrastructure. So, we use an open

17:09

source uh tool called Temporal, which is

17:11

interesting to look into if you're if

17:13

you're curious, so that we don't have to

17:14

rebuild a lot of this very complicated

17:16

stuff from scratch. Of course, you still

17:17

have to run the infrastructure for it,

17:19

but some of the pieces to put together

17:20

there. Um, so let's say a new message

17:25

comes in. There's different priorities

17:28

based on whether I'm messaging my agents

17:30

or messaging my bots versus if it's in a

17:33

group chat or a Slack thread or some

17:36

background routine for example. So if I

17:39

say oh actually you know change the date

17:41

for this event, we actually want that

17:43

agent loop to stop and my message is

17:45

going to have priority over everything

17:46

else that's happening which kind of

17:48

makes sense. Um, for everybody else,

17:50

you're kind of putting things into the

17:52

queue in the back of the line. And if

17:54

you're if the the agent gets, you know,

17:56

three or four messages very quickly,

17:57

it's able to kind of batch those

17:59

together. So, it's just one turn. You

18:01

can kind of figure out whatever heristic

18:02

you want there. Um, and then it kind of

18:04

goes through and does each turn at a

18:06

time.

18:08

Okay. So, let's talk about the the

18:10

harness itself. And this is going to

18:13

kind of take the the basic harness that

18:15

we talked about last week and add some

18:17

some new tools, some new concepts to it.

18:21

The biggest difference with the Grockbot

18:23

harness um and probably some of the

18:26

other tools like this uh on the market

18:28

is

18:30

the way we've done the thin client fix

18:33

server architecture is there's only one

18:34

tool that communicates between the

18:36

client and the server and it's just send

18:39

user. And the great thing about this is

18:41

that it's actually very architecturally

18:44

simple where the client is just texting

18:47

the server in some ways and you're

18:49

putting a lot of these heavier tools on

18:50

the server. And there's many of those

18:52

tools. There's running shell commands,

18:54

reading files, but also calling on a

18:56

cloud agent or recording your screen or

18:58

doing demos for example. Um, and this I

19:01

I think has worked pretty well because

19:03

now regardless of where you spin up a

19:05

new client, whether it's a mobile app, a

19:07

server, um, a desktop app, email, Slack,

19:10

it all just has this one single tool

19:12

that communicates back and forth with

19:13

the server. On the client though, even

19:16

though it just has this one tool, when

19:18

it receives the tool, it can decide how

19:20

it wants to display that in a rich and

19:22

interactive way that makes the most

19:23

sense for the user. So if it gets uh

19:26

information back from the server that

19:28

the user is trying to log into

19:29

something, it can kind of show these

19:30

unique sign-in forms and connect to

19:32

tools like one password. It can go and

19:34

call on services like uh stripe link or

19:37

use X money or uh other services to go

19:40

and allow people to pay for their

19:42

services. You can if you're trying to do

19:44

something that is non you can't reverse

19:47

it sending an email or a Slack message,

19:49

it might come back and ask you to you

19:51

know edit and approve a draft before you

19:53

send it. And then sometimes it just you

19:56

know you can just leave an emoji

19:57

reaction if you don't actually need to

19:58

send a message. Um so going back to what

20:01

I was saying with dynamic tool

20:03

discovery. So for uh for any model

20:06

there's a context window which is kind

20:08

of its working memory and that's some

20:10

limit in tokens. And if you just jam

20:14

every single tool possible into that uh

20:17

context window, you're not going to have

20:19

a lot of space. And the way the tools

20:21

work is you have kind of the the name of

20:24

the tool, you have the schema, you have

20:26

all these different details. And if you

20:28

start installing your Google Drive

20:31

connector, your Gmail connector, your

20:33

YouTube connector, like all these

20:35

different services that use MCP servers,

20:38

which all have tools, you're going to

20:40

start looking at something like the top

20:41

where a lot of the blocks are already

20:43

filled. Uh, and the problem with that is

20:46

it doesn't give the model as much space

20:47

to actually do the work of the

20:49

conversation. Um, so one kind of context

20:53

engineering or harness trick um that a

20:55

lot of the the popular harnesses now use

20:57

is uh it's called dynamic context or I

21:00

think uh some other folks call it the

21:02

tool search tool which is a fun name but

21:06

basically you're only putting the names

21:08

of the tools into the every message that

21:11

gets sent to the model and then the

21:12

model can decide okay I actually want to

21:16

go use this tool. I can go read the

21:17

information from a file. It's kind of

21:19

files and a file system all the way down

21:21

for all of these things. Uh, and that

21:24

just helps you save on your context

21:26

usage, which then also helps save on

21:28

cost, save on usage, just better for

21:30

everybody. In general, the principle

21:32

here really is to try the cheapest

21:35

option or the most reliable option first

21:37

and kind of work your way up the ladder

21:39

here. So, the easiest one is just what

21:42

you know what the agent or what the bot

21:44

already knows based on the conversation

21:46

history. uh if it doesn't if it's not in

21:48

the immediate context, maybe there's a

21:50

plugin, an MCP server, some kind of API

21:53

that it can go and pull, you know, your

21:55

uh your banking information from Plaid,

21:58

for example. Uh maybe that doesn't work

22:00

and it needs to go do some kind of web

22:01

search to see, you know, what was the

22:03

score of the Cubs game last night. Um

22:07

maybe if it doesn't have that, it

22:09

actually needs to go to the computer and

22:12

open up the browser and do some searches

22:13

on there. And finally, like maybe it

22:15

needs to go and actually use the entire

22:17

desktop. So run scripts or run commands

22:19

or build things on that Linux computer.

22:21

And only if all of those fail, then go

22:23

bug the user and ask them to go give

22:25

input or approve something. And this

22:27

really helps it feel like what it would

22:29

be like to work with a colleague is like

22:30

they're not going to bug you for all

22:32

these different things. Ideally, they're

22:33

going to try a lot of these options

22:35

first.

22:37

um on that computer. So the the Linux

22:40

machine that the agent is able to use,

22:43

there's kind of three different ways

22:44

that it can retrieve information from

22:47

it. Um of course it can just take a

22:49

screenshot of the screen, but there's

22:51

more efficient ways of doing this. Um it

22:53

could also look at the HTML of the page,

22:55

but you can also look at the

22:56

accessibility tree, which is a nice way

22:58

of like cutting down a lot of the uh

23:01

token usage and basically to help you

23:03

find the buttons or links or other

23:05

elements that you want. uh the browser

23:07

to click on. Uh, of course, if none of

23:09

that works, you can just click on

23:10

pixels. So, some websites, for example,

23:13

have modals or dialogues that pop up

23:15

that you need to click. Uh, and so your

23:18

model, your agent needs to have that

23:19

functionality as well. Or again, kind of

23:21

worst case on that ladder is you go back

23:23

to the user and like, oh, there's like a

23:25

two-factor off code or there's some kind

23:27

of capture or or something like that.

23:30

Um, so the idea is that kind of putting

23:34

the pieces together, you have this main

23:36

agent and it's kind of like your

23:39

orchestrator

23:41

and the main agent you want to keep the

23:44

conversation as sparse as possible

23:46

because it now has the ability to go

23:49

call on all of these different helpers.

23:51

And the helpers are just sub agents. And

23:53

so this is helpful because if you hand

23:55

it off to a helper to go run a bunch of

23:57

shell commands or do a lot of expensive

23:59

tool calls, all of that work that

24:02

ultimately is ending up in some answer

24:03

to a question or a task. None of that is

24:06

bloating the conversation in the context

24:08

of the main agent. It's only coming back

24:10

with the result. And this is how you get

24:12

to a product that feels like it has

24:15

infinite context. Obviously there

24:17

infinite context is not a real thing.

24:19

Every model has a context limit. But

24:20

there are these tricks and workarounds

24:22

such that especially when you train

24:24

models to get better at this that it

24:25

doesn't feel like you're running up

24:27

against the limits of the context window

24:29

and you don't feel a degradation of the

24:31

model's quality as you get further along

24:33

that as well. So we have these different

24:35

helpers each with different skills. Each

24:38

one of these is its own durable

24:40

workflow. So, going back to the cues

24:42

versus workflows thing, that means if it

24:44

fails, the video helper crashes uh

24:46

something, you know, uses up too much

24:48

memory or something that can restart and

24:50

keep going without losing progress along

24:52

the way. And then the main agent, it is

24:56

kind of like a co-orker where it's

24:58

managing a to-do list. You have a few

24:59

things you can do. You can go check on

25:01

it. You can send it a message, you know,

25:03

you can stop the helpers, for example.

25:05

Um, and so a lot of this is modeled

25:07

after the ideal way you would scale, you

25:09

know, a team of engineers working on a

25:11

very complicated problem. So, uh, like

25:14

just a few little interesting tidbits

25:16

from building this out on the team. Um,

25:18

I think we'll talk a little bit more

25:20

about prompt caching here, but um,

25:23

ideally you don't add or remove new

25:25

tools during turns. Um, and just like I

25:28

mentioned with the slide with all of the

25:30

uh boxes, if you have extremely long

25:32

tool descriptions or schemas, that's

25:34

going to take up a lot of space in the

25:36

context window, which is bad for

25:38

pricing. It's bad for usage. Ideally,

25:40

you want to minimize that. So, there's

25:41

plenty of optimizations you can make

25:43

here on the engineering side to just

25:45

trim that down. Um, another interesting

25:48

one is like you might hear people talk

25:50

about how, oh, like we can have a

25:51

harness, it just has one tool and that

25:53

one tool is like running shell commands.

25:55

And this is true and you can model a lot

25:57

of things this way, but if you see the

25:59

model doing, you know, 90% of its time

26:01

doing shell commands for this one thing,

26:03

it actually can be more efficient to

26:04

make a dedicated tool for some of that

26:06

stuff. Um, and it's it's a bit more

26:08

legible for people actually reviewing

26:10

[clears throat] how the product is

26:11

working. Uh, and the last one's kind of

26:13

funny, too. Uh obviously if you're

26:15

writing errors for humans it's helpful

26:17

to make it very descriptive like okay

26:18

this thing failed like click on this

26:20

link or here's what you need to do next.

26:22

Turns out that's also very helpful for

26:23

agents. So there there is some DX in

26:25

designing um good error messages for

26:27

agents too.

26:30

And when you're trusting your bots to go

26:33

and do work over a long period of time

26:35

it is very important that you trust them

26:38

to be secure with your information to be

26:40

secure with how it operates. So even

26:43

working on its own separate machine not

26:45

polluting anything on your machine still

26:47

you would like it if you know a model

26:50

reviews every shell command before it

26:52

goes. So auto review it would be great

26:54

if you know when these untrusted inputs

26:56

like a email in your inbox could be a

26:58

fishing email or some web hook. You

27:00

don't want to send that to the model and

27:02

act like it was a user instruction. And

27:03

there's plenty of work to do here uh in

27:05

the industry on just making sure that

27:07

that is uh properly protected from um

27:10

prompt injections. um for these kind of

27:14

uh non-reversible things like sending

27:16

emails, right? You want to really ask

27:17

the user first and then also you can

27:19

build in some workflows into the product

27:21

to keep passwords and other things

27:23

outside of the model conversations.

27:26

Okay, context engineering is

27:30

also known as harness engineering. Like

27:32

I feel like it's kind of the same thing.

27:33

they've evolved over time, but basically

27:35

what are some tips and tricks we can do

27:37

to minimize the amount of context that's

27:39

being used in these harnesses. Um, which

27:42

is good for efficiency, for reducing

27:44

costs, um, and other reasons. I talked

27:47

about prompt caching. Um, this is a good

27:50

example of why it's really important. So

27:52

if you think about when you're working

27:53

with a coding agent or any agent, most

27:56

of the tokens used are actually ideally

27:59

cached tokens because you're sending it

28:02

a conversation and then you're doing a

28:03

change. You're you're adding one more

28:05

message and then you're basically

28:06

resending the entire conversation over

28:08

again. So if you look at the pricing for

28:10

models, there's huge discounts of course

28:12

on if a token is cached and you want to

28:14

keep them in the cache window as long as

28:16

possible. And so a way to do this is you

28:19

try to keep the tool definitions, the

28:22

system prompts, all of this information

28:24

as static as possible between different

28:27

calls so that you don't break the cache

28:29

and then have to pay for the uncashed

28:31

tokens every single time. So if you have

28:34

that fixed part at the top, then you

28:36

know when you get down to the bottom and

28:37

you only have the newest message that

28:38

comes in, that's the only delta that

28:40

you're paying the uncashed tokens for.

28:42

And there's a a whole art and science uh

28:45

to this as well.

28:47

basically because that main agent in the

28:50

most ideal sense, this is the one you're

28:51

having this very very very long

28:53

conversation with that's compacting or

28:56

compressing so summarizing the context

28:58

many many times over uh without you even

29:01

noticing sometimes which we'll show an

29:03

example of. So you really want to keep

29:05

that context small. Um some ways of

29:08

doing that is anytime there's a very

29:10

long output rather than stuffing that in

29:13

the context you can just put it in a

29:14

file and the agents are very good at

29:16

reading files. Uh so this actually works

29:19

pretty well which is just like reading a

29:20

skill or reading a memory for example.

29:22

If you give the model a screenshot um or

29:26

some other very expensive work you just

29:28

delegate that out to a helper again so

29:30

it goes to the sub agent and it doesn't

29:33

affect the main agents context. Um, and

29:36

then also in an ideal world, you want to

29:39

kind of be always gardening and trimming

29:42

and removing unnecessary stuff from this

29:44

conversation, from this context window

29:46

as you're going, both for cost, but for

29:49

also recall and just making sure the

29:51

model is performing well. So, if there's

29:53

a cash cache miss, it's kind of like a

29:55

bug in the system really. Um, and if if

29:59

you go all the way back to uh all the

30:02

way back to January of this year, uh

30:04

when OpenClaw was really getting popular

30:06

and people were using it uh inside of

30:08

subscription plans for uh model

30:11

providers, this was a very new usage

30:13

pattern that model providers hadn't

30:15

really seen and the the harness hadn't

30:18

really yet been optimized for this type

30:20

of workflow. So, there was a lot of

30:22

cache misses. And when you're doing

30:23

something like this where agents are

30:25

running all the time, that's obviously

30:27

problematic. So, lots of fixes uh had to

30:29

go in to make that robust to keeping the

30:32

cache uh uh keeping the prompt cached as

30:34

much as possible. Um, so like a fun uh a

30:37

fun trick here is just trying to keep

30:40

everything in the prompt in order such

30:43

that even if you deploy a new change

30:45

where you want to test out some new

30:46

change to the system prompt or add a new

30:48

tool, you can you can almost hash each

30:51

section and always keep a fixed order.

30:53

Like let's say you're doing a AB test

30:55

and you want to put a new piece of the

30:56

prompt in there. You really don't want

30:58

that to screw up the prompt order for

31:00

everybody else. Otherwise, you just

31:01

busted the cache. Uh, and then that's

31:03

that's problematic. Um, another fun one

31:06

is when you open the chat and you start

31:08

typing, um, the server can actually go

31:11

and prepare the prompt early, uh, to

31:13

basically warm the cache. Almost like if

31:15

you're on a website and you go to hover

31:17

on a link, it can go and prefetch the

31:18

next page. Um, so there's like tons and

31:21

tons of little engineering optimizations

31:24

that compound how far these models can

31:27

go in terms of token usage, uh, and how

31:29

smart they can be when you use them for

31:31

very long periods of time. And the key

31:33

thing is that when you're working over a

31:35

very long period of time, you

31:37

essentially have to get very good at

31:39

compaction or summarization because

31:41

let's say the model has a 200,000 token

31:44

context window. You're going and going

31:45

and going. Eventually, it's going to hit

31:47

a point where it needs to summarize and

31:49

all compression is lossy. So, you have

31:52

to figure out the best algorithm to do

31:54

the compression and not lose important

31:56

details. And there's, I'm sure, entire

31:59

uh PhDs just for this problem. like it's

32:01

it's very very tough to do, right? But

32:03

just to show an example, like let's say

32:04

you're going back and forth with your

32:06

with your bot. Uh you have over 100,000

32:09

tokens uh and then you go and do

32:11

something else. Well, there is this time

32:13

period where the model providers have uh

32:15

like a cache window when the cache is

32:17

still warm. Maybe it's 10 minutes, maybe

32:19

it's 60 minutes. Uh one trick that we do

32:22

which I think is really helpful is while

32:23

you're still within the warm cache

32:25

period, you should probably do the the

32:27

compaction right now because it's you

32:29

have this long conversation. it's going

32:30

to be much cheaper. So just go ahead do

32:33

the compaction and now when the user

32:35

comes back in 30 minutes or whatever

32:37

they're starting from this summary and

32:40

the summary now is going to be much

32:42

cheaper for them to continue adding

32:43

messages on. So lots of work to do there

32:45

to make it u to make the algorithm very

32:48

high quality for how you do the

32:50

summarization. When you talk about

32:52

compaction and what information you

32:54

choose to include in the agent's memory,

32:56

I kind of think about it in a few

32:58

different layers. The first one is like

33:02

facts about the user that are very

33:04

important to be included every single

33:06

time. Going back to earlier when I said,

33:07

"Oh, like the user always wants to have

33:10

lowercase text." That probably needs to

33:12

be in every single message. Otherwise,

33:13

it would be really weird if one message

33:15

now has uppercase letters. Uh if you're

33:17

going like full Sam Alman style, um then

33:20

uh you probably also have some prompts

33:22

that you want or some facts that you

33:23

want to include in there. And there's

33:26

many different ways you can make this

33:28

algorithm to determine what are the most

33:30

important facts to keep. So ideally

33:32

there's this kind of garbage collection

33:34

of this fact. And like if you think

33:36

about it, this is kind of how our brains

33:37

work. It's like this fact right now is

33:39

important to keep. It's relevant for

33:41

this conversation today. You're probably

33:43

not thinking about, you know, a soccer

33:45

match right now. Uh and your brain is

33:47

very good at keeping those ones in

33:49

context right now and kind of

33:50

remembering them for later. We try to

33:52

model some of that here where the the

33:55

the harness has of course these core

33:57

facts. It also has these dated logs of

34:00

kind of your past conversation history

34:03

essentially and then also these kind of

34:05

scratchpad notes. So just like little

34:07

things that you've been working on right

34:09

now that are designed to be ephemeral

34:10

and they will go away but they might be

34:12

relevant just for the next 30 minutes or

34:14

an hour. And everything else can be

34:17

found through files. This is the magic

34:19

of files of the models being good at

34:22

running commands is like they can always

34:23

just go look stuff up and they're

34:25

actually very good at doing that. So

34:27

where where is this going? There is this

34:30

flywheel of training the model to get

34:33

better at the product and just as a

34:36

highle refresher on kind of the stages

34:38

of training a large language model. Uh

34:40

first you have pre-training where the

34:42

model learns uh a general understanding

34:45

of the world from large amounts of

34:47

internet text or other private data.

34:50

Then you have supervised fine-tuning

34:52

where you're teaching the model how to

34:54

behave uh in a certain way maybe as a

34:56

chat assistant or as an agent. And then

34:59

finally you have reinforcement learning

35:01

where it's kind of like teaching the

35:02

model how to play games and get better

35:04

at those games. Um, and in the context

35:07

of these always on agents, it's very

35:09

helpful, for example, to train the model

35:12

to see examples of the harness and to

35:14

know how to work inside of the harness

35:16

in SFT. Um, so it can understand the

35:19

tools that you have available. It's

35:20

helpful to train the model to be better

35:23

at instruction following during

35:25

reinforcement learning. How to

35:26

understand the right skill to call when

35:28

there's hundreds of skills, for example.

35:30

how to click on the right spots when

35:32

you're given in a browser and it needs

35:34

to kind of click around. Um, and not

35:37

only the training itself, but also how

35:39

you serve the model, the inference that

35:41

you do. The main agent you're talking

35:43

with, you know, ideally you want it to

35:45

have very low latency. You want it to

35:46

come back to you right away, give you

35:47

really fast replies, and you can tune

35:49

the harness and the inference to do

35:51

that. But then when you go hand it off

35:52

to a helper and you're asking it to go

35:54

do some work for a very very long period

35:57

of time, the difference between 10

35:58

minutes and 12 minutes is kind of

36:00

negligible at that point. So that allows

36:02

you to kind of control uh how you

36:04

optimize things. Um the way that this

36:07

kind of flywheel works is that let's say

36:10

your agent gets something wrong. Um

36:13

ideally then that gets turned into some

36:15

kind of evaluation. So, uh, the model,

36:17

you know, accidentally, uh, called this

36:20

tool and it wasn't supposed to. Um, then

36:22

on the back end, you can go and fix the

36:24

harness. You can fix the bug, you can

36:26

fix the prompt, maybe it's an inference

36:28

issue. Um, you can check and make sure

36:30

it's actually fixed in the eval, and

36:31

then ship the change. Uh, and you know,

36:34

the teams uh, and the products uh, the

36:36

teams working on these products, they're

36:38

kind of doing this every single day.

36:39

They're finding bugs, they're fixing it,

36:40

they're measuring it with eval. That's

36:42

kind of this inner loop that you're

36:43

always getting better at. But then

36:44

there's this outer loop which is like

36:46

every few months or every month or

36:47

however long um you're training new

36:49

models and ideally the new models are

36:51

learning from all of these bugs and

36:53

these failures and they're kind of

36:55

climbing on the evals to get better at

36:56

the things that you care about and you

36:58

can drop the new versions into the

37:00

product and make sure that they're

37:01

improving at all these different things.

37:03

And the fun thing about this is then

37:04

when the new models come out they have

37:06

new capability jumps. Um it allows you

37:08

to use the product in new ways. So there

37:11

hasn't really been a lot of training

37:12

data for example on using bots in a

37:15

group chat. This is kind of an emergent

37:17

thing that's being figured out. Uh and

37:19

it's important to get it right.

37:20

Obviously if you've seen things like

37:22

multbook or the hugging face incident

37:24

like there are ways that that goes

37:25

poorly. So it's important to be able to

37:27

train the models how to behave well in a

37:29

multi- aent system where uh different

37:32

bots are talking to each other for

37:33

example. Um, and so ideally you're

37:37

always kind of running these inner loops

37:38

and outer loops as a product team

37:40

building these type of harnesses at a

37:42

company uh working on on AI together.

37:45

Um, one of the principles that we had at

37:48

Cursor uh and now at SpaceX is to delete

37:52

the product. And what we mean by this is

37:54

you kind of have to internalize and

37:56

assume that the models today are the

37:58

worst they'll ever be. which means that

38:00

in six months uh you might need to

38:02

completely rebuild the UI. Like most of

38:04

the stuff that I've talked about today,

38:06

uh it's a pretty big update from if I

38:08

would have given this talk six months

38:10

ago or definitely a year ago because

38:12

we've learned a lot of uh new practical

38:14

ways of building harnesses and doing

38:17

context engineering. So a good strategy

38:20

here is you really want to try to build

38:22

for the next generation of the model.

38:24

like how do you build as little as

38:26

possible and get by uh while making just

38:29

a really really simple interface to work

38:30

with these models and allow them to uh

38:33

to to think and act for you. So um some

38:36

things that I think will happen in the

38:38

next 6 to 12 months. We'll see. I think

38:41

these are decently safe bets. But uh you

38:44

know right now let's say you have your

38:45

agents or your bots working for maybe a

38:48

few hours, maybe a day depending on how

38:50

tough it is. I think it's very possible

38:52

that pretty soon um your agents will be

38:55

working for weeks or months. Uh and it

38:58

does bring up some kind of interesting

39:00

questions in terms of how we train and

39:02

evaluate those models. I think the

39:04

models are going to get better and

39:05

better about how they recall on past

39:08

information in in conversations. Plenty

39:11

of different approaches in research here

39:13

for the type of algorithms used or the

39:16

representation of the data under the

39:17

hood. Maybe it's a graph, maybe it's

39:19

files, maybe something else. Um, I think

39:22

ideally if you have this digital

39:23

colleague, you give it literally all the

39:25

tools that you have if you were

39:27

onboarding a real human. And so I think

39:29

organizations are still warming up to

39:31

that idea, of course. So there's a more

39:33

to do there. I think the interfaces uh

39:36

will maybe get even easier. I think some

39:38

people will prefer to just talk to their

39:40

kind of bots or agents uh as a in a

39:44

speech-toech way. Maybe they'll have

39:45

their like uh Johnny IV device that is

39:48

hopefully really cool and they can like

39:50

talk to that uh all day. Um I think the

39:53

models and the products will get really

39:55

good at um learning skills on the job

39:58

and then encoding that into files in

40:01

instructions skills uh and taking that

40:04

raw intelligence and actually making it

40:05

useful for whatever uh whatever task

40:08

you're asking it to do. and people and

40:11

companies um are going to get much

40:13

better I think about uh writing down how

40:16

all of these kind of bespoke processes

40:18

work so that they're legible for agents

40:20

to use. So some interesting problems I

40:22

think that like could just spark ideas

40:25

for you all to to research or to look

40:27

into. The first one is like going back

40:28

to this idea of agents that run for a

40:30

month. What do you do when you need to

40:32

evaluate an agent that runs for a month

40:35

but the models ship every month? you

40:37

know, it's like [laughter]

40:38

you can't fully evaluate that the model

40:40

is uh sufficiently uh behaving as

40:44

expected. So, you either have to figure

40:46

out how to do a better eval or you know

40:47

not ship models as fast and I think

40:50

that's going to be a problem that the

40:52

industry has to figure out in the next

40:53

six months. Um memory obviously we

40:56

talked about the different algorithms. I

40:57

think there's so much to do on the

40:59

security side for how to make a secure

41:02

harness, how to make the infrastructure

41:04

[clears throat] around these agents um

41:06

very secure uh and so they can't be

41:08

taken over by bad actors. I think we

41:11

didn't really talk about it too much,

41:12

but the heristics around when the agent

41:15

is silent or when it kind of bugs you,

41:18

uh I don't think that we have it perfect

41:20

yet. And I think trying to thread the

41:22

needle between being proactive and

41:24

suggesting like, "Oh, hey, like I saw

41:26

that you missed class yesterday. Like I

41:28

I had a recording there and I took the

41:31

notes and like here's the thing." Like

41:32

maybe that's helpful or maybe that's

41:33

kind of annoying, you know? And like

41:35

trying to figure out the right the right

41:36

balance there.

41:38

Um, I also think that it's a pretty safe

41:42

bet to assume that at this time next

41:44

year there will be significantly

41:46

significantly more tokens flowing in the

41:49

world which means that optimizing the

41:51

inference optimizing the models

41:53

optimizing the harnesses trying to drive

41:55

down the tokens to uh be more efficient

41:59

will also the token usage to be more

42:01

efficient I think will also be very

42:03

important. So there's kind of these new

42:05

building blocks to think about. I

42:07

believe it was Alex from Open Router

42:09

that I saw on on X. He had a really good

42:12

take on this. There was people saying

42:14

like, "Oh, every app today it looks the

42:15

same. You've got the sidebar, you know,

42:17

you've got the agent when you got the

42:19

chat box. Like everybody's building the

42:20

same thing." And he was like, "Yeah,

42:22

that's like in 2005 you said everybody's

42:24

building the same thing. They've got a

42:26

server, you got a database, they've got

42:28

some like CRUD interface to like update

42:31

and delete things." And the reality is

42:33

this is just kind of the new normal now

42:35

where every new product is going to have

42:38

to have some kind of uh agentic side to

42:40

it either integrating or it will become

42:43

a uh a you know a headless piece of data

42:46

for another agent to use. Um, and so

42:48

we've talked about a lot of these

42:50

products today. Durable workflows, um,

42:52

sandboxes, computers in the cloud,

42:54

memory, skills, uh, connecting

42:56

[clears throat] to other services, doing

42:58

voice, payments, identity, evals,

43:00

observability. Each one of those boxes

43:02

is like multiple billions of dollars of

43:04

VC capital, you probably trillions.

43:07

There's 20 startups for each one of

43:09

those. And there's so many different

43:11

things to explore and all of those

43:12

because they kind of are the new

43:13

primitives for the next generation of

43:15

companies, the next generation of

43:17

products, and they all have to integrate

43:19

and be part of harnesses. So there's

43:21

there's definitely a lot to do there. A

43:23

few things uh to take away and then

43:25

we'll have time for questions.

43:26

Obviously, you all are engineers. You're

43:28

thinking about how to write software.

43:30

It's more important than ever to think

43:31

about the broader system design and the

43:34

architecture of the code that you're

43:35

working on because that the decisions

43:38

you make there will really compound. Um,

43:40

so I'm still I'm still pro looking at

43:42

code. You know, I still look at the code

43:44

that I'm writing. Some people not

43:45

looking to code. I still think it's

43:47

helpful, especially to understand the

43:48

architecture that you're shipping. Uh,

43:50

it's crazy how much that's changed in

43:51

just a couple years. Um, secondly, you

43:54

know, it sounds like y'all were kind of

43:55

messing around with building own

43:57

versions or understanding of how the

43:58

harness worked in uh, last week. Uh, I

44:02

encourage you to think about some of the

44:04

things that I did today and try to apply

44:06

that and think about how you might build

44:08

a version uh, for yourself. I'm pretty

44:11

sure there's like open grobbot on GitHub

44:13

somewhere where somebody's like, you

44:15

know, slot forked our code and made and

44:18

made a version. Uh, and you could

44:20

probably set up like the the guey for

44:22

that with, you know, a sandbox somewhere

44:25

and, uh, you know, a a VM and like all

44:28

sorts of different things. Um, so that

44:30

could be interesting if you want to

44:31

learn a little bit more about how these

44:32

pieces work. And then finally, um, I

44:36

would not shy away from just learning a

44:37

little bit more about how LLMs work. You

44:39

know, we gave a little bit of the TLDDR

44:42

here of how they're trained, but there's

44:44

so much more you can go into here

44:46

without necessarily going deep in ML or

44:48

deep into the science part of it. But I

44:50

think it's helpful to understand just

44:52

like when you're building a web app,

44:54

it's helpful to know how databases work

44:56

because increasingly the there are so

44:58

many decisions you make at the product

45:00

in the harness uh level that are

45:02

influenced by the models themselves. And

45:04

having an intuition of how that works, I

45:06

think will make you very well positioned

45:08

for uh whatever role you're operating

45:10

in. And uh that's all I have today.

45:13

[applause] Thank you.

Continue with YouTLDR

Analyze another video with Pro

Process a new video, search every timestamp, compare sources, and keep the result in your library.

Get Pro — $12/month30-day money-back guarantee

More transcripts

Explore other videos transcribed with YouTLDR.