---
title: "What Is Function Calling? A Full Round Trip With a Minimal Example"
description: "Function calling means the model proposes a call and your code runs it. See the tools definition, the returned tool_calls, and the result you send back."
url: https://cdnping.org/en/blog/what-is-function-calling
language: en
published: 2026-11-25
category: "AI concepts"
tags: ["Function calling","Tool use","tools parameter","LLM API","Beginner guide"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# What Is Function Calling? A Full Round Trip With a Minimal Example

Function calling (also called tool use) is a mechanism where you tell the model in the request which functions are available. The model doesn't run anything itself. It only returns something like "I want to call this function with these arguments." Your code does the actual execution, hands the result back to the model, and the model turns it into the final answer. In one sentence: the model decides what to call and what arguments to fill in, and you do the executing.

Below we walk through one complete round trip. The examples use the OpenAI-compatible format. Anthropic's format uses different field names but the same idea; check each vendor's official docs for details.

## Can a model really "call a function itself"?

No. A model can only output text, including structured JSON text. It has no way to reach your database, your weather API, or the files on your machine.

A "call" is the model writing a response in an agreed format that says, roughly, "please call `get_weather` with `city` set to `北京`" (Beijing). Your program reads that, calls the real function, and sends the return value back as a new message. Only after seeing the result does the model write the final reply to the user.

Two things follow from this. First, the safety boundary is in your hands: your code decides what runs and what doesn't. Second, the model can fill in wrong arguments, so validate them instead of trusting them blindly.

## What does one full round trip look like?

Three steps, two requests.

### Step 1: Send the tools definition with the request

The `tools` parameter is an array. Each item describes one function: its name, a description of what it does, and a JSON Schema for its parameters.

```json
{
  "model": "<model name>",
  "messages": [
    {"role": "user", "content": "北京今天天气怎么样？"}
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {"type": "string", "description": "City name"}
          },
          "required": ["city"]
        }
      }
    }
  ]
}
```

The clearer the `description`, the easier it is for the model to decide when to use the function. The contents of `tools` count as input tokens, so more functions mean higher cost; see [What is a token in an LLM](/en/blog/what-is-token-in-llm) for background. (The user message above is Chinese for "What's the weather like in Beijing today?")

### Step 2: The model returns a request to call

If the model decides it needs the weather, the assistant message it returns isn't plain text. It contains `tool_calls`:

```json
{
  "role": "assistant",
  "content": null,
  "tool_calls": [
    {
      "id": "call_001",
      "type": "function",
      "function": {
        "name": "get_weather",
        "arguments": "{\"city\": \"北京\"}"
      }
    }
  ]
}
```

Note that `arguments` is a JSON string, not an object, so you have to parse it yourself. Keep the `id`; you need it in the next step.

### Step 3: You run the function and send back the result

Your code parses the arguments, calls the real weather API, and then sends the whole conversation again: the original messages, plus the assistant message above, plus a message with `role` set to `tool`.

```json
{
  "role": "tool",
  "tool_call_id": "call_001",
  "content": "{\"temp_c\": 18, \"weather\": \"晴\"}"
}
```

(`晴` means "sunny".) `tool_call_id` must match the `id` from step 2. Once the model receives this, it generates a final answer along the lines of "It's sunny in Beijing today, 18 degrees Celsius." If it needs to call more tools, it returns `tool_calls` again and you repeat the process. That is the loop.

## What details do you need to handle in code?

This checklist is handy the first time you build it:

| Area | What to do |
| --- | --- |
| Defining functions | Use English names with underscores; write a description that states the purpose and when to use it; put required parameters in `required` |
| Checking the response | Look for `tool_calls` first; if absent, it's a plain text answer |
| Parsing arguments | `arguments` is a string; parse it as JSON and catch parse failures |
| Validating arguments | Never splice arguments directly into SQL or shell commands |
| Returning results | Include the matching `tool_call_id`; use a string for the content |
| Controlling the loop | Set a maximum number of rounds so the model can't call tools forever |
| Parallel calls | One response may contain several `tool_calls`; return each one |

Whether a model supports function calling, and parallel calls in particular, varies by model and vendor. Check the relevant official docs.

## How do function calling, MCP, and agents relate?

Function calling is the lowest layer: it defines how the model expresses what it wants to call.

[MCP (Model Context Protocol)](/en/blog/what-is-mcp-model-context-protocol) solves a different problem: who provides the tools, and how different clients can plug them in in a uniform way. In the end, the client still has to convert those tools into tool definitions the model understands.

An [AI agent](/en/blog/what-is-ai-agent-coding) adds a loop on top of this mechanism: the model repeatedly decides to call a tool, reads the result, and decides the next step until the task is done. Coding tools like Claude Code and Codex reading files and running commands are built on exactly this loop.

## What do you need to try it yourself?

Very little: an endpoint you can call, an API key, and the code for the three steps above. If those concepts are new, start with [What is an API key](/en/blog/what-is-api-key-beginner) and [What is a base URL](/en/blog/what-is-base-url-api-endpoint). To get the simplest request working first, see [How to use an LLM API](/en/blog/llm-api-first-request-guide).

Also, if you call overseas endpoints from mainland China, latency and timeouts slow down multi-round trips, because every tool call adds another request. You can use cdnping's [API status](/en/status) page to see probe results for each vendor's official endpoints, run the [speed test on the home page](/en) to measure connection time from your own network to each endpoint, and read [AI API latency](/en/learn/ai-api-latency-china) for more background.

### No overseas credit card? How do you get API access?

Payment methods and supported regions for official APIs differ by vendor. Follow the official docs and terms of service, and comply with those terms and local laws.

If you'd like to pay in RMB, you can look at AGI Plan. It is a multi-vendor LLM aggregation gateway sold as a monthly subscription, paid in RMB, with no overseas credit card needed. One subscription lets you switch between several capability groups, such as frontier reasoning, balanced general-purpose, Chinese flagship models, and fast lightweight models. It is compatible with both the OpenAI and Anthropic API formats, so the examples above work by swapping the address and key. Tools like Claude Code and Codex only need one endpoint changed. Some models are available only outside mainland China; see the [models page](https://agiplan.dev/models) for specifics. The service also states that it is not an officially authorized channel. AGI Plan is a commercial partner of cdnping. For setup steps, see [agiplan.dev/startup](https://agiplan.dev/startup).

## What should you do next?

1. Using the JSON above, build a request with `tools` by hand and confirm you get back `tool_calls`.
2. Write a small real function (for example, one that reads a local text file), and use code to make the second request and get the final answer.
3. Add argument validation and a maximum loop count, then consider adding more functions.
4. To estimate the cost of multi-round calls, see [How to calculate LLM API cost](/en/blog/llm-api-cost-estimation-method).
