Beginner Course
Function Calling Beginner
49 lessons across 7 chapters. Every lesson is standalone — start anywhere.
49 lessons 7 chapters
1 Function Calling vs Prompt Extraction 7 lessons
1
Why function calling beats JSON in prompts Understand why the LLM tools API guarantees structured output where prompt-based JSON extraction fails.
2 Schema-validated extraction guarantee Define a strict JSON schema for the tool's expected parameters and validate the model's response against it before using extracted data downstream.
3 Parallel calls: native support OpenAI's tools format natively returns multiple tool calls in a single API response, allowing you to execute them in parallel without sequential loops.
4 Streaming with tool calls Handle OpenAI tool calls while streaming token-by-token responses to your user without losing tool execution logic.
5 When JSON mode is sufficient Decide whether to use JSON mode or tool-calling based on whether the model needs to invoke external functions or just return structured data.
6 Function calling vs structured outputs Decide whether to use function calling (for agentic workflows) or structured outputs (for guaranteed JSON schema compliance) based on your use case.
7 When to use each approach Decide whether your LLM interaction needs function-calling or if a simpler pattern (direct output parsing or chaining) will work.
2 Defining Tool Schemas 7 lessons
1
Function name: action verb convention Name your function-calling functions with action verbs to help the LLM correctly decide when and how to invoke them.
2 Description: model reads this for tool selection The description field is what the model actually reads to decide whether to call a tool: it must be precise, actionable, and free of ambiguity.
3 parameters: JSON schema object Define the input shape and validation rules for your tool by writing a JSON Schema parameters object.
4 Required vs optional parameters Decide which function parameters the LLM must always provide versus which ones it can skip, then encode that choice in your schema.
5 type: string, number, boolean, array, object Define the JSON Schema types for each parameter your function expects, which tells the model what data structure to pass.
6 Enum for Constrained Values Use enum in tool parameter schemas to restrict function arguments to a fixed set of valid options.
7 Description per parameter: it matters Define tool parameters with precision: unclear schemas cause the model to ignore your functions or pass wrong arguments.
3 Handling Tool Call Responses 7 lessons
1
Response with tool_use vs text content Determine whether the model chose to call a tool or respond with plain text, and route the response appropriately.
2 Extracting tool name and arguments Parse the LLM's tool_calls response to extract the function name and its JSON arguments for execution.
3 Executing the function in Python Parse the tool call from the LLM response and execute the actual Python function with the extracted arguments.
4 Error handling in the function Decide how to handle failures when the LLM calls your function: validate inputs, catch exceptions, and return structured error responses so the model can retry or adjust.
5 Formatting the result Extract and structure the tool call result so it can be sent back to the model for further reasoning.
6 Returning result to model Send the function execution result back to the model so it can decide next steps based on what actually happened.
7 Model's follow-up response Parse the model's tool_calls response and execute the requested function before sending results back to the model.
4 Building a Tool Execution Loop 7 lessons
1
Send request with tools defined Construct and send an OpenAI API request with tool definitions so the model can decide which function to call.
2 Check for tool_calls in response Inspect the LLM response to see if the model actually invoked a tool or just answered naturally.
3 Execute each tool call Parse the LLM's tool_calls response and invoke your actual functions with the extracted arguments.
4 Collect results Extract and parse the tool call results from the LLM response before feeding them back for the next iteration.
5 Send results back in conversation Return the tool execution result to the model so it can incorporate findings into its next response.
6 Repeat until no more tool calls Loop on the assistant's response until it stops requesting tool calls and returns final text instead.
7 Extract final answer from last response After the model stops calling tools, parse the final text response to get your answer.
5 Tool Selection and Description 7 lessons
1
When models choose wrong tools Diagnose and handle cases where the model calls a tool that doesn't match the user's intent or doesn't exist in your tool registry.
2 Description engineering for accurate selection Write tool descriptions that guide the model to pick the right function for the user's intent.
3 Tool naming for clarity Choose tool names that the LLM can understand and use consistently without ambiguity or confusion.
4 Number of tools: cognitive load consideration Decide how many tools to expose to the model based on the complexity the model can reliably handle.
5 Tool categories and grouping Decide how to organize and group your tools by semantic category before submitting them to the API.
6 Disabling tools mid-conversation Control when the model can invoke tools by switching between tool-enabled and tool-disabled states during a multi-turn conversation.
7 Forcing specific tool use Use tool_choice='required' or tool_choice={'type': 'function', 'function': {'name': 'tool_name'}} to guarantee the model calls a specific tool instead of choosing one or skipping tools entirely.
6 Error Handling in Tool Calling 7 lessons
1
Tool execution exception handling Catch and recover from tool execution failures so the model can retry or adapt instead of crashing.
2 Returning error message to model When a tool call fails, send the error back to the model in the correct message format so it can retry or adjust its approach.
3 Model recovery from tool error When a tool call fails, inject the error back into the conversation so the model can retry or choose a different approach.
4 Retry logic for transient errors Automatically retry failed function calls when the API returns transient errors like rate limits or temporary outages.
5 Timeout handling in function-calling workflows Decide how long to wait for the LLM to respond before your code should stop waiting and handle the failure.
6 Invalid arguments from model Detect and handle when the model generates tool arguments that don't match your schema or are unparseable JSON.
7 Graceful degradation pattern Design your function-calling system to continue working even when the model refuses to call a function or returns malformed output.
7 Testing Tool-Calling Systems 7 lessons
1
Unit testing tool functions Write isolated tests for your tool functions before connecting them to the LLM to catch logic bugs early.
2 Testing tool selection logic Verify that the model correctly selects which tool to call for a given user query before running production requests.
3 Testing with invalid inputs Validate that your function schema rejects malformed inputs before they reach production.
4 End-to-end testing Write a test that sends a real request to the model, triggers tool use, executes the tool, and returns the final response to verify the complete loop works.
5 Mocking tool calls in tests Replace real API calls with fake tool responses in unit tests to verify your agent's behavior without calling external services.
6 Evaluation of tool use accuracy Verify that the model's tool calls match the arguments your functions actually expect before executing them.
7 Common failure modes Identify and fix the 5 most common function-calling mistakes before they cascade into production bugs.