0:00Like I could optimize the agent to be way faster by saying, do nothing and give me a stupid answer, but that's not the kind of optimization we want to do here.
0:16Should I take one second to appreciate whoever got this 3,000 300 lb rock board up on the stairs? We don't really think about that. So, my name is Jedaya. I've been working on Cursor's agent harness, everything that controls the model since early 2025. And now I work on the Grok harness. Today I want to talk to you all about how we make an efficient agent harness.
0:44When we're building the agent, we ask ourselves, how do we make it run fast?
0:51And how do we make it run as cheaply as possible?
0:56So cost and speed are going to be a big focus today. Quality is obviously really important too, but I'm not going to go as deep into that today. Before I dive in, I want to give a quick recap for those of you who need it on how a standard agent turn works. [music] So an agent turn is made up of a series of steps.
1:19Each step processes the conversation that we've seen so far, and it ends with the next action, which is typically a tool call. And some of this uh some of each step is cached. Everything that has been processed in a previous call doesn't have to be processed again. This is all the cached portion. And only the uncached portion needs to actually have inference run.
1:43And so over a turn, we have a bunch of steps, and in each successive step, more and more of this conversation gets cached. Eventually we might run out of the context window available to the model, and then we do compaction. And compaction is when basically summarize everything that has happened so far in the conversation, and we bring it into a more compressed format so we can start the process over again.
2:12So the tokens that we put into the model and the way that we manage the cache are extremely important for controlling the cost and speed of [music] the agents.
2:28So I want to talk about some of the numbers that we care about when it comes to cost and speed. And I'm going to write a few equations to kind of explain how we look at these. And I know some of you are freaking out about the equations. These are what we call in the biz engineers equations, which means that they are not very precise and they're just kind of giving us a heuristic vibe on these metrics.
2:54So the first one is of course the cost to return. When I give the agent a request, how much does that actually cost?
3:03So one of the most obvious factors in the cost per turn is the price per token, you know, that we are being charged by the provider. Typically that's, you know, set by the provider and maybe we can get a discount depending on what company we are and so on. That's obviously a big deal.
3:18And then that gets multiplied by every single step. We add up the cost of the uncashed tokens and the cash tokens, which usually get a big discount. [music] Now, despite us getting a big discount on the cash tokens, uh it turns out that cash tokens are often per agent or close actually where more of the cost goes. And that's because the uncashed tokens we only pay in a linear way.
3:49In each successive step we pay another piece of uncashed token, another piece of uncashed token. For those cash tokens, they actually grow quadratically because we send them over and over again. We pay these cash tokens, then we cash some more, we pay those again on every single step. And so those cash tokens actually result in a lot of costs, too.
4:05So I'm going to write down cost here, and and then some of these like variables that we can leverage that we can pull and how they affect that cost. So first of all, one of the most powerful levers that we have is to reduce cash misses. And if we reduce cash misses, that's going to shift more of these un cash tokens to be cashed to make them cheaper [music] and bring down our costs.
4:33Something else we can do is reduce the total amount of context that flows through each step. We can reduce tokens. And most of those tokens are going to be cash tokens in in the limit. And that will also bring down our cost. Something else that sometimes we can do is maybe we can negotiate a discount. We can bring down the price per token to reduce our cost.
4:56And then the last thing is we could bring down the number of steps that the agent takes. We can have it do the same amount of work using fewer steps, fewer calls to the model, which will also bring down costs.
5:19Now let's talk about speed. There's actually two ways that we measure speed that are really important. The first one of those is time to first token or TTFT. TTFT is how long it takes after I send a request to the model for me to start getting something back.
5:37And that controls whether your app, you know, whether GraphQL or Persist or whatever your workflow is, feels really snappy. Does it feel like I send a message and something's happening, or does it feel like I'm just waiting and I'm going to go bounce?
5:50So our un cashed and cash tokens once again figure into time to first token, but in different ways. The un cash tokens, they actually need to to processed. They need to inference product. And that is a compute-bound process. That's actually limited by the number of like flops that we have or someone has on the server.
6:14For the cached tokens, they don't need to be reprocessed in that way. We just need to pull those processed tokens out of a cache in memory somewhere. And so these are memory bounds. Memory is a much faster process than compute. And so typically it's the uncached tokens which can have a much bigger impact in reducing your time to first token. So we add time to first token here.
6:41So with time to first token, one of those impactful things we can do is reduce the cache mystery because we're going to shift away those uncached tokens. And then maybe not as much, but to some extent reducing the total number of tokens, reducing cache tokens can also improve time to first token. And then this has a few new steps for cost.
7:03Now the other thing that we care about when it comes to speed, which is arguably even more important, is the end-to-end latency.
7:12How long does it actually take to complete my task?
7:17And so the end-to-end latency, we have to pay a time cost on every step of the turn. And what do we pay? Well, we pay that time to first token. We pay the time to actually run tools. And we also pay the time to generate the model's output, to do inference. And that could vary depending on how much output the model actually gives. Maybe it's thinking on max effort or maybe it's thinking on very low effort.
7:47Uh and also what speed we serve the model on. Sometimes we can serve it at, you know, 200 tokens per second or only 60 tokens per second. And so for the end-to-end speed, bring down the the number of cache misses that improves TTFT, which helps on every step. Same with tokens. Cost is configured here.
8:09And then bringing down the number of steps, for example, using a lower reasoning effort if it's still smart enough, that's one most impactful things that you could do to bring down the end-to-end speed. Understanding these variables and how they relate is really helpful to guide us to optimize an agent harness.
8:41But it turns out that it's not enough because optimizing an agent harness is a very experiment-driven process. We need to actually measure and profile what is going on. And so one of our like most powerful tools that we have, and we're doing it all the time, is running AB tests. And the AB tests are useful for a few reasons. First of all, they allow us to actually measure over the full distribution of our users' workloads.
9:14And then they also allow us to measure our quality metrics too, which like I said, I'm not going to go too much in detail on. That could be its own talk.
9:22But our quality metrics make sure that when we are optimizing the speed and the cost of the harness, we want to make sure we're not just making the agent run worse. Like I could optimize the agent to do it faster by saying do nothing and give me a stupid answer, but that's not the kind of optimization we want to do here.
9:37So with these AB tests, we're often trying to measure, okay, what is the bottleneck in our system?
9:45Is it cache misses? Tokens? Is that? And then we try to propose experiments to fix it. Now I'm kind of explaining how these levers help in a vacuum, but it turns out that often we when we propose experiments, there's trade-offs. I might make one of these things better and I make make one of them worse. And so we have to actually make sure in our workflow that this is helping.
10:15Let me talk about some real examples of experiments that we could do. And the first one is a real example for Grokbot. So for Grokbot, we saw that the cache hit rate, the number of cache misses was much higher than we were seeing in the first and that was hurting costs, that was hurting speed. And so we figured how can we improve that?
10:37Well, first of all, there's some kind of like three things that you should always do that really help the cache hit rate, which is stabilizing the prefix.
10:50So what do I mean by that?
10:52When you have this all these cache tokens, if I were to change a token right here, that's going to invalidate all of this cache. So I really want to make sure I'm not touching those as much as I possibly can.
11:04And then also across different users, I want to make sure that if there's anything in that cache which is in those tokens which is volatile at the time, I want to put that as far away as possible so we can get the biggest cache as possible on the first request they send. So we did [music] that.
11:19And then the second thing we did, which was maybe even more impactful, we realized by looking at the way that the Grokbot harness works, [music] Grokbot is not a typical single agent, but rather it's a coordinator agent that delegates to sub agents. So this is my cute little Grokbot. Let say I send it a request.
11:39It might make a few tool calls to like kind of understand what I'm talking about and then pretty quickly it's going to delegate that to a sub agent. And I'm going to add a little timeline here. Let's call this like 5 minutes, 30 minutes, an hour.
11:56And so the sub agent is going to receive this request. It might work on it for a while, maybe even half an hour, and then it's going to say, hey Grokbot, I'm done. And then Grokbot will say, hey user, your task is done.
12:07And at that point, [music] what happens?
12:11Well, Grokbot hasn't done anything for 20 minutes. The subagent was working that whole time. And so we come back to Rockbot, we get a cache miss, and we pay for the full cost. And so this was like the source of the bad cache hit rate for Rockbot.
12:27And so what can we do?
12:28Well, something that we could do is we can sometimes actually make the lifetime of the cache longer. We could increase the cache TTL. And we use different models in Rockbot.
12:39So uh we have the power to do this for Rockbot, of course. We use Cloud sometimes too, and they offer this as a parameter where you can change the default lifetime from 5 minutes to 1 hour. And so if we have a 1 hour cache, then all these tokens they last just long, and by the time we get back here, we get a cache hit.
12:58Now, why wouldn't we just always do that?
13:02Well, it turns out, at least as far as Rockbot is concerned, this cache TTL, though it does reduce misses, this actually is going to increase your price per token because they charge you 25% more for the privilege of a long cache.
13:17And so you have to be careful because that could actually swamp the gains that you make from having more caches. And so we ran this experiment, we measured it, and we found out that for the Rockbot coordinator, this was a huge cost win. Um we were not even sending that many tokens to the coordinator because a lot of them are just delegated to the subagent. And we were seeing a lot of cache misses due to that async nature of the product. And so making that long cache could we save money. [music] But for the subagents, it
13:46was actually the opposite. The subagents were having a much better cache hit rate to begin with because they were doing a lot of continuous work not very async.
13:55And they were also processing way more tokens. So that extra 25% markup actually totally dominated the improvement in the cache hit rate and made these subagents cost more. So by measuring that, we were able to understand what the right thing to do was, which was use a 1 hour cache lifetime for the parent and keep the 5 minutes for the sub agents.
14:21Okay. So what else can we do? Let's say that tokens is a problem for us and we say, okay, we want to reduce the amount of tokens that are processing are flowing through our agent system. Well, we can artificially lower the context window. Essentially, we can compact more. Instead of saying let's use 1 billion context, we can use 250k context, for example.
14:47And compaction is quite good now, so this is not nearly as much as a quality hit as you would think. And this means that as our cache as our prefix grows, the cost of a cache miss is less and the quadratic scaling of these cache tokens also is less. There is a downside there. That's not always a free win.
15:06The quality aspect is one thing. And then also when you when you compact more, you take more steps because sometimes if you took something out of the context, the agent might need to go back and fetch it again and take more steps.
15:20And something else you can do to reduce tokens is use more dynamic context, which means taking stuff out of your system prompts and moving it to skills or taking tools away and allowing the agent to find them by searching for them.
15:35Similarly, that removes context in every step, but it can add more steps because the agent then has to go find those skills, has to go find those tools, right?
15:43So everything has trade-offs.
15:45And then maybe [music] I'm using a very expensive model. Maybe I'm using Fable and I'm saying, okay, do I need to use Fable?
15:52And of course, the solution there is we can use a cheaper model. We want to measure that and make sure that cheaper model is still smart enough for our workload. However, the cheaper models, we've seen time and time again that they do the same work also [music] with more steps.
16:14Some of you might remember when Sonic 5 came out, a lot of evals actually showed that it was no cheaper than because it actually took way more steps to do the same work and that just made it the same price in the end. So it's like why would you use And then I don't have space to write bring down steps, but you could for example lower the reasoning effort as well.
16:35Overall, these are just a few things you can do and I haven't talked about all the possible experiments you can do, all the possible levers you can do, but what I want to emphasize you know just to summarize optimizing nature harness is an empirical process like science. You want to use your understanding, use your measurement of the system to guide you to experiments. You want to form hypotheses and then you want to measure them in the
17:08So I would encourage you to build up in your own organization, your personal work, your own muscle for designing and running experiments. Even if you are not building an agent harness, you can still use this way of thinking to optimize your own workloads to be much faster, to be much cheaper, and to be as good as they can possibly be. So thank you all for being here today.