AI engineering glossary · Prompt context engineering
What is function calling?
Function calling (also called Tool calling, Tool use): A feature where the model, instead of plain text, returns a request to run a named function with arguments in JSON that your code then executes. You describe available functions in advance. It connects language models to databases, APIs and calculators.
Function calling: an example
Given get_weather(city), the model answers a Bengaluru weather question with {"name":"get_weather","arguments":{"city":"Bengaluru"}}.
Function calling in Python
A short example you can run (Python 3.11). The output below is what it really prints.
import json
tool_schema = {"name": "convert_currency", "parameters": {"type": "object",
"properties": {"amount": {"type": "number"}, "to": {"type": "string"}}, "required": ["amount", "to"]}}
def convert_currency(amount, to):
rates = {"USD": 0.012, "EUR": 0.011} # illustrative rates
return round(amount * rates[to], 2)
# What a model returns when it decides to call the tool:
model_reply = '{"name": "convert_currency", "arguments": {"amount": 29999, "to": "USD"}}'
call = json.loads(model_reply)
print(tool_schema["name"], "->", convert_currency(**call["arguments"]))Output
convert_currency -> 359.99
Related terms
More Prompt context engineering terms
- GBNF grammar
- Generated knowledge prompting
- Grammar-constrained decoding
- Grounding
- In-context learning
- Instruction following
- Instruction hierarchy
- Instructor library
Where you learn it
Function calling is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.