Tool Use in Language Model Agents
Transcript
OK, so hi everyone. Welcome to class number two. Today I'll be talking about tool use for language model agents. And this is the most fundamental thing about agents that makes them different from a language model. So the basic idea here is that we're going to be, a tool is basically an interface through which a language model can invoke an external computer program.
And this is, a language model cannot do this on its own because it's fundamentally a token predicting machine. But this enables you to do many, many different things. So you can search. You can call a calculator. You can call a browser. You can call Python. You can call arbitrary APIs. And so the language model, as you'll see, the method for tool calling is standardized. But what you do downstream with the tool call can be very different.
So there's basically two benefits of tools. And all of this is from one of the readings that I posted on the website, if you want to go back and read more about it. This is a survey that we wrote together, Daniel and I and Zora Wang, one of our students. The first thing is extending the ability of language models. So this is giving language models the ability to do something that they fundamentally cannot do. And so this could be like accessing information or actions that are outside the language model's action space.
The second thing is facilitating the language model. And so what I mean by this is this is something that fundamentally the language model can do, but it's just a lot easier if you don't require the language model to do it. Can anybody come up with an example? Well, I guess we already have an example in here. So one thing the language model fundamentally cannot do would be get the time, four months ago or something like that. So the most recent information it saw is from four months ago, and then its parameters are frozen. So there's no way it could know that without calling an external function.
On the other hand, something in the realm of facilitating is language models can do math actually quite well nowadays, but they take a lot longer than calling a calculator to do math because they have to do reasoning like you ask them to multiply two seven-digit numbers, and it will take them a very long time to do it where a calculator can do that really quickly. So those are the two kind of basic categories. So to go a little bit more deeply into those, you can do things like look up current information. And the benefit of this is this gives you fresh evidence to ground your answers.
Do exact computation using calculators or Python. This gives you the ability to execute more reliably and quickly than using the language model itself. Other things is access private states, so giving the model access to information about the user so they can respond based on the user state, or make a change to your external environment. So this could be browsing or calling an API. So to have some examples of these, If we think about ChatGPT, ChatGPT used to literally just be text in and text out when it first came out.
The only thing that it could do was give you textual responses to your queries. Now it's kind of an everything app. It allows you to do so many different things. So that's another specialized API. So all kinds of different things. I kind of categorized these into a list of things that I think are the most typical tools. And most of the other tools can kind of be categorized into each of these. So the first one is textual responses. So this is kind of the boring one, where it just gives you an answer.
There's two ways you can implement this within an agentic loop. By the way, ChatGPT is an agent now. If there was any question about it, ChatGPT is not just a chat app. It's an agent because it makes iterative tool calls to answer your queries. So that's kind of the definition of an agent. But textual responses, there's two ways you can implement these. One way you can implement these is you can have a finish tool that says, OK, I'm done interacting with the user, so I'm just going to finish and output some text. The other way you can do it is if the agent decides to not call the tool, you can just finish the agentic loop. So that's kind of the simple, most obvious one.
Another extremely common one, so all of these are tools. And so theoretically, all of them can be thought of as an API, but just to break them down a little bit more. Another one is searching the web. And this is pulling in information from all of these different settings. How many people have heard of retrieval augmented generation? Probably everybody who took the NLP class here has heard of it, but a lot of people. How many people have heard like rag is dead?
a fairly large number of people. So there's been an argument in language model circles that retrieval-augmented generation was so two years ago and nobody cares about it anymore. My argument here is retrieval-augmented generation, where you retrieve context and then generate answers based on it, has become so normal that people don't even realize that they're using it anymore. And so it's so alive that people think it's dead. But basically, searching the web and retrieving information and then generating answers based on it is now essentially the norm within these applications now. So that's another tool that's provided very, very broadly in any chat interface.
Another one is code execution. And so this is throwing in a set of code to do calculations or something like this. Very useful for computation, all the other stuff we talked about. Another common one is having some sort of image generation. And this is obviously another very popular use case in chat GPT. This one's a little bit different, because like all of the other ones here that I was talking about, basically mostly interface with text.
Here, this is interfacing with images. One interesting thing. is you'll notice that this is not the same thing as here. So this is like watercolor robots studying in a library where the query was actually different. A big part of calling these images is actually deciding what query to put in there. So the language model will really, really expand the query before it calls an image generation API. I don't know if you can see this anymore in ChatGPT, but you used to be able to see in ChatGPT when you downloaded the image that it would have a very different caption than the caption you gave. And you could kind of latently see what query the image generator sent to the generate image tool.
Then there's all kinds of custom functions, like buy me bananas, milk, and coffee. So this could create a grocery cart for you and call the Instacart API to do this. And you can basically go as wild as you want with all of these tools and add whichever ones you want there. OK. There's a bunch of different ways you can think about tool calling. The first way you can think about tool calling is just you provide a list of 50 or 70 API functions that the agent is able to interact with. And it can call these APIs.
And if you do that, basically what you're doing is every step, you're calling one API. Or if you're doing parallel tool calling, you're calling multiple APIs or something like this. But code is kind of a special API, because code is extremely expressive. And you can view code as a meta tool. So within this, we have the baker's bake 200 loaves of bread. How many loaves of bread did they have left? Here, this is calling an assignment tool, an assignment tool. This is calling a subtraction tool.
This is calling a assignment tool, et cetera, et cetera. So essentially, I imagine that a lot of people have taken a programming languages course or have some familiarity with programming languages. But basically, you can express. Each piece of code is a tree of these function calls. And the tree can also have loops and other control flow in it. So it's essentially a very rich way of calling tools. And so you can also pull in external libraries. So these are now.
the custom APIs that you pull into your system. So now if you're calling pandas, now you've opened up all of the tools in pandas. And you can build your own utility functions. So you could call special purpose functions that you want as well. Code is kind of special. It's one of the special things that it's different from other tools in the way that it opens up your ability to call tools in new ways.
And so there's actually research work that demonstrates that this is very effective. Right now, we basically take it for granted, but this was not how we called tools three or four years ago when we first started building agents with LLMs. And so the standard then was like, if you got a query, sorry, this is very small, but it basically says, determine the most cost effective country to purchase the smartphone model CodeAct 1. The countries to consider are the US, Japan, Germany, and India.
Basically, what this would require you to do was you would call your lookup rates tool on Germany. You would call your lookup phone price tool. You would call convert and tax, then you would call lookup rates again, then you would call lookup phone price, and you would step over and over and over again in order to finally get the result. But if you write code, you can just write a single program to perform this. And so this makes it a much richer, a much more efficient way of calling tools in addition to being richer. And so this method called CodeAct has a paper about it. And some of the core results from the paper are essentially the success rate goes up and the average number of interaction turns goes down. And this is even for tasks that didn't traditionally require code.
These are for tasks that were viewed as regular tool use tasks, but code is just a good medium for how you would do this. But there's a reason why people don't use code programmatic tool calling for everything. And it largely has to do with the level of power that you want to give to the agent. So code is very high powered. So it's expressive. It gives you loop variables and libraries. This is good. But what could be a downside of this?
Any ideas? Just with respect to loops and variables at first, maybe. Yeah, it could get stuck. So it could write an infinite loop, and then you need to have something in your system to deal with infinite loops. That's a little bit annoying. What about libraries? Any problem with that? Yeah, security problems would be one thing. Also, maybe just versioning and stuff like this. But I'm not sure if people are familiar with this, but one of the major issues with agents in cybersecurity right now is the agents will pull in a library that has been compromised.
The agent gets compromised, and then your whole system gets compromised. So this is something you need to be very careful about. They're also harder to constrain because they have the broad action space. So it's harder to validate and get predictable behavior. They also have higher impact. So they can use more memory, use more disk, use more CPU, and stuff like this. And so because of this, we'll talk about this more in the safety lecture. But once you move to programmatic tool calling, you need to think about sandboxes and how you're going to contain the agent and stuff like that as well.
Any questions about this? Cool. OK, so I want to talk next about the mechanics of providing tools. And right now, every language model is basically pretty good at calling tools. But this was not taken for granted. I think I mentioned this on Tuesday as well. But basically, tool calls are expressed as tokens, like this. And so a text continuation is, the weather is sunny. A tool continuation is a tool call, and then you have getWeather or something like this within the tool call. And very often, you have both in the same output. So if you remember ReAct,
which was talked about before. I can actually draw this on the board. But ReAct, basically you have your text up here. And then you have your tool call. And then you have your tool call down here. Yeah, this is like the end of the tool call. And so now you have the model giving a response to the human while it's making the tool call. And then in here, you have the actual tool call that it's making. And so the ReAct loop is no longer expressed as separate code. It's expressed more as just a single completion.
If you're using one of the reasoning models, that can have long reasoning traces and stuff like that. In addition, you might have the thinking tokens up here, and then a message, and then the tool calls. And so the thinking is very verbose thinking that you don't show to the user. The text is the less verbose message that you show to the user. And then the tool call is the actual output you have there. [Student question: Are all special/service tokens in the vocabulary during pre-training? Do tool call tokens need to be in the model's vocabulary?] pre-train? Yes. So actually, that's a good point. I said yes, but the answer might be no. I'm going to repeat the question first. So the question was, do these tokens have to be in the vocabulary when you do pre-training?
Typically, if you're pre-training on all of the internet, you don't necessarily have to have them in the vocabulary yet. There are cases where you might do that, but it's actually more typical to introduce them in a process of mid-training, which is like between, it's when you're training on moderately large amounts of data on things in the format that you like. And then you might do reinforcement learning after that, but you might not necessarily have them when you pre-train on the whole internet.
Cool. I think this should likely be familiar to people who have implemented things in previous natural language processing or whatever courses. But if you have the chat format, like the OpenAI chat format, you have things like the system message, the user message, the assistant message. And then these are represented as something like JSON like in Python when you call the model. But these get converted into something with like a special token here. The system, like this is a system prompt and then be concise and stuff like this. So like each chat message gets its own format. And this is an example of the Qwen format.
But as I'll show later, the other functions are in different formats. Tools basically add a structure to the request. And so you have a tool definition like this. And this tells you that it's a function. The function name might be getWeather. The parameters are city. And the type is string. And this is required. And this schema gets passed to, for example, the chat completions function that you would use when calling OpenAI or something like this.
So when the prompt contains something like this, you might have this included in the tools section of the prompt. And then the output ends up being something like name get weather with the appropriate arguments, like the city of Pittsburgh. So we had a question about how you pre-train the models. And you might not be pre-training, but at least at mid-training and beyond, you will be training the model to fit a particular tool call format. The interesting thing is every model has a different tool call format. Maybe not every one, but many of the different ones. And so Qwen has IM start, tool call, IM end.
Mistral has tool calls, and then all of the tool calls. DeepSeek has their DSML, DeepSeek Markup Language, function calls. They don't call it tool calls, they call it function calls. And then Evoke, Invoke. And basically, any of these works. There might be minor, minor differences in which one gets better performance or not if you train the same model with the same data. What you need to know is you need to know that these are different. So you can't just assume that some parsing code that works with DeepSeek Quen => Qwen will suddenly work with Quen and vice versa. [Student question: Could that lead to loose generalizability?] Yeah, so when you're fine tuning, you will want to match, like if you're fine tuning from a model that's already been trained, you 100% want to match the tool call format. You don't want to try to get it to do something it wasn't trained on.
And the good news is this is all implemented in Hugging Face with the apply chat template function. And so you need to apply the chat template for the model you're using, but that's all implemented. If it's not implemented properly, you can go in and, like, you ask them, and they'll go in and implement it, or you do it yourself, or something like that. But yeah, it's all standardized now. But I want people to know that this is going on under the hood, because if you don't know this, you can make mistakes, and it will not be happy.
Cool. So once you have done this, the next thing is that you do a tool call parsing. And actually, I'm going to talk about a tool call parsing in the next section, so not immediately here. But let's assume that if the language model outputs this, you have a way of parsing it into the function arguments. You register the tool at initialization, and you have a way to resolve it so that you get a map from the name to the tool that you want to execute. So most agentic frameworks basically will have a way that you can add new tools, each tool gets its own name, and then you have a Python function or something like that, Python TypeScript, whatever language, and then it will be dispatched to that with the appropriate arguments.
And so then at each time you parse, you look up the tool, you validate the action from the arguments, and you execute, and then you return. But at validation time, this can fail if the model gave you the wrong tool call. At execution time, this can also fail if there's like Not something that was explicitly said is bad, is invalid, but implicitly is invalid. So for example, if you have an execute Python tool, it might accept any string as Python. But then when you actually try to run the Python, it might fail. So that would be an execution error. And then if it does, well, whether it fails or not, then you get an observation out of it.
So this is another detail that's pretty helpful, but also really annoying when you're implementing agents if you get it wrong. But basically, each tool call is assigned an ID when you run it, basically by either the language model inference code or the agent code. And then you get a result. And the result has a matching call ID. And this is useful, especially in the case of parallel tool calling, because I'm going to talk about parallel tool calling in a bit. And having the IDs matched lets you know which tool call is associated with which result. But the problem is,
For instance, Anthropic, if you have a tool call with no matched tool result, it will die on you. So it will tell you this is an invalid history, and I'm not going to accept this history and generate any more outputs for you. In my practical experience, there's times when, for example, the assistant makes a tool call, then suddenly your program dies or something like that. It fails to write out the tool result, and then you resume it. It doesn't have the associated result, and Anthropic fails to work for you. So this is a little bit of a detail that you need to know.
OK, so any questions there? Yeah. And great question, are the tool calls asynchronous? I'm going to talk about that in the next section. Cool. OK, so now here's a very technically algorithmically difficult problem that may be very interesting to you or may be not very interesting to you. But if it's not interesting to you, be glad that other people are solving it for you.
So language models are definitely not guaranteed to generate well-formed tool calls. And this is an example. You can see that this is JSON that's missing the last closing bracket. So this is poorly formed JSON. You put this into your JSON parsing program, and it will throw an exception, and you won't be able to get the data out of it. So I've seen a million people go in and try to figure out if the brackets are missing and add one post hoc, but you don't want to be doing that. That's not fun.
So there's a bunch of constraints that you could be assigning to each of the tool calls. So the first one is syntax. So it has to be valid JSON. So this is kind of like table stakes, right? So it needs to parse. The second thing is shape. So make sure that if you have expected or required fields, you have the required fields. The third thing is types. So if you have city, the city must be a string. Oh, sorry. Yeah. So if you have a city, city is a string.
If you have values, the units are in the appropriate units. So this can all be expressed through something called JSON schema, or basically any schema library. So it's like, this is a city. The type is string. And the units can be Celsius or Fahrenheit. And city is required. The good thing about this is this has machine-readable validation rules, so you can check if a call follows the schema.
But one way that you can express whether the schema is valid or invalid is by defining a grammar over the schema. And how many people did context-free grammars or context or those sorts of things, regular grammars? How many people know regular expressions? OK. A lot of people. OK. So a regular expression is a regular grammar.
Does anybody know how you can parse a regular expression? Finite state automaton. Yeah, that's exactly correct. So a finite state automaton, for those who are less familiar with this, is something that looks a little bit like this.
So you have a state. And every time you take a token as input, you move to a different state. So if you get A, you would move to this state. If you get B or C, you move to this state. This works on something called regular grammars. So regular expressions, like the slashes and dot stars and stuff like this that you might use to search for search text, are regular expressions, and they can be expressed through regular grammars.
JSON schema cannot be expressed through a regular grammar. It can be expressed through something called a context-free grammar. And the context-free grammar can basically make a tree that looks a little bit like this. So every JSON expression can have brackets on the side. It can have commas between its members. This can be a string. And this can be a unit. If the JSON schema is in the correct format, you should be able to create a tree that looks like this. If the JSON schema is in the wrong format, you might be able to create most of a tree. But then when you get to a unit, a unit cannot be k. So basically, this would fail.
even though a unit can be K, but we define this to not accept Kelvin as our unit of temperature. Basically, if you can parse the tool call with a context-free grammar defined from your JSON schema, you can do this. And I'm not going to go into a lot of details, but a finite state automaton cannot parse a context-free grammar. But there's something called a pushdown automaton that can allow you to do this. So basically, it adds a stack to the regular grammar. And you push things onto the stack, pop things off of the stack, and eventually can decide whether it's parsable or not.
And so what you do is you start out at the very beginning with a state in this pushdown automaton, and you step through and validate your output as you generate it from left to right. And I'm giving some examples from Xgrammar. And Xgrammar is a thing that's actually pretty widely used in all of the LLM inference libraries. It's actually developed by people in the machine learning department here.
So what this means is every time you get an LLM, predict the logits of the next token. You take a look at your grammar. And if the grammar says this is a valid next token, you get a score of 1. And if you have an invalid next token, you get a score of 0. And you set all of the invalid next tokens to negative infinity for the output logits. And then you renormalize so that you get probability only on the valid tokens.
And so what this lets you do is this ensures that you generate valid output. And so this is another figure from the X-Grammar paper. And they do these tricky things where basically there are some vocabulary that are always valid or always invalid. And you pre-compute these. And then there are those that are valid only in some contexts, only in some stack contexts. And then you calculate them on the fly.
It's very interesting algorithmically, if you like algorithmic stuff. If you don't like it, be glad that the people in the machine learning department are doing it for you, basically. So because of this, almost always, if you have this enabled, you will, in set your appropriate JSON schema, you will get well-formed tool calls. So that's great. There is one case where you actually can't get well-formed tool calls despite this.
Maybe I can give a quick quiz. This is very, very difficult, so I don't know if anybody can get it. But anybody have an idea where even this would not be enough? You need to be familiar with LLMs, I guess. Yeah? When you are learning to read between, are you talking to an off-topic object I think that's close enough to what I wanted to say. Basically, when you run out of tokens. Models can only generate so many tokens, and you might run into a place where you're still on a valid path through the automaton, but you're not at the final state, so you generate part of a JSON output. Very good. I've actually encountered this. There's theoretically a way you could do that, which is you count the number of output tokens that you still have left, and you cut them off.
If somebody wants to implement that in XGrammar as an extra credit assignment, I'd be happy to have that. Now, let's go to REST API calling. There's different ways to express tools. And I'd say probably most people are familiar with REST APIs. These are the ways you interact with web services.
Actually, sorry, there's one other way to call APIs. And one way to call APIs is through programmatic tool calling, where basically you just give the model a Python program, and you say, use the stuff in this Python program to call tools. And so if you do something like that, you might have weatherlib.py, and you have the getWeather function in here with its city and its units. And then you just say, call this function whenever you need to. And you give it the ability to execute Python, and it will be able to call that tool. So in programmatic tool calling, this works. But this is not callable remotely, and it's also not callable from the standard method of tool calling that you use when you implement this, not through non-programmatic tool calling.
So there's a way to turn this into REST APIs. Has anybody used FastAPI before? It's a pretty common, OK, yeah, a lot of people. It's a pretty common library. And basically what this allows you to do is this allows you to create a web backend extremely simply by just setting up a fast API, giving an API key so you can get people to validate against the API, and then writing a Python decorator where weather becomes this.
So this is good for a couple reasons. The first reason is maybe if you don't want people looking at the weather, then you can prevent them from looking at the weather. But probably more importantly, if you don't want people getting your personal data or something like that, you can require an API key. And this will allow you to block anybody who doesn't have an appropriate API key, for instance. The other reason why is this gives you a JSON schema for free.
And when I say for free, of course, this is something that's implemented in FastAPI. But your entire API can be expressed as a JSON schema. And that JSON schema can then be provided as is to a language model. And that language model can then use it to specify which calls are allowed. And so then you could go in and process this. Whenever the agent made a tool call, you could then send it to your API server, and it could use that on the API server. Another option for calling these is you can just use the curl command. And the curl command basically allows you to call these APIs directly like this as well. And so if you have a coding agent that has access to Bash, it has the ability to call like this.
This is maybe reasonably straightforward if you're familiar with REST and stuff like this. Any questions or comments? If you haven't played around with FastAPI, it's nice. It's very easy to use. But the reason why I wanted to cover this first is this is kind of like the traditional way of programming before agents. There is something called MCP, the Model Context Protocol. And this was introduced by Anthropic maybe two-ish years ago or a year and a half ago. And the idea was this was designed to be a standard way to give your model additional context.
When I first saw this, I actually was like, why do we need to do this? I don't understand why we need to do this. Let's just let our agents call APIs. But there is one important point about one or two important points that make MCP different from just calling a normal API. And I'll explain them here. But first, I'll explain why I didn't Like, I thought this was not actually necessary. And basically, the reason why is every time you set up an MCP, what you do is you have your AI application, and then you set up a couple MCP clients. So maybe this is your weather client. This is your weather MCP and your GitHub MCP or something like that. So you can ask what the weather is and push it to your GitHub.
The reason why I didn't think this was necessary originally was because why do you need to run a program to call an API? We already have good ways to call APIs. We just send it to the REST API server. OK, yeah, so that's a little bit of an aside. I'll get back to that in a moment. But for a little bit of an aside, there's a really nice library called FastMCP. And FastMCP is basically designed to be FastAPI for MCPs. So if you want to create an MCP server for whatever you want, you can just basically swap out FastMCP and swap out FastAPI and swap in FastMCP. And you can create an MCP server very easily.
You can also take in a spec that was generated by like a REST API and just turn it into an MCP as well. But here's the main reason why you might want to use MCP as opposed to an API. And the reason why is because it gives you an extra layer of security. And so MCPs have an MCP API key. And this MCP API key is something that you need to provide to the process that's running your agent.
Separately from this, you have the upstream API key. So let's say you are getting your agent to connect to your GitHub. The upstream API key would be your GitHub token, and you would provide that to the MCP server, but you would not provide it to the agent. And then what you provide to the agent is the MCP API key, which allows you to authenticate into the MCP server. And so the reason why this is important is what happens if you give your agent your GitHub token and your agent suddenly decides that it's a good idea to push that to your public GitHub repository?
Not good, right? Your account gets compromised, and you get malware installed onto all your repositories or something like this. So you want to give the agent something that is relatively harmless, so that even if it leaks it for whatever reason, that leak is relatively harmless. And then the actual credentials get preserved. And so that was basically the reasoning behind it. So just to go into a summary, they all give you the ability to add a set of APIs into your agent. You could do that through a regular JSON schema or through an MCP. But a lot of other things are different. So for discovery, you might need to fetch an OpenAPI document. For MCP, there is a special API that allows you to list all of the tools.
For invocation, you call an HTTP thing here. For MCP, there's an MCP transport. And this can be through various ways. You connect through standard IO, or you connect through a socket, or something like that. I already talked about authentication. And MCP actually has a few other conveniences. You can add resources, prompts, and extensions, but I'm not going to go into a lot of detail about that.
Cool. Actually, there's two other things I'd like to mention. I guess I forgot to add them to my slide. So the first thing is MCP registry. in official MCP registry. So if you want to find MCPs that allow you to do something like this, you can go to this registry and find any of a very large number of MCPs. There's also like a million and a half MCP server websites that you can go to that are ranked based on popularity and stuff like this.
Another thing is I've been talking a lot about the Pittsburgh weather. The funny thing is today, I prepared all of these slides earlier, of course, but today we actually got Pittsburgh weather that was simultaneously rainy and sunny at the same time. And so I managed to break my own API because it cannot return multiple states at the same time. But yeah, it was quite the coincidence. You could see the torrential downpour and the bright side at the same time. OK, back to serious stuff.
Cool. So another big development pretty recently is parallel tool calling. In parallel tool calling, the basic problem we want to solve is the same problem that I talked about when I talked about the CodeAct and the programmatic tool calling, which is if you call tools one at a time, it could become very long for even a simple operation, because you might do a weather, and then do a calendar, and then do a flight.
But if tool calls are independent, you can actually call them at the same time. And this is done pretty simply. You just add multiple tool call blocks to a single agent response. There's a few nuances about this. The first one is you can only parallelize tool calls if they don't have dependencies on each other. So if you were looking up the weather for Pittsburgh and reading today's calendar and looking up flight status, that might be good.
But if you need to find a customer ID, use it to fetch orders, and refund the selected order, obviously you can't do that. So there's limits to the parallel tool calling you could do. And if your calendar or your flight was dependent on the weather, then obviously that'd be a problem too. So there was a question about this. Do you execute the tool calls in parallel? And the answer is typically yes. You can just use typical Python syntax to have a whole bunch of tool calls and then execute them and then use something like asyncio.gather to return the results of them when they run in the same time. So this is pretty common.
Another thing I'd like to point out is this is a major difference between really efficient language models and not efficient language models. And there's this weird paradox in agentic language models nowadays that more expensive models can actually be cheaper if you measure them on a task-by-task basis. And there's two major reasons for this.
The first major reason is more expensive models can be smarter, so they pick an appropriate solution more quickly. But another thing is more recent models do parallel tool calling a lot better. So they make a whole bunch of calls at the same time. So if you're using a coding agent or something like this, you use the better models. And they are doing a bunch of view images at the same time, or they're writing a bunch of different files at the same time, or something like that. That's because they're doing parallel tool calling well.
Does anyone have an idea of why they got so much better at this? This was not a huge thing. Even though parallel tool calling mechanisms have existed for a long time, this was not a huge thing until maybe six months ago or something. Yeah. Yes, exactly. So reinforcement learning kind of forced them to do that. And so the models were very heavily incentivized to finish tasks quickly. And parallel tool calling is one of the good ways to do that. So if you do reinforcement learning with a very heavy brevity penalty on how long the task execution is, you can get a lot more parallel tool calling.
Cool. So the final thing I'd like to talk about in this lecture today is evaluating tool use. And so I'm not going to talk about evaluating end-to-end agentic tasks immediately yet, but rather talk about evaluating just the tool usability of language models, because it's kind of like a prerequisite for language models to be good at agentic tasks. And there's a bunch of data sets for this. The most famous one is the Berkeley Function Calling Leaderboard.
And this has four versions. It started out with version one, which was basically single-turn tool calling. It's then evolved through the four versions, and now it covers multi-turn tool calling. It also does have agentic tool calling, but this is more not for end-to-end coding agents or something like that. It's tool calling agents. And they also have tool calling robustness. So they measure hallucination of arguments. They also measure format sensitivity. So when I talked about all the different formats, they have parameters about that as well.
When you go in to evaluate the whole stack, there's a number of things that you want to evaluate. So the first thing is, did the language model choose the appropriate tool for the task? So it might have picked an inappropriate tool that can't allow you to do that, the task at all. The other thing is arguments, which is whether you add the right values. If you're talking about agentic tool calling, you can talk about whether it did it in the right order. And then you can measure end-to-end task accuracy if you're doing agentic tool calling. And all of these are kind of metrics in this leaderboard.
Then separately from that, there's also efficiency. So how quickly can you solve the task? How many tokens did it take? How much did it cost? Reliability. So if a tool call times out, does it retry appropriately? If a tool is out of order, can it choose a different one? And also safety, so if you have adversarial outputs where you ask it to use a tool in an inappropriate way, can you do this? And all of these are over the four iterations of this benchmark, it covers all of these dimensions.
Yeah, I kind of covered that already, actually. So one other really interesting benchmark for tool calling is this OpenRouter tool call error rate benchmark. I don't know if anybody uses OpenRouter or has seen this before. Maybe no. OK. This is for the same model. All of these are for, I think this is GLM 5.3. So it's like the same model. And it's measuring the tool call error rate.
Simply, the only thing they varied is who serves the model to you. And this is pretty important, obviously, because you think, oh, I'm going to use this model. Maybe it's going to be the same regardless of who provides it to me. But actually, the difference is between 15% and 0.01% to 0.05%. Any idea why this might be?
Yeah, great points. So I'll repeat it. So number one is the decoding or the inference algorithm could be different. And one very big difference is whether they're using quantization of the model or not, or what level of quantization of the model they're using. So my personal experience is FP8 models, FP8 is kind of like the default level of quantization for the model, get a lot fewer mistaken tool calls than FP4 models, because FP4 is like compressing it very heavily. It's saving money for the inference provider, but it's usually a worse model. There aren't very many FP4 quantizations that can allow you to get the same performance. Another one was speculative decoding. I don't think we're going to talk about speculative decoding a huge amount in this class. Maybe I'll talk about it during long context. Or actually, there are a few algorithms specifically for agents which are interesting. So maybe we can talk about it.
But basically what it is, is you have another cheaper model that predicts what the more expensive model is going to do. And then you use that to speed up your inference. Lossless speculative decoding probably shouldn't make a difference, because it's not changing the underlying results of the model. But if they're doing some sort of lossy speculative decoding, then that might cause it to be different. But then calling it the same model is also probably a bad idea. There's one other thing that I mentioned.

Ask anything
We've read this whole study set, so questions about it get the best answers.