# What is function calling in the OpenAI API, and how does it work?

Date: 2023-06-14 · Author: Robert Scales · https://guidetoartificialintelligence.com/en/learn/what-is-openai-function-calling/

**Answer:** Function calling, added to OpenAI's API on June 13, 2023, lets developers describe the tools their app can use, and lets GPT-4 or GPT-3.5 Turbo answer with a neatly structured request to use one of them instead of plain prose. The app runs the tool itself, sends the result back, and the model turns it into a clear reply.

Yesterday, June 13, 2023, OpenAI announced a set of updates for developers who build on its models. The headline feature is **function calling**: a way for GPT-4 and GPT-3.5 Turbo to say, in a precise and predictable format, "please run this tool with these details." The same announcement brings new model versions, a version of GPT-3.5 Turbo that reads four times more text, and lower prices.

If you have used ChatGPT plugins, you have seen the idea from the user's side: the chatbot decides it needs outside help, uses a tool, and reports back. Function calling hands that ability to any developer, inside their own app.

## What OpenAI announced

The blog post, by Atty Eleti, Jeff Harris and Logan Kilpatrick of OpenAI, lists several changes to the API, the service that lets programs send text to OpenAI's models and get answers back:

| Change | Detail |
|---|---|
| Function calling | Built into the new June versions of both models: gpt-4-0613, and the matching gpt-3.5-turbo-0613 |
| Longer context | gpt-3.5-turbo-16k reads about 16,000 tokens, roughly 20 pages, at twice the standard price |
| Cheaper chat model | gpt-3.5-turbo input tokens cost 25% less, now $0.0015 per 1,000 (output tokens cost $0.002 per 1,000) |
| Cheaper embeddings | text-embedding-ada-002 is 75% cheaper, at $0.0001 per 1,000 tokens |
| Upgrades | Apps using the standard model names move to the new versions on June 27; the March versions stay available until September 13 |

A **token** is a chunk of text, often part of a word. **Embeddings** are lists of numbers that capture what a passage is about, used for search and recommendations. OpenAI also says it will invite many more people from the GPT-4 waitlist over the coming weeks, aiming to remove the waitlist entirely.

## How function calling works

A **function** in programming is a named action with inputs. For example, `check_library_hours(branch, date)` might look up when a library branch opens. Before function calling, a developer who wanted a model to trigger such an action had to ask nicely in the prompt and then try to parse whatever text came back. Sometimes it worked; sometimes the model added chatter or changed the format.

Now the developer sends the model a list of available functions, each with a name, a short description, and the inputs it accepts, written in **JSON Schema**, a standard way to describe data. OpenAI says the new models were fine-tuned, meaning given extra training, to recognize when a function would help and to reply with **JSON**, a simple text format for structured data, that matches the description.

One point is easy to miss: the model never runs anything itself. It only proposes the call. Your code decides whether to run it, runs it, and passes the result back. TechCrunch's summary says the models "create code" for the functions; in OpenAI's own description, the model writes the arguments for a call and the developer's program does the rest.

## A worked example, step by step

Imagine a small app that answers questions about a city library. The developer describes one function, `check_library_hours`, which needs a branch name and a date.

1. **The user asks:** "Is the Eastside branch open on Sunday?"
2. **The app sends** the question to gpt-3.5-turbo-0613, along with the description of `check_library_hours`.
3. **The model replies** not with prose but with a structured request:

```json
{"name": "check_library_hours", "arguments": "{\"branch\": \"Eastside\", \"date\": \"Sunday\"}"}
```

4. **The app runs** its own function, which checks the library's schedule and returns, say, `{"open": "12:00", "close": "17:00"}`.
5. **The app sends** that result back to the model as a new message.
6. **The model answers** the user in plain words: "Yes, the Eastside branch is open on Sunday from noon to 5 p.m."

OpenAI's announcement walks through the same three-part loop with a weather question about Boston. The pattern is always the same: the model chooses, your code acts, and the model explains.

## Why it matters

The change makes AI apps more dependable. Structured output means fewer broken replies and less guesswork in code. OpenAI lists uses such as chatbots that consult outside tools, turning a request like "who are my top customers this month?" into a database query, and pulling names, dates and places out of a long text into a tidy list.

It also brings risks into the open. OpenAI warns that text returned by a tool can contain hidden instructions that push the model to do something unintended. Its advice: use only trusted tools, and ask the user to confirm before any action with real-world effects, such as emailing someone, publishing a post or making a purchase.

> **Try it:** Pick a task you do often, like booking a meeting room. Write down the function you would give a model: its name, a one-sentence description and each input it needs. Then write three different ways a person might ask for it, and check whether your inputs cover all three.

## How can a beginner start building with function calling?

Start small, with one function that only reads information, like the library hours above. Read-only functions are safe to experiment with because nothing changes in the world if the model makes a mistake. OpenAI's developer documentation describes the two new request fields, `functions` and `function_call`, and the second one lets you force the model to use a particular function when you need it to.

Write clear descriptions. The model chooses a function based on its name and description, so "Look up opening hours for one library branch on one date" works better than "hours." Mark which inputs are required, and give examples of acceptable values.

Then check everything the model sends you before acting on it. Validate the inputs, handle missing or odd values, and add a confirmation step for anything that sends, posts, pays or deletes. Treat the model as a helpful assistant filling in a form, and keep your program as the one that decides whether the form gets submitted.

## Sources

- Function calling and other API updates: https://openai.com/blog/function-calling-and-other-api-updates
- OpenAI intros new generative text features while reducing pricing: https://techcrunch.com/2023/06/13/openai-intros-new-generative-text-features-while-reducing-pricing/
