Function Calling Basics

What is Function Calling?

Function calling allows LLMs to:

Basic Flow

1. Define Available Functions

const functions = [
  {
    name: 'get_weather',
    description: 'Get current weather for a city',
    parameters: {
      type: 'object',
      properties: {
        location: {
          type: 'string',
          description: 'City name'
        }
      },
      required: ['location']
    }
  },
  {
    name: 'get_stock_price',
    description: 'Get current stock price',
    parameters: {
      type: 'object',
      properties: {
        symbol: {
          type: 'string',
          description: 'Stock ticker symbol'
        }
      },
      required: ['symbol']
    }
  }
];

2. Message Flow

// 1. User Message
{
  role: 'user',
  content: 'What's the weather like in London?'
}

// 2. LLM Response with Function Call
{
  role: 'assistant',
  content: null,
  tool_calls: [{
    id: 'call_abc123',
    type: 'function',
    function: {
      name: 'get_weather',
      arguments: '{"location":"London"}'
    }
  }]
}

// 3. Function Execution Result
{
  role: 'tool',
  content: '{"temperature": 18, "condition": "cloudy"}',
  tool_call_id: 'call_abc123'
}

// 4. Final LLM Response
{
  role: 'assistant',
  content: 'The weather in London is currently cloudy with a temperature of 18°C.'
}

How LLMs Choose Functions

Function Selection Process

  1. Intent Recognition
  2. Function Matching

Examples of Clear vs Ambiguous Matches

// Clear Match
User: "What's the stock price of Apple?"
LLM: Calls get_stock_price with {"symbol": "AAPL"}

// Ambiguous Match
User: "How's AAPL doing today?"
LLM: Could call get_stock_price or might give general info

// No Match
User: "Tell me about TypeScript"
LLM: Regular response, no function call

Common Function Types

1. Data Retrieval

{
  name: 'search_products',
  description: 'Search product database',
  parameters: {
    type: 'object',
    properties: {
      query: { type: 'string' },
      category: { type: 'string', optional: true }
    }
  }
}