Understanding Function Calling and ReAct for LLM Agents with LangChain

This article explains how large language models can act as agents by using OpenAI's Function Calling and the ReAct prompting paradigm, compares their trade‑offs, and demonstrates practical implementations with LangChain, including code examples for defining tools, invoking functions, and orchestrating multi‑step reasoning.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Understanding Function Calling and ReAct for LLM Agents with LangChain

ChatGPT's plugin mechanism has become popular, and many API‑wrapped clients now adopt similar plugin systems. To understand how LLMs decide which plugins to call, we first need to grasp the concept of an Agent, which uses a language model as a reasoning engine to select and order actions.

An Agent can be thought of as an intelligent entity that breaks down a problem, infers required actions, and selects appropriate tools. In the AI era, the LLM itself serves as this intelligent agent.

Plugins are essentially the tools that the LLM perceives and can invoke. There are two mainstream ways to enable this capability:

ChatGPT Function Calling ReAct prompting

Function Calling is a feature introduced in OpenAI's 2023‑06‑13 release that lets the model call predefined functions using a JSON schema for arguments. The usage is straightforward, as shown in the following JavaScript example:

const api = new OpenAIApi(configuration)
const response = await api.createChatCompletion({
  // note: use a model released after 613
  model: 'gpt-3.5-turbo-16k',
  messages: [/* omitted */],
  // 🔴 define function schema
  functions: [
    {
      name: 'calculator',
      description: 'Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.',
      parameters: {
        type: 'object',
        properties: {
          expr: {
            type: 'string',
            description: 'mathematical expression'
          }
        }
      }
    },
    {
      name: 'bing-search',
      description: 'a search engine. useful for when you need to answer questions about current events. input should be a search query.',
      parameters: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'search query'
          }
        }
      }
    }
  ]
})

The response includes a function_call field indicating which function to invoke and the arguments to pass. By feeding the function's result back into the conversation, the LLM can continue reasoning until it reaches a final answer.

To illustrate, the article walks through a concrete example: answering the question "Who is Kobe Bryant's wife and what is twice her weight in kilograms?" The LLM first calls bing-search to retrieve the wife's name and weight, then calls calculator to compute the doubled weight, finally returning the answer.

ReAct (Reason + Act) is a prompting paradigm that makes the reasoning process explicit. It follows a three‑step loop:

Thought : chain‑of‑thought reasoning.

Action : select a tool to execute.

Observation : result of the action.

The article provides a ReAct prompt template and shows how the same Kobe‑wife question is solved using a sequence of Thought/Action/Observation steps, first searching with bing-search and then calculating with calculator.

Both approaches aim to give LLMs external tool access, but Function Calling is a closed‑source, fine‑tuned feature that is easier to use with fewer tokens, while ReAct is model‑agnostic and more flexible, though it requires longer prompts.

Finally, the article demonstrates how to integrate these mechanisms with LangChain , a framework that abstracts agents, tools, and chains. A concise Node.js snippet shows how to initialize an OpenAI model, define WebBrowser and Calculator tools, create a zero‑shot‑react executor, and call it with a user query:

import { initializeAgentExecutorWithOptions } from 'langchain/agents'
import { OpenAI } from 'langchain/llms/openai'
import { OpenAIEmbeddings } from 'langchain/embeddings/openai'
import { WebBrowser } from 'langchain/tools/webbrowser'
import { Calculator } from 'langchain/tools/calculator'

const model = new OpenAI({ temperature: 0, modelName: 'gpt-3.5-turbo-16k' })
const embeddings = new OpenAIEmbeddings()
const tools = [new WebBrowser({ model, embeddings }), new Calculator()]
const executor = await initializeAgentExecutorWithOptions(tools, model, { agentType: 'zero-shot-react-description' })
const res = await executor.call({ input: '科比的老婆是谁,它的体重的两倍是多少公斤?' })
console.log(JSON.stringify(res, null, 2))

LangChain simplifies chaining prompts, LLMs, memory, and agents, and provides a rich ecosystem of tools for building LLM applications.

In summary, Function Calling offers a compact, stable way to invoke tools on OpenAI models, whereas ReAct provides a universal, transparent reasoning loop that works across different LLMs. Using LangChain, developers can easily experiment with both paradigms.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AILLMLangChainAgentFunction Calling
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.