How to calculate the tokens when using function call

Based on the given example, I was able to find a format that gives a much more accurate token length than the examples above, without magic numbers:

namespace functions {

// Get the current weather in a given location
type get_current_weather = (_: {
    location: string, // The city and state, e.g. San Francisco, CA
    unit?: "celsius" | "fahrenheit",
}) => any;

} // namespace functions

In testing, this gives token length to within 5 tokens of the actual number calculated by OpenAI.

My full implementation:

1 Like