Lesson · 40 min · Free
Tool Calling: Giving Agents Superpowers
Tool Calling: Giving Agents Superpowers body { font-family: sans-serif; line-height: 1.6; color: #333; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 1em; border-radius: 5px; overflow-x: auto; } c
AI Agents Crash Course: From Zero to Nutrition Agent
Tool Calling: Giving Agents Superpowers
In the previous modules, we've explored the foundational concepts of AI agents, their architecture, and the core components that enable them to reason and act. While large language models (LLMs) are incredibly powerful for natural language understanding and generation, their knowledge is inherently limited to their training data. They cannot, for instance, browse the live internet, execute code, access proprietary databases, or interact with external APIs in real-time without an additional mechanism. This is where tool calling comes into play, a critical paradigm that transforms a passive LLM into an active, problem-solving agent. Tool calling, also known as function calling, refers to the ability of an LLM to identify when an external function or "tool" is needed to fulfill a user's request, and then to generate the necessary arguments to call that function. The LLM doesn't execute the tool itself; rather, it outputs a structured request (often JSON) that describes which tool to use and with what parameters. An external orchestrator (your agent's framework) then intercepts this request, executes the actual tool, and feeds the tool's output back to the LLM. This feedback loop allows the LLM to incorporate real-world information and continue its reasoning process, leading to more accurate, up-to-date, and capable responses. Consider a pharmaceutical research scenario: an agent might need to look up the latest clinical trial data for a specific drug, calculate a pharmacokinetic parameter based on patient-specific inputs, or query an internal drug interaction database. None of these tasks can be accomplished by the LLM alone. By providing the agent with tools like a web search API, a Python interpreter for calculations, or a database query function, we empower it to extend its capabilities far beyond its inherent knowledge. The process typically involves: Tool Definition: You define the available tools to the LLM, often by providing a structured schema (e.g., OpenAPI specification or a simple JSON description) that includes the tool's name, a description of its purpose, and the parameters it accepts. LLM Inference: The LLM receives a user prompt and, based on its training and the provided tool definitions, decides if a tool is necessary. If so, it generates a structured call to that tool. Tool Execution: An external agent framework intercepts the LLM's tool call, validates it, and executes the actual function/API. Observation & Re-prompt: The output from the tool execution is then fed back to the LLM as an observation. The LLM can then use this new information to formulate a final answer, decide to call another tool, or refine its understanding.
Example: Defining a Simple Tool for Drug Information Retrieval
Let's imagine we want our agent to be able to retrieve basic information about a drug from a hypothetical internal database. We would define this tool for the LLM. Note that the actual implementation of get_drug_info is separate from its definition for the LLM. # Python code representing the actual tool implementation (not seen by the LLM directly) def get_drug_info(drug_name: str) -> dict: """ Retrieves basic information about a drug from an internal database. Args: drug_name (str): The name of the drug to look up. Returns: dict: A dictionary containing drug details like active ingredient, common indications, and known side effects, or an empty dict if not found. """ # In a real scenario, this would query a database or an external API drug_data = { "Paracetamol": { "active_ingredient": "Acetaminophen", "indications": ["Pain relief", "Fever reduction"], "side_effects": ["Liver damage (overdose)", "Allergic reactions"] }, "Aspirin": { "active_ingredient": "Acetylsalicylic acid", "indications": ["Pain relief", "Fever reduction", "Anti-inflammatory", "Antiplatelet"], "side_effects": ["Stomach upset", "Bleeding", "Reye's syndrome (children)"] } } return drug_data.get(drug_name, {}) # How we might define this tool for an LLM (e.g., using OpenAI's function calling format) tool_definition = { "type": "function", "function": { "name": "get_drug_info", "description": "Retrieves basic information about a drug from an internal database.", "parameters": { "type": "object", "properties": { "drug_name": { "type": "string", "description": "The name of the drug (e.g., 'Paracetamol', 'Aspirin')." } }, "required": ["drug_name"] } } } When a user asks, "What are the side effects of Aspirin?", the LLM, having been presented with the tool_definition , would recognize that get_drug_info is relevant. It would then generate a tool call similar to this (often as part of a JSON response from the LLM): { "tool_calls": [ { "id": "call_abc123", "function": { "name": "get_drug_info", "arguments": { "drug_name": "Aspirin" } }, "type": "function" } ] } Your agent's orchestrator would then execute the actual get_drug_info("Aspirin") function, receive its output, and feed it back to the LLM. The LLM would then synthesize this information into a human-readable answer. For pharmacy and biotech students, the implications are profound. Imagine an agent that can not only answer questions about drug mechanisms but also: Query real-time drug availability in pharmacies. Calculate complex drug-drug interaction risks based on a patient's medication list. Access and summarize the latest peer-reviewed literature on a novel therapeutic target. Simulate drug kinetics based on patient demographics and dosage. These capabilities move agents from mere conversational interfaces to powerful assistants that can augment human expertise in critical decision-making processes.
Key Takeaways
Tool calling extends LLM capabilities: It allows LLMs to interact with external systems and access real-world, up-to-date, or proprietary information. LLMs don't execute tools: They generate structured calls; an external orchestrator executes the tool and feeds the result back. Structured Tool Definitions are Crucial: Clear descriptions and parameter schemas enable the LLM to correctly identify and use tools. Enables real-world applications: From web browsing to database queries and code execution, tools unlock practical utility for agents in specialized domains. Essential for domain-specific agents: For pharmacy/biotech, tools bridge the gap between general LLM knowledge and specific scientific or clinical data.
Practice Exercise
You are designing an AI agent for a clinical pharmacist. The agent needs to be able to calculate a patient's creatinine clearance (CrCl) using the Cockcroft-Gault equation. Propose a tool definition for a Python function that performs this calculation. Consider the necessary parameters (e.g., age, weight, serum creatinine, sex) and their data types. Describe how the LLM would decide to call this tool based on a user prompt, and what the expected output from the tool would be, which the LLM would then interpret.
Watch the full lesson — free
This topic is part of AI Agents Crash Course: From Zero to Nutrition Agent, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →