Skip to main content
Large Language Models cannot directly execute python code or fetch URL contents. Instead, they use Function Calling (Tool Binding), where the model outputs a structured JSON request indicating which tool to run and what arguments to pass. The client application executes the code locally and returns the result to the model.

1. The Function Calling Lifecycle

The function calling loop operates as a communication contract between the application client and the LLM API:
[!IMPORTANT] LLMs do not run your tools. The model only generates the JSON arguments specifying how you should run them. Your Python script is responsible for executing the function and feeding the text result back to the model.

2. Defining & Binding Tools in LangChain

In LangChain, you convert any standard Python function into a tool using the @tool decorator. The decorator automatically generates the JSON schema description based on your function name, docstring, and type hints.

2.1 Python Implementation

Below is a working script demonstrating tool binding. The model evaluates a user question and outputs a structured tool call request.
Output:

3. Practice Exercises

Practice 1: Binding Multiple Tools

Create a second tool called get_current_weather(city_name: str) that returns a mock weather string (e.g. "22°C and sunny"). Bind both calculate_salary_bonus and get_current_weather to your model. Query "What is the weather in London?" and verify the correct tool call is returned. Instructions:
  1. Write the get_current_weather tool with a docstring.
  2. Call llm.bind_tools([calculate_salary_bonus, get_current_weather]).
  3. Invoke the query and print response.tool_calls.