LLM Tool Call Pattern

The Tool Call pattern (also known as Function Calling) enables Large Language Models to interact with external systems, APIs, and data sources by invoking predefined tools during a conversation.

Overview

LLM Tool Call Pattern

Pattern Description

This pattern allows LLMs to:

  1. Analyze user requests and determine when external data or actions are needed
  2. Generate structured tool calls with appropriate arguments
  3. Process tool results and incorporate them into natural language responses

Components

User

Application

LLM API

Tool Router

Tools

External APIs & Data Sources

Sequence Flow

Step From To Description
1 User Application User sends a message
2 Application LLM API Request includes message + tool definitions
3 LLM API Application LLM returns tool_calls if needed
4 Application Tool Router Route each tool call
5 Tool Router Tools Execute with provided arguments
6 Tools External Query APIs or data sources
7 External Tools Return results
8 Tools Application Return formatted results
9 Application LLM API Continue conversation with results
10 LLM API Application Generate final response
11 Application User Display response

Tool Definition Example

{
  "type": "function",
  "function": {
    "name": "get_weather",
    "description": "Get current weather for a location",
    "parameters": {
      "type": "object",
      "properties": {
        "location": {
          "type": "string",
          "description": "City name or coordinates"
        },
        "units": {
          "type": "string",
          "enum": ["celsius", "fahrenheit"],
          "default": "celsius"
        }
      },
      "required": ["location"]
    }
  }
}

Tool Call Response Example

{
  "role": "assistant",
  "content": null,
  "tool_calls": [
    {
      "id": "call_abc123",
      "type": "function",
      "function": {
        "name": "get_weather",
        "arguments": "{\"location\": \"San Francisco\", \"units\": \"celsius\"}"
      }
    }
  ]
}

When to Use

Implementation Considerations

Security

Error Handling

Performance

Design Principles

Pattern Relationship
Agentic RAG Uses tool calls for retrieval
ReAct (Reasoning + Acting) Combines reasoning with tool use
Chain of Thought Reasoning before tool selection

PlantUML Source

@startuml

title LLM Tool Call Pattern

skinparam participant {
    BackgroundColor #E3F2FD
    BorderColor #1565C0
}

actor User
participant "Application" as App
participant "LLM API" as LLM
participant "Tool Router" as Router
collections "Tools" as Tools
database "External APIs\n& Data Sources" as External

== User Request ==
User -> App : Send message
activate App

App -> LLM : API Request with\n- User message\n- Tool definitions\n- System prompt
activate LLM

LLM --> App : Response with\ntool_calls array
deactivate LLM

== Tool Execution ==
App -> Router : Route tool call
activate Router

Router -> Tools : Execute tool\nwith arguments
activate Tools

Tools -> External : Query/Action
activate External
External --> Tools : Result
deactivate External

Tools --> Router : Tool result
deactivate Tools

Router --> App : Formatted result
deactivate Router

== Response Generation ==
App -> LLM : Continue with\ntool results
activate LLM

LLM --> App : Final response
deactivate LLM

App --> User : Display response
deactivate App

@enduml