VITALIFY.ASIA logo

What Is Function Calling? How AI Agents Work and How to Implement Them with LangGraph

Author profile
Toshihiko Nagaoka06/30/2026
What Is Function Calling? How AI Agents Work and How to Implement Them with LangGraph

Today, combining an LLM with external tools makes it possible to develop a custom AI agent that can search company data, perform calculations, run tests, and manipulate files.

For a small prototype, APIs and existing frameworks now make it possible to build these systems with far less code than before.

The core technology behind this is Function Calling.

“Recent AI systems do more than chat. They seem to rewrite files and test programs by themselves. Is that really happening?”

If you have noticed this, you are absolutely right.

AI technology surrounding large language models, or LLMs, has recently undergone a dramatic shift. It is moving beyond the phase of merely generating plausible text and into a phase in which AI can autonomously use external tools to complete tasks.

Behind this evolution are technologies known as Function Calling and Tool Use, as well as the AI Agent design pattern built on top of them.

In this article, we will explain the fundamentals of this mechanism, how AI agents evolved into today’s advanced development tools, and how developers can build and use these systems in real-world work.

Giving an LLM a Calculator: What Is Function Calling?

LLMs are extremely good at generating text. However, they are not particularly good at exact calculations or retrieving real-time information, such as today’s weather or the latest stock price.

This is because an LLM essentially predicts the most probable word or token to come next.

For example, when asked to calculate 12,345 × 67,890, an LLM may produce a plausible-looking but incorrect answer, known as a hallucination.

Function Calling was introduced to address this problem.

Instead of asking the LLM to perform the calculation itself, the system asks it to generate instructions for using an external function or API, such as a calculator or weather API.

The process works as follows:

  1. User: Enters, “What is 12,345 × 67,890?”
  2. LLM: Determines that it should use a multiplication tool rather than calculate the answer itself.
  3. LLM output: Produces a function-call instruction such as multiply(a=12345, b=67890) as structured data.
  4. Application: Executes the actual calculator program and obtains the result, 838102050.
  5. LLM response: Receives the result and replies naturally, “The answer is 838,102,050.”

Instead of forcing the LLM to calculate, the system delegates the task to a program designed to do it accurately.

This significantly improves the accuracy of AI responses.

Can Developers Add This Capability to an Existing LLM?

“You have explained that Google Gemini and OpenAI GPT support these functions, but can we also connect an LLM to functions in our own system or local environment?”

The answer is yes.

In fact, this has become a standard approach in modern AI application development.

There are two main ways to implement it.

Approach A: Use the API’s Built-In Functionality

Major APIs such as Gemini and GPT already provide mechanisms that allow developers to register custom functions.

You only need to provide the LLM with the name and description of your program, such as a function that searches your company’s customer database.

Based on the user’s question, the LLM can then automatically return an instruction such as:

Run this function with these arguments.

Approach B: Control the Process Through Prompts

Frameworks such as LangChain can implement a similar mechanism even when the API does not provide native Function Calling support. This approach is commonly used with open-source LLMs.

For example, you could instruct the model as follows:

You can use a tool called calc(expression). If a calculation is required, stop generating prose and output only Action: calc(expression).

When the application detects text in this format, it executes the calculation and sends the result back to the LLM as additional context.

The program automates this entire process.

What Is the Most Refined Design Pattern for Production Systems?

“Embedding function-execution commands directly inside natural-language chat sounds risky. Wouldn’t that cause parsing errors?”

That instinct is completely correct.

A common approach in production development is to prevent the LLM from producing prose during the tool-selection phase and require it to output only structured data, usually JSON.

[User’s question]
       │
       ▼
[LLM: First call] ── “Do not output prose. Return JSON only.”
       │
       ▼ Parse the JSON
[Execution in the application] ── “Search a database or perform a calculation.”
       │
       ▼ Add the result to the context
[LLM: Second call] ── “Combine all available context and generate the final answer.”

In this design, the LLM’s primary role is limited to deciding what action should happen next and packaging the required arguments.

The execution itself is handled by a trusted program controlled by the developer.

The result is then added to the conversation history as a response from an external tool, after which the LLM performs another round of reasoning.

This repeating cycle of reasoning and execution is the core design pattern behind an autonomous AI agent.

From Concept to Practical Use: The History of AI Coding Agents

The development tools available today have refined the following cycle to an advanced level:

Structured output → external program execution → context reinsertion

Let us look back at how this technology evolved.

1. Spring 2023: The Birth of the Concept — AutoGPT and BabyAGI

The global boom surrounding autonomous agents began with open-source projects such as AutoGPT and BabyAGI, which appeared in March 2023.

