Tool Use (Function Calling)
Enabling LLMs to interact with external APIs with strict schema enforcement.
Advanced Tool Execution
Basic function calling simply returns a JSON string, which breaks heavily in production. Modern AI Engineering requires Strict Schema Enforcement (like Pydantic parsing and OpenAI Structured Outputs) to guarantee valid payloads.
How Function Calling Works
Isolated Sandboxing
Never run LLM-generated code on your backend. Production systems rely on secure micro-VMs (e.g., E2B, Firecracker) to dynamically execute data-analysis code, securely stream the stdout/stderr, and tear down the environment instantly.
Speculative API Execution
Mature agents don't wait sequentially. They employ speculative execution (via asyncio.gather) to execute multiple parallel tools simultaneously, aggressively buying down Time-to-First-Byte (TTFB) latency floors.
Code Example
The model intelligently selects which tools to call and generates proper arguments. Here it calls both weather and flight tools in parallel.
1from openai import OpenAI
2import json
3
4client = OpenAI()
5
6def get_weather(location: str) -> str:
7 # In production, call a real weather API
8 return json.dumps({"location": location, "temp": "22°C", "condition": "sunny"})
9
10def search_flights(origin: str, destination: str) -> str:
11 return json.dumps({"flights": [{"price": "$450", "airline": "United", "time": "10:00 AM"}]})
12
13tools = [
14 {"type": "function", "function": {
15 "name": "get_weather",
16 "description": "Get current weather for a city",
17 "parameters": {"type": "object", "properties": {
18 "location": {"type": "string"}
19 }, "required": ["location"]}
20 }},
21 {"type": "function", "function": {
22 "name": "search_flights",
23 "description": "Search for flights between cities",
24 "parameters": {"type": "object", "properties": {
25 "origin": {"type": "string"},
26 "destination": {"type": "string"}
27 }, "required": ["origin", "destination"]}
28 }}
29]
30
31response = client.chat.completions.create(
32 model="gpt-4o",
33 messages=[{"role": "user", "content": "Plan a trip from SF to NYC. What's the weather there and find me flights."}],
34 tools=tools
35)
36
37# Model calls BOTH tools in parallel
38for call in response.choices[0].message.tool_calls:
39 print(f"Tool: {call.function.name}")
40 print(f"Args: {call.function.arguments}")Use Cases
Common Mistakes
Interview Insight
Relevance
High - Core agent capability