0Pricing
Learn AI with Python · Lesson

Function Calling and Tool Use with LLMs

Defining tools as JSON schema, handling tool_calls in response, executing and returning results.

Function Calling and Tool Use with LLMs is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is Function Calling?

Function calling (also called tool use) lets an LLM request that your code run a function. The model does not execute anything itself, it returns a structured request saying "call this function with these arguments". Your program runs it and feeds the result back.

This is how LLMs fetch live data, do math, or trigger real actions.

Describing a Tool

You give the model a list of tools. Each tool has a name, a description, and parameters defined as a JSON Schema. The description is critical, the model uses it to decide when the tool applies.

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name"}
            },
            "required": ["city"]
        }
    }
}]

JSON Schema for Parameters

The parameters field is standard JSON Schema. You declare an object with properties, each having a type and description, plus a required array. This tells the model exactly what arguments to produce.

"parameters": {
    "type": "object",
    "properties": {
        "city": {"type": "string"},
        "units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
    },
    "required": ["city"]
}

Passing Tools to the Model

Send the tools list to chat.completions.create(). The model now may respond with text OR with a tool call, depending on the user request.

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Weather in Tokyo?"}],
    tools=tools
)

Detecting a Tool Call

When the model wants a tool, the reply contains message.tool_calls instead of normal text. Check whether this list exists before assuming you have a plain answer.

msg = response.choices[0].message

if msg.tool_calls:
    print("Model wants to call a tool")
else:
    print(msg.content)

Parsing the Arguments

Each tool call has a function.name and function.arguments. The arguments arrive as a JSON string, so parse them with json.loads() before use.

import json

call = msg.tool_calls[0]
name = call.function.name
args = json.loads(call.function.arguments)
print(name, args)  # get_weather {"city": "Tokyo"}

Executing the Function

Now run YOUR actual Python function with the parsed arguments. The LLM only decided what to call; your code does the real work.

def get_weather(city):
    return {"city": city, "temp_c": 18, "condition": "Cloudy"}

result = get_weather(**args)

Returning the Result to the Model

Send the result back so the model can write a natural-language answer. Append the original assistant message, then a tool role message carrying the result and the matching tool_call_id.

messages.append(msg)  # the assistant tool-call message
messages.append({
    "role": "tool",
    "tool_call_id": call.id,
    "content": json.dumps(result)
})

Final Answer Generation

Call the model again with the updated messages. Now that it has the tool result, it produces a friendly final reply for the user.

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

The Full Tool-Use Loop

The complete cycle: (1) send tools, (2) model returns a tool call, (3) parse args, (4) run the function, (5) return the result as a tool message, (6) model writes the answer. A model may request several tool calls before finishing, so loop until tool_calls is empty.

Controlling Tool Choice

Use the tool_choice parameter to steer behavior. "auto" (default) lets the model decide; "none" forbids tools; passing a specific tool name forces that exact function. This is useful when you must guarantee structured output.

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice={"type": "function", "function": {"name": "get_weather"}}
)

Quick Check

Test your tool-use understanding.

Recap: Function Calling

You defined tools with a name, description, and JSON-Schema parameters, then passed them to the model. You detected tool_calls, parsed function.arguments with json.loads(), executed your real function, and returned the result as a tool role message with the matching tool_call_id.

A final model call turns the raw result into a natural answer, completing the tool-use loop.

Frequently asked questions

Is the “Function Calling and Tool Use with LLMs” lesson free?

Yes — the full text of “Function Calling and Tool Use with LLMs” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Function Calling and Tool Use with LLMs”?

Defining tools as JSON schema, handling tool_calls in response, executing and returning results. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Function Calling and Tool Use with LLMs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. OpenAI API: chat.completions and Streaming
  2. Anthropic Claude API in Python
  3. Function Calling and Tool Use with LLMs
  4. Prompt Engineering for Production LLM Apps
← Back to Learn AI with Python