Users could provide an LLM with a goal, and the system would divide that goal into tasks, repeatedly search Google, and create files.

This behavior surprised many people and demonstrated the possibilities of agent technology to the wider world.

2. Summer 2023: Practical Use in the Terminal — Aider

Many early agents remained interesting concepts without becoming practical everyday tools.

Aider emerged as a tool that developers could use in their daily work.

It runs in the terminal and integrates closely with Git. It established a practical development-agent workflow in which an LLM modifies code and, when no problems are found, automatically creates a Git commit.

3. Spring 2024: The Impact of the First Full-Scale “AI Engineer” — Devin

In March 2024, Cognition introduced Devin as the world’s first fully autonomous AI software engineer.

Devin used its own sandbox environment, built-in browser, and terminal. It could interpret GitHub Issues, write and run tests, repair errors, and automate the process through deployment.

The announcement had a major impact on the software industry.

4. Late 2024 to the Present: Natural Integration into the IDE — Cursor

Devin took an approach based on delegating an entire task to AI.

Cursor instead integrated an agent directly into the IDE that developers already used to write code.

Features such as Composer allowed an LLM to autonomously search, modify, and apply changes across multiple files while a developer monitored and directed the process in real time from the editor.

This helped agent-based development move from an advanced experiment into the daily workflow of a much wider range of developers.

5. From 2025: Deeper Agent-First Development — Claude Code and Google Antigravity

AI agents are now moving beyond individual platforms and continuing to evolve.

Claude Code

Claude Code is a command-line agent released by Anthropic.

It uses Claude’s reasoning capabilities and fast responses to autonomously fix bugs, edit code, and run tests from the terminal.

Google Antigravity

Google Antigravity is a next-generation, agent-first development platform.

It takes advantage of an LLM’s ability to process extremely large contexts, potentially covering millions of tokens, to understand an entire project’s structure while autonomously running builds and tests in a virtual environment.

Building for Real-World Use: Create Your Own Specialized Agent

“Surely advanced systems such as Devin and Cursor can only be developed by Google or overseas startups?”

There is no need to assume that.

We can now build custom agents based on essentially the same mechanism for our own organizations and individual work.

There are three reasons why building a custom agent has become so practical.

1. Fully Structured Data Output — Structured Outputs

Modern major LLMs, including Gemini and GPT, provide functionality designed to ensure that generated JSON conforms to a specified schema or type definition.

This reduces the probability of parsing errors and makes integration with application systems dramatically more stable.

2. The Spread of Lightweight Agent Frameworks

Frameworks and libraries such as LangGraph for JavaScript and TypeScript, as well as CrewAI, make it possible to implement the following loop with type safety and only a few dozen lines of code:

Ask the LLM to decide → execute a function → return the result to the context

Let us examine a concrete implementation using the TypeScript version of LangGraph.

Example AI Agent Implementation with LangGraph and TypeScript

The following TypeScript agent gives an LLM access to a custom calculator tool and repeatedly performs the autonomous cycle below:

Reasoning → calculator execution → result reinsertion → final answer
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { BaseMessage, Annotation } from "@langchain/core/messages";
import { ChatOpenAI } from "@langchain/openai";
import { StateGraph, START, END } from "@langchain/langgraph";
import { ToolNode } from "@langchain/langgraph/prebuilt";

// 1. Define the type and schema for the agent's memory, or state.
// This state accumulates messages in an array.
const StateAnnotation = Annotation.Root({
  messages: Annotation<BaseMessage[]>({
    reducer: (x, y) => x.concat(y),
    default: () => [],
  }),
});

// 2. Define the custom function, or tool, available to the LLM.
// The Zod schema communicates the argument types and descriptions to the LLM.
const calculateMultiply = tool(
  async ({ a, b }) => {
    return (a * b).toString();
  },
  {
    name: "calculate_multiply",
    description:
      "A function that multiplies two numbers. The LLM reads this description and determines the arguments.",
    schema: z.object({
      a: z.number().describe("The first number"),
      b: z.number().describe("The second number"),
    }),
  }
);

const tools = [calculateMultiply];
const toolNode = new ToolNode(tools);

// Register, or bind, the tools to the model.
const model = new ChatOpenAI({
  modelName: "gpt-4o",
  temperature: 0,
}).bindTools(tools);

// 3. Define the nodes, or processing steps.

// This node calls the LLM and asks it to decide whether to generate text
// or execute a tool.
const callModel = async (state: typeof StateAnnotation.State) => {
  const response = await model.invoke(state.messages);
  return { messages: [response] };
};

