Giving Models Tools

Let the model call functions in your code to fetch data or take actions.

On their own, LLMs only produce text. Tool use (also called function calling) lets a model ask your program to run a function — check the weather, query a database, send an email — and then use the result.

Why it is powerful

  • The model can access live data it does not know.
  • It can take actions in the real world.
  • It can do reliable math or lookups by delegating to real code.

Crucially, the model does not run anything itself. It requests a call; your code decides whether and how to run it.

Example

Example · json
{
  "user": "What's the weather in Tokyo right now?",
  "model_wants_to_call": {
    "name": "get_weather",
    "arguments": {"city": "Tokyo"}
  }
}

When to use it

  • A travel assistant lets the model call a live flight-search function so prices and availability are always current, not hallucinated.
  • A developer tool lets the model call a 'run_tests' function so it can verify whether the code it wrote actually passes the test suite.
  • A CRM chatbot gives the model a 'lookup_customer' function so it can retrieve the real account balance before answering billing questions.

More examples

Define and call a simple tool

Defines one tool and passes it to the model — the model responds with a structured function call instead of a text answer.

Example · python
from openai import OpenAI
import json

client = OpenAI()

# The actual function your code runs
def get_weather(city: str) -> str:
    return f'The weather in {city} is 22°C and sunny.'

# Tell the model the tool exists
tools = [{
    'type': 'function',
    'function': {
        'name': 'get_weather',
        'description': 'Get the current weather for a city.',
        'parameters': {
            'type': 'object',
            'properties': {'city': {'type': 'string'}},
            'required': ['city']
        }
    }
}]

r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': 'What is the weather in Tokyo?'}],
    tools=tools
)
print(r.choices[0].message.tool_calls[0].function)

Execute the tool and return result

Completes the full tool loop: call the function, append its result, then get the model's final text response.

Example · python
from openai import OpenAI
import json

client = OpenAI()

def get_weather(city):
    return f'{city}: 18°C, overcast.'

tools = [{'type':'function','function':{'name':'get_weather','description':'Weather for a city.','parameters':{'type':'object','properties':{'city':{'type':'string'}},'required':['city']}}}]

messages = [{'role':'user','content':'Weather in Paris?'}]
r = client.chat.completions.create(model='gpt-4o-mini', messages=messages, tools=tools)

tool_call = r.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
result = get_weather(**args)

messages.append(r.choices[0].message)
messages.append({'role':'tool','tool_call_id':tool_call.id,'content':result})

final = client.chat.completions.create(model='gpt-4o-mini', messages=messages)
print(final.choices[0].message.content)

Multiple tools available to model

Registers two tools and lets the model pick the appropriate one based on the user's request.

Example · python
tools = [
    {'type':'function','function':{
        'name':'get_weather',
        'description':'Current weather for a city.',
        'parameters':{'type':'object','properties':{'city':{'type':'string'}},'required':['city']}
    }},
    {'type':'function','function':{
        'name':'convert_currency',
        'description':'Convert an amount from one currency to another.',
        'parameters':{'type':'object','properties':{
            'amount':{'type':'number'},
            'from_currency':{'type':'string'},
            'to_currency':{'type':'string'}
        },'required':['amount','from_currency','to_currency']}
    }}
]
from openai import OpenAI
client = OpenAI()
r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role':'user','content':'Convert 100 USD to EUR.'}],
    tools=tools
)
print(r.choices[0].message.tool_calls[0].function.name)

Discussion

  • Be the first to comment on this lesson.