How AI Actually Uses Tools: The Developer's Guide to Function Calling
By Weavecode Team
Published 2026-07-14
3 min read
How AI Actually Uses Tools: The Developer's Guide to Function Calling
In early integrations, LLMs were closed text handlers. Today, modern models are capable of tool use or function calling—the ability to interact with databases, calculators, external APIs, and local scripts.
But how does a model actually execute a function?
Crucially, the model does not run the code itself. Instead, the model outputs structured arguments (usually in JSON format) matching a schema you define. Your application receives these arguments, executes the local code, and returns the output to the model to continue reasoning.
1. The Tool Calling Lifecycle
User: "Get user ID 123" ──► Weavecode API (evaluates request against Tool Schema) │ ▼ Local DB Query ◄── (executes) ◄── Tool Call Response (JSON: {"user_id": "123"})
Schema Submission: The developer passes a list of available tools (defined as JSON schemas) alongside the user's prompt.
Analysis & Tool Output Selection: The model evaluates the prompt and determines that a tool is required. It outputs a tool_calls block containing arguments matching the tool schema.
Local Execution: Your application intercepts the response, reads the structured JSON arguments, calls the local function, and gathers the result.
Context Return: The result of the tool run is passed back to the model, allowing it to compose the final answer.
2. Python Implementation: Building Tool Loops
Let's build a script that queries a weather API dynamically using Weavecode endpoints.
Step 2.1: Define the Tool Schema
tools_schema = [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current temperature in a city", "parameters": { "type": "object", "properties": { "location": {"type": "string", "description": "e.g., San Francisco, CA"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["location"] } } }]
Step 2.2: Implement the Agentic Loop
import osimport jsonfrom openai import OpenAIclient = OpenAI(base_url="https://api.weavecode.ai/v1", api_key=os.getenv("WEAVECODE_KEY"))# Local mock functiondef get_current_weather(location, unit="celsius"): return json.dumps({"location": location, "temperature": "22", "unit": unit})# 1. Send query + tool schemasmessages = [{"role": "user", "content": "What is the temperature in Tokyo right now?"}]response = client.chat.completions.create( model="google/gemini-1.5-flash", messages=messages, tools=tools_schema)# 2. Check if model wants to call a toolmessage = response.choices[0].messageif message.tool_calls: for tool_call in message.tool_calls: if tool_call.function.name == "get_current_weather": args = json.loads(tool_call.function.arguments) # Execute local function result = get_current_weather(location=args["location"]) # 3. Pass result back to model messages.append(message) messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": result }) final_response = client.chat.completions.create( model="google/gemini-1.5-flash", messages=messages ) print("Final Agent Answer:", final_response.choices[0].message.content)