As you know, function calling, or tool use, gives LLMs a way to interact with the outside world. Pretty cool, right? Once I learned how it actually worked, I was blown away. Whoever came up with it was seriously smart.

It's hard to believe function calling can be this simple.

  1. First, write a general explanation of the tools in the system prompt. This tells the LLM that it can call functions by writing a <function_calls> block, and it also shows what that block should look like.
  2. Then, list the available tools in JSON schema format so the LLM knows what it can use.
  3. Whenever the LLM writes a <function_calls> block during a conversation with a user, extract the parameters from that block.
  4. Call the actual function with these parameters and get the result.
  5. Format the result as a <function_results> block and send it back to the LLM. That's it. The LLM have called the tool and got the result. Amazing.