Expose tools (APIs, search queries, code execution) to an LLM by giving it a JSON schema; the model decides when and how to call.
An executive assistant who can call the travel agent if you tell them ‘Book a flight’.
tools = [{'type':'function','function':{
'name':'get_weather','description':'...',
'parameters':{'type':'object','properties':{...}}}}}]
model -> tool_call
runtime -> execute + return result
Function calling lets the LLM defer part of an answer to a deterministic tool. You supply a JSON schema for each tool describing inputs; the model emits a structured invocation you parse and execute. This converts LLMs into orchestrators. Always inspect tool_choice; enforce max calls to avoid runaway loops.
tools = [{'type':'function','function':{
'name':'get_weather','description':'Get weather for a city',
'parameters':{'type':'object',
'properties':{'city':{'type':'string'}},
'required':['city']}}}]
resp = openai.chat.completions.create(
model='gpt-4o-mini', tools=tools,
messages=[{'role':'user','content':'What is the weather in London?'}])
print(resp.choices[0].message.tool_calls[0].function.name)
Tool description is the prompt: clear descriptions get better tool selection.
resp = openai.chat.completions.create(
model='gpt-4o-mini', tools=tools,
tool_choice={'type':'function','function':{'name':'get_weather'}},
messages=[{'role':'user','content':'Weather for Berlin.'}])
tool_choice forces a specific tool — useful for upgrades that require a single call.
resp = openai.chat.completions.create(
model='gpt-4o-mini', tools=tools,
messages=[{'role':'user','content':'Compare weather in London and Berlin today.'}])
for tc in resp.choices[0].message.tool_calls:
print(' -', tc.function.name, tc.function.arguments)
Setting tools allows parallel calls in one round-trip.
Defining too many tools — model picks poorly or context windows blow up. Aim for 10 or fewer tools with crisp single-purpose descriptions.
Tools equal JSON-schema-defined verbs the model can invoke. Keep descriptions specific; cap the count. Wrap side-effecting tools with confirmations.