Tool calling
Let a compatible model request a tool while your application validates and executes the action.
A model returns a requested tool name and JSON arguments. Your application validates and authorizes the action, executes it, and sends the result back. This is inference orchestration; it does not modify the model's weights.
Example: look up a demo order
Install openai. Use an authorized API key and a model ID whose current
capabilities include tools. The order below is fictional in-process demo data;
replace the lookup with an account-scoped, authorized application operation.
js
import OpenAI from "openai";
const apiKey = process.env.OPENPOND_API_KEY;
const model = process.env.OPENPOND_MODEL;
if (!apiKey || !model) throw new Error("Set OPENPOND_API_KEY and OPENPOND_MODEL");
const client = new OpenAI({ apiKey, baseURL: "https://api.openpond.ai/opchat/v1" });
const tools = [{
type: "function",
function: {
name: "lookup_order",
description: "Read the shipping status of a demo order.",
parameters: {
type: "object",
properties: { order_id: { type: "string" } },
required: ["order_id"],
additionalProperties: false,
},
},
}];
const messages = [{ role: "user", content: "Where is demo order DEMO-42?" }];
const first = await client.chat.completions.create({ model, messages, tools });
const message = first.choices[0]?.message;
if (!message) throw new Error("No assistant message");
if (!message.tool_calls?.length) {
console.log(message.content);
} else {
if (message.tool_calls.length !== 1) throw new Error("This demo handles one call");
const call = message.tool_calls[0];
if (call.type !== "function" || call.function.name !== "lookup_order")
throw new Error("Unsupported tool");
const args = JSON.parse(call.function.arguments);
if (!args || typeof args !== "object" || Array.isArray(args) ||
Object.keys(args).length !== 1 || typeof args.order_id !== "string")
throw new Error("Invalid order arguments");
const result = args.order_id === "DEMO-42"
? { order_id: args.order_id, status: "shipped" }
: { error: "not_found" };
const final = await client.chat.completions.create({
model,
messages: [...messages, message, {
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
}],
});
console.log(final.choices[0]?.message.content);
}Illustrative exchange; model wording and whether it requests a tool vary:
text
Requested tool: lookup_order({"order_id":"DEMO-42"})
Tool result: {"order_id":"DEMO-42","status":"shipped"}
Final answer: Demo order DEMO-42 has shipped.- Model-generated arguments are untrusted, even when a schema was supplied.
- Match the original call ID in the tool result.
- Keep authentication, tenant boundaries, spending and required approvals in your application. A model request does not authorize an action.
- Bound tool loops and retries. Do not execute duplicate side effects after a lost reply; retain the application's original action identity.
Sources
- OpenAI JavaScript SDK: compatible client and tool-message types.
- OpenPond Inference: endpoint and model capability discovery.