nsfwllms.comGuide
Function Calling With LLMs: How It Works
Function calling is a way for a language model to hand work back to your program. The model does not run anything; it returns the name of a function and a set of arguments, and your code decides what happens next.
Updated
Key points
- The model proposes a call as structured JSON. Your program executes it.
- In the OpenAI format the field is tools, and calls come back in tool_calls.
- Good schemas and strict validation matter more than clever prompts.
- It works on this API with model
uncensored, in both normal and streamed responses.

What function calling is, and what it is not
Function calling with LLMs solves a specific problem: a model only produces text, but real applications need to look up records, do arithmetic, hit an API or update a database. Function calling bridges the two. You describe your functions, the model picks one when it fits the conversation, and it writes the arguments in JSON that matches your description.
Three misunderstandings are common:
- The model does not execute code. It cannot reach your database or the internet unless your program does that on its behalf.
- It is not a separate model. It is a behavior of a chat model that was trained to emit structured calls.
- It is not a guarantee of correct arguments. The output is a prediction, so you check it like any other input.
The older OpenAI functions parameter has been replaced by tools, and that is the form this API accepts, together with tool_choice. If you find a tutorial that uses function_call, translate it to the newer fields.
The sequence of messages
It helps to see the conversation as a list of messages with different roles.
| Step | Role | Content |
|---|---|---|
| 1 | user | "What does my character carry?" |
| 2 | assistant | No text, one tool_calls entry: get_inventory with a character name |
| 3 | tool | JSON from your code, tied to the call by tool_call_id |
| 4 | assistant | A normal answer that uses the data |
Steps 2 and 4 are model output. Steps 1 and 3 are yours. Each trip is an ordinary request to POST /v1/chat/completions, which means each one is billed as normal input and output tokens at $0.15 and $0.50 per 1M. The tool definitions count as input too, so ten verbose schemas add cost to every call. Keep them short.
Writing a schema the model can use
The schema is the only documentation the model gets, so it deserves care.
Names and descriptions
Pick verbs that say what happens: get_inventory, save_memory, roll_dice. In the description, state when to use the function and when not to. A line such as "Use this only when the user asks about items; do not guess the contents" prevents invented answers.
Parameters
Give every property a type and a one-line description. Use enum for fixed options, set required, and add minimum and maximum values for numbers. The tighter the schema, the fewer repair steps you need later.
How many functions
Start with a handful. As the list grows, models choose less reliably and your prompt grows. If you need dozens, group them by task and send only the relevant group for each request.
A complete example in Node
This script handles one question with one function. It uses the official openai package, pointed at this API.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.nsfwllms.com/v1",
apiKey: process.env.API_KEY,
});
const inventories = { mira: ["lantern", "rope", "silver key"] };
function getInventory({ character }) {
return { items: inventories[character.toLowerCase()] ?? [] };
}
const tools = [{
type: "function",
function: {
name: "get_inventory",
description: "List the items a named character carries. Use only when the user asks about items.",
parameters: {
type: "object",
properties: { character: { type: "string", description: "Character name" } },
required: ["character"],
},
},
}];
const messages = [{ role: "user", content: "What is Mira carrying?" }];
const first = await client.chat.completions.create({
model: "uncensored",
messages,
tools,
});
const msg = first.choices[0].message;
if (msg.tool_calls) {
messages.push(msg);
for (const call of msg.tool_calls) {
const args = JSON.parse(call.function.arguments);
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(getInventory(args)),
});
}
const final = await client.chat.completions.create({
model: "uncensored",
messages,
tools,
});
console.log(final.choices[0].message.content);
} else {
console.log(msg.content);
}
Notice the loop over msg.tool_calls. It is a list, so write your handler to accept more than one entry, even if most replies carry a single call.
Function calling, JSON mode or plain prompting
These three techniques overlap, and the right pick depends on what you need.
- Function calling fits when the model should trigger an action or fetch data, and you want the choice of action to be the model's.
- JSON mode (
response_formatset to{"type": "json_object"}) fits when the final answer itself must be a JSON object, for example a character profile your app will store. No function runs. - Plain prompting with a format instruction is the cheapest option for one-off extraction, but you parse free text and handle drift yourself.
You can combine them across steps: use tools while gathering facts, then switch to JSON mode for the last response.
Safety rules for your own code
Because the arguments come from a model, treat each call as untrusted. A few rules keep things safe:
- Allowlist function names. If the name is not in your table, return an error message as the tool result.
- Validate arguments against the same schema you published, and catch JSON parse errors.
- Run read-only functions freely. Put a human confirmation in front of anything that writes, deletes or spends.
- Limit the number of rounds per user turn, and log every call with its
X-Request-Idresponse header so you can trace problems. - Never pass model output into a shell, a SQL string or an eval call.
Prompts sent to this API are not used for training, but that does not change what your own tools expose. Only give the model functions you would be comfortable having it call with any plausible input.
Try it
The quickstart documentation shows authentication and the base request, and the pricing page lists top-up amounts and bonuses. After signing up you can claim $0.50 of trial credit on the API key page, valid for 7 days and one per person. Trial keys allow 2 concurrent requests and 60 per minute, which is plenty for testing a tool loop.
More guides
Questions and answers
What is function calling in simple terms?
The model returns the name of a function and JSON arguments instead of plain text. Your program runs the function and sends back the result, and the model then writes the final answer.
Is function calling the same as tool calling?
In practice yes. The OpenAI format now calls the feature tools, and the older functions field has been replaced. On this API you use tools and tool_choice.
Can the model call several functions at once?
The reply contains a list called tool_calls, so write your code to handle more than one entry. Return one tool message for each call id.
Does it work with streaming?
Yes. Tool call arguments arrive in fragments, and you join them by index and parse them after the stream ends.