Lesson 2 of 6·12 min read

Tool Use and Function Calling

Tools make the difference between a "smart writer" and an "agent that can act." Through tool use, an AI agent can interact with the real world: query databases, call APIs, create files, send emails.

How Does Function Calling Work?

Modern LLMs (Claude, GPT-5, Gemini) support native function calling:

  1. Tool definitions: You describe available tools as JSON schema
  2. LLM decision: The model decides which tool to call with which parameters
  3. Execution: Your code executes the tool call
  4. Result: The result is returned to the LLM
const tools = [{
  name: "search_customers",
  description: "Search customers by name or email",
  parameters: {
    type: "object",
    properties: {
      query: { type: "string", description: "Search term" },
      limit: { type: "number", description: "Max results", default: 10 }
    },
    required: ["query"]
  }
}]

Tool Categories

Database Tools

  • SQL queries (read-only for security)
  • CRUD operations with validation
  • Aggregations and reports

API Tools

  • Call REST/GraphQL endpoints
  • Trigger webhooks
  • Third-party integrations (Slack, Jira, HubSpot)

File Tools

  • Read and write files
  • Generate documents (PDF, Excel, CSV)
  • Process images and screenshots

System Tools

  • Execute shell commands (sandboxed!)
  • Start/stop containers
  • Retrieve monitoring data

Best Practices for Tool Design

PrincipleDescription
Single ResponsibilityOne tool = one clearly defined task
Clear DescriptionThe LLM must understand when to use which tool
ValidationValidate input parameters before executing the action
IdempotencySame input → same result (where possible)
Error HandlingClear error messages the LLM can process

Security Aspects

  • Read vs. Write: Read-only tools as default, write tools explicitly enabled
  • Sandboxing: Execute shell commands only in isolated containers
  • Rate Limiting: Cap maximum tool calls per session
  • Approval Flow: Critical actions (delete, send) require human approval

Practical tip: Start with 3–5 well-defined tools. More tools = more confusion for the LLM. If accuracy drops, you probably have too many similar tools.