Function Calling and API Integration
Master how to prompt LLMs to call external functions or APIs, enabling them to interact with real-world systems.
Function Calling and API Integration is a free AI Prompt Engineering lesson on CoddyKit — lesson 1 of 3. 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 AI Prompt Engineering learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Call External Tools
LLMs are powerful, but they primarily live in a text world. What if they need real-world information or need to perform actions?
Function calling lets LLMs interact with external tools and APIs! This bridges the gap between language and action, allowing AI to:
- Get current information (weather, stock prices)
- Perform actions (send emails, book appointments)
- Connect to databases or custom tools
It unlocks new possibilities for AI systems.
LLM's Role: Suggesting Calls
When you enable function calling, you provide the LLM with descriptions of available tools (functions).
Instead of directly answering, the LLM might decide that a tool can help. It then outputs a structured "suggestion" for a function call, including the function's name and its arguments.
Important: The LLM doesn't execute the function itself. It just tells your application what function to call and with what inputs.
Defining Your Tools
To use function calling, you must define your tools (functions) in a way the LLM can understand. This usually involves providing:
- A name for the function (e.g.,
get_current_weather) - A description of what it does (critical for the LLM to choose correctly)
- The parameters it accepts, including their types and descriptions (e.g.,
location: string)
This "schema" tells the LLM everything it needs to know to suggest a valid call.
Weather Tool Definition
Here's a conceptual Python definition for a weather tool. Notice the clear description and parameters. This is what you'd expose to the LLM.
This function represents the external tool an LLM would conceptually "call".
def get_current_weather(location: str, unit: str = "celsius"):
"""
Get the current weather in a given location.
Args:
location (str): The city and state, e.g. "London, UK"
unit (str): The unit of temperature, "celsius" or "fahrenheit"
Returns:
dict: Weather information.
"""
# In a real app, this would call a weather API
if location == "London, UK":
return {"temperature": "15", "unit": unit, "forecast": "cloudy"}
elif location == "New York, USA":
return {"temperature": "20", "unit": unit, "forecast": "sunny"}
else:
return {"temperature": "N/A", "unit": unit, "forecast": "unknown"}
Crafting Prompts for Tools
To encourage the LLM to use your tools, your prompt needs to clearly state the user's intent. The LLM then decides if a tool can help.
You don't explicitly tell the LLM "call get_weather". Instead, you ask a question that implies it:
- "What's the weather like in London?"
- "Can you book a meeting for me tomorrow?"
The LLM, armed with tool descriptions, will infer the need for a function call.
LLM's Call Suggestion
When the LLM decides to use a tool, it doesn't give you a direct answer. Instead, it returns a structured object, typically JSON, indicating the function to call and its arguments.
Your application then parses this output.
Here's what an LLM might suggest if asked "What's the weather in London?":
{
"tool_calls": [
{
"id": "call_abc123",
"function": {
"name": "get_current_weather",
"arguments": "{\"location\": \"London, UK\", \"unit\": \"celsius\"}"
},
"type": "function"
}
]
}Your App Runs the Tool
Upon receiving the LLM's function call suggestion, your application takes over:
- It identifies the requested function (e.g.,
get_current_weather). - It extracts the arguments (e.g.,
"London, UK"). - It executes the actual Python function (or makes the API call) within your application's environment.
This is where the "real-world" action happens outside the LLM.
LLM Processes Tool Output
After your application executes the function, it receives a result (e.g., {"temperature": "15", ...}).
This result is then passed back to the LLM, along with the original conversation history and the function call request.
The LLM then uses this new information to generate a natural language response to the user's original query.
End-to-End Interaction
Let's put it all together. This example shows the flow where a user's question triggers an LLM to suggest a function call, your app executes it, and the LLM then gives a final answer.
This is a simplified representation of the interaction loop.
def get_current_weather(location, unit="celsius"):
if location == "London, UK":
return {"temp": "15", "unit": unit, "forecast": "cloudy"}
return {"temp": "N/A", "unit": unit, "forecast": "unknown"}
def simulate_llm_response(user_query):
# LLM 'thinks' and decides to call a tool
if "weather in London" in user_query:
return {
"type": "tool_call",
"function": "get_current_weather",
"args": {"location": "London, UK"}
}
return {"type": "text", "content": "I cannot help with that."}
def simulate_llm_final_response(tool_output):
# LLM processes tool output and gives natural language
temp = tool_output.get("temp", "N/A")
forecast = tool_output.get("forecast", "unknown")
return f"The weather in London is {temp} degrees and {forecast}."
def main():
user_input = "What is the weather in London, UK?"
print(f"User: {user_input}")
llm_output = simulate_llm_response(user_input)
if llm_output["type"] == "tool_call":
func_name = llm_output["function"]
func_args = llm_output["args"]
print(f"LLM suggested calling: {func_name}({func_args})")
# Execute the function
tool_result = get_current_weather(**func_args)
print(f"Tool output: {tool_result}")
# Feed result back to LLM
final_answer = simulate_llm_final_response(tool_result)
print(f"LLM's final answer: {final_answer}")
else:
print(f"LLM: {llm_output['content']}")
if __name__ == "__main__":
main()
Function Calling Check
Imagine an LLM returns a function call request. What is the next immediate step your application should take?
Recap: Tools in Action
In this lesson, we explored how LLMs can interact with the real world through function calling and API integration.
We learned that:
- You define tools (functions) with descriptions and parameters.
- The LLM suggests which tool to call and with what arguments.
- Your application executes the actual tool and feeds the result back.
- The LLM then uses the result to formulate a final, informed response.
This powerful technique allows LLMs to go beyond just text generation, enabling them to perform actions and retrieve up-to-date information.
Frequently asked questions
Is the “Function Calling and API Integration” lesson free?
Yes — the full text of “Function Calling and API Integration” is free to read here on the web, and the AI Prompt Engineering course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Function Calling and API Integration”?
Master how to prompt LLMs to call external functions or APIs, enabling them to interact with real-world systems. You practise AI Prompt Engineering 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 AI Prompt Engineering?
No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Function Calling and API Integration” 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 AI Prompt Engineering lesson?
Yes. Every AI Prompt Engineering 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
- Function Calling and API Integration
- Multi-Agent Prompting Systems
- Custom Tools and Plugins for LLMs