Inference
Tools
Use OpenAI-compatible function tools with chat completions.
Updated
Tool calling follows the OpenAI chat completions format. Send function
definitions in tools. When the model returns finish_reason: "tool_calls",
execute the requested tool in your application, then send the result back as a
tool message with the same tool_call_id.
Choose a model whose metadata includes supported_features: ["tools"] before
sending tool definitions.
Request fields
| Field | Type | Description |
|---|---|---|
tools | array | Function tool definitions in OpenAI-compatible format |
tool_choice | string | object | auto, none, required, or a specific function tool |
Example
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.llmbase.ai/v1",
apiKey: process.env.LLMBASE_API_KEY,
});
const weatherTool = {
type: "function",
function: {
name: "get_current_weather",
parameters: {
type: "object",
properties: {
city: { type: "string" },
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["city", "unit"],
},
},
} as const;
const first = await client.chat.completions.create({
model: "deepseek/deepseek-v4-flash",
messages: [{ role: "user", content: "What is the weather in Berlin?" }],
tools: [weatherTool],
});
const toolCall = first.choices[0]?.message.tool_calls?.[0];
if (toolCall) {
const final = await client.chat.completions.create({
model: "deepseek/deepseek-v4-flash",
messages: [
{ role: "user", content: "What is the weather in Berlin?" },
first.choices[0].message,
{
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify({ city: "Berlin", temperature: 14, unit: "celsius" }),
},
],
tools: [weatherTool],
});
console.log(final.choices[0]?.message.content);
}
Tool result messages
Tool results are normal chat messages with:
| Field | Value |
|---|---|
role | tool |
tool_call_id | The ID from the model’s requested tool call |
content | String content, usually JSON serialized by your application |
Keep tool result payloads compact. Long repeated tool context can increase cost;
for repeated agent loops, combine tools with
prompt_cache_key.