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

User LLM selects tool + generates args Your Code validates args executes function LLM formats final answer Reply

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.

python
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

Building assistants that can search, calculate, and take actions
Automating workflows that span multiple APIs
Creating coding assistants that can execute and test code
Building agents that interact with databases via natural language

Common Mistakes

Not validating tool arguments before execution — the model can generate invalid inputs
Having tool descriptions that are too vague for the model to use correctly
Not handling tool execution errors gracefully
Giving too many tools (>20) which degrades tool selection accuracy

Interview Insight

Relevance

High - Core agent capability

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In