LLM Function Calling: The Tool Definition Contract
Ever wonder how LLMs "know" what functions to call? It's all in the contract!
LLMs need explicit, structured descriptions (tool definition contracts) of external functions to call them reliably.
π― System Scenario & Requirements
System Goal
LLMs need explicit, structured descriptions (tool definition contracts) of external functions to call them reliably.
Actors & Components
- Request Gateway
- Policy & Router
- Target Executors
- Request Entrypoint
- Policy Router
- Target Handler
Functional Requirements
- LLMs need explicit, structured descriptions (tool definition contracts) of external functions to call them reliably.
- Without this contract, they'll hallucinate or misuse tools, making your AI agents unpredictable.
- π€ As AI systems evolve from simple text generation to intelligent agents, they need to interact with the real world by calling external tools or APIs.
Constraints & Assumptions
- Operational latency, consistency levels, and network partition boundaries must be enforced.
β‘ Execution Stages & Decision Trace
Stage 1 β Crafting the Function Contract
The solution is a "tool definition contract," often expressed as a JSON schema, that you provide to the LLM alongside the user's prompt.
This contract acts as a precise instruction manual for each available function.
Deep Dive
Deep Dive: Stage 1 β Crafting the Function Contract
The solution is a "tool definition contract," often expressed as a JSON schema, that you provide to the LLM alongside the user's prompt. This contract acts as a precise instruction manual for each available function. It specifies the function's name, a human-readable description of its purpose, and a detailed schema for its input parameters. By parsing this contract, the LLM can accurately determine if a user's intent matches an available tool, extract the necessary arguments from the prompt, and generate a correctly formatted function call. This structured approach significantly improves the LLM's ability to use tools reliably and consistently.
Observable outcome: This structured approach significantly improves the LLM's ability to use tools reliably and consistently.
Deep Dive: Stage 2 β How LLMs Use the Contract
When an LLM receives a prompt and a set of tool definitions, it first analyzes the user's intent. If it identifies an intent that can be fulfilled by one of the defined tools, it then consults the tool's parameter schema. It extracts relevant information from the user's prompt to populate these parameters, ensuring the generated function call adheres to the tool's API specifications. The LLM then outputs a structured JSON object representing the function call, which your application intercepts, executes, and potentially feeds the result back to the LLM for further processing.
[
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature to return"
}
},
"required": ["location"]
}
}
}
]
Explanation: This JSON array defines a single tool, get_current_weather. It clearly states its purpose and the location and unit parameters it expects, including their types and a description. The required: ["location"] ensures the LLM knows location is mandatory. With this contract, an LLM can transform "What's the weather in Seattle?" into {"name": "get_current_weather", "arguments": {"location": "Seattle"}}.
Observable outcome: With this contract, an LLM can transform "What's the weather in Seattle?" into {"name": "get current weather", "arguments": {"location": "Seattle"}} .
π System Outcome & Trade-offs
Observed User & Operator Behavior
User: Clients observe deterministic state without lost updates or race conditions.
Operator: Operators can verify stage outcomes against the scenario requirements.
Key Architectural Trade-offs
- System guarantees are traded against replication overhead and failover convergence times.
Key learnings
- The solution is a "tool definition contract," often expressed as a JSON schema, that you provide to the LLM alongside the user's prompt.
- When an LLM receives a prompt and a set of tool definitions, it first analyzes the user's intent.
Apply Concepts from this Scenario
Test your understanding of the architecture decisions and trade-offs presented in this case study.