// This conditional routing function examines the LLM's response and decides
// whether to execute a tool or end the workflow.
const shouldContinue = (state: typeof StateAnnotation.State) => {
  const lastMessage = state.messages[state.messages.length - 1];

  // When the LLM has decided to call a tool and tool_calls exists:
  if (
    lastMessage.additional_kwargs.tool_calls &&
    lastMessage.additional_kwargs.tool_calls.length > 0
  ) {
    return "tools"; // Proceed to the tools node.
  }

  return END; // End the workflow.
};

// 4. Build the graph, or network.
const workflow = new StateGraph(StateAnnotation)
  // Add the nodes.
  .addNode("agent", callModel)
  .addNode("tools", toolNode)
  // Add the edge from the starting point to the agent.
  .addEdge(START, "agent")
  // After the agent node, route according to shouldContinue.
  .addConditionalEdges("agent", shouldContinue)
  // After executing the tool, return to the agent node and reason again.
  .addEdge("tools", "agent");

// 5. Compile the graph into an executable application.
const app = workflow.compile();

// Execution entry point.
async function main() {
  const inputs = {
    messages: [
      {
        role: "user",
        content: "What is the result of multiplying 12345 by 67890?",
      },
    ],
  };

  const stream = await app.stream(inputs);

  for await (const chunk of stream) {
    // Output state transitions in real time.
    for (const [node, state] of Object.entries(chunk)) {
      const lastMsg = (state as any).messages?.[
        (state as any).messages.length - 1
      ];

      console.log(
        `[${node}] Current output:`,
        lastMsg?.content || "(Executing tool...)"
      );
    }
  }
}

main().catch(console.error);

In this TypeScript code, the final .addEdge("tools", "agent") clearly expresses the cyclic structure that returns the tool result to the agent node for another round of reasoning.

Type-safe definitions also provide robust management of the agent’s internal state through StateAnnotation, making it possible to deploy large autonomous workflows safely in production environments.

3. The Greatest Advantage: Building an Agent Specifically for Your Organization

Cursor is a general-purpose tool for writing code.

An agent that you build yourself can instead focus on highly domain-specific work that no general-purpose product can exactly reproduce.

For example, it could:

  • Search your organization’s internal database
  • Apply your own calculation logic
  • Post the result to an internal Slack channel

This is the greatest advantage of building a custom agent.

Conclusion: AI Is Moving from a Conversation Partner to a Colleague

Earlier LLMs were chatbots that simply returned long blocks of text in response to questions.

Function Calling gives an LLM the role of coordinating data and tool usage. A custom program performs the actual action and feeds the result back into the context.

Through this approach, AI has evolved into an agent that can reason, act, and correct its own mistakes.

This mechanism is not reserved for large corporations.

By combining a simple script that calls an API with your own business logic and functions, you can begin creating a capable digital colleague that performs parts of your work.

What kinds of hands and feet should we give AI, and what work should we assign to it?

The next major area of software development is shifting toward building custom agents.

Why not take your first step into agent development today?

Struggling to turn ideas into reality? With a proven track record of over 1,000 clients, our agile and flexible team will accelerate your business growth.

Book a Free Consultation
#Generative AI & ML

More on "Generative AI & ML"

A Concept for Developing AI Through Artificial Languages

A Concept for Developing AI Through Artificial Languages

Toshihiko Nagaoka07/11/2026

Can AI learn logic more efficiently through an artificial language than through natural language? This article compares Esperanto, Lojban, and Ithkuil, then presents a custom GPT-2 model trained from scratch on Lojban-based data that achieved 100% accuracy on prepared three-valued logic tests.

7 AI Image-to-3D Generators I Tested in 2026: Which One Is Actually Worth Using?

7 AI Image-to-3D Generators I Tested in 2026: Which One Is Actually Worth Using?

Thinh Tran07/10/2026

Using the same portrait and default settings, we tested Prism 3.1, Meshy 6, Tripo P1, Hunyuan3D, Forge, Trellis 2 and Rodin 2.5. See how they compare in facial similarity, texture, hair, clothing and mesh quality, plus which tools work best for realism, game assets and further editing.

Constellation Recognition: GNN + Symbolic AI — Developing a Hybrid Model

Constellation Recognition: GNN + Symbolic AI — Developing a Hybrid Model

Toshihiko Nagaoka06/30/2026

We developed a hybrid AI system that detects, separates, and reconstructs Orion, Cassiopeia, and the Big Dipper from noisy, rotated, and scaled point clouds. It combines geometric signatures, an edge-updating GNN, and symbolic graph-isomorphism constraints, reaching a test macro F1 score of 96.91%.

I'm Duper, ask me anything!