Creative Genius Creative Genius
Lesson 2 of 4 · 22 min read

Tool Use Fundamentals

Function calling is the spine of every agent. Master it before adding frameworks.

Tool use (a.k.a. function calling) lets the LLM choose to call a function instead of replying directly.

Code

import OpenAI from "openai";
const client = new OpenAI();

const tools = [{
  type: "function",
  function: {
    name: "get_weather",
    description: "Get current weather for a city",
    parameters: {
      type: "object",
      properties: { city: { type: "string" } },
      required: ["city"],
    },
  },
}];

const r = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "What's the weather in Miami?" }],
  tools,
});

// r.choices[0].message.tool_calls -> [{ name: "get_weather", arguments: '{"city":"Miami"}' }]
← Agents vs. Pipelines Stop Conditions: Don't Burn $1000 in 3 Minutes →