Skip to Content

Function Calling and Tool Use

When to use

Expose tools (APIs, search queries, code execution) to an LLM by giving it a JSON schema; the model decides when and how to call.

Analogy

An executive assistant who can call the travel agent if you tell them ‘Book a flight’.

Data-flow diagram

   tools = [{'type':'function','function':{
     'name':'get_weather','description':'...',
     'parameters':{'type':'object','properties':{...}}}}}]
   model   ->  tool_call
   runtime ->  execute + return result

Deep explanation

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.

Examples

Example 1

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.

Example 2

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.

Example 3

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.

Common mistake

Defining too many tools — model picks poorly or context windows blow up. Aim for 10 or fewer tools with crisp single-purpose descriptions.

Key takeaway

Tools equal JSON-schema-defined verbs the model can invoke. Keep descriptions specific; cap the count. Wrap side-effecting tools with confirmations.

Production Failure Playbook

Failure scenario 1: tool-hallucination

Failure scenario 2: infinite-tool-loop