Examples
JavaScript tool-calling agent
A Node.js agent loop using the OpenAI SDK with Orqen as the base URL.
agent.mjs
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ORQEN_API_KEY,
baseURL: "https://api.orqen.app/v1",
});
const tools = [
{
type: "function",
function: {
name: "list_workspace_files",
description: "List files in the workspace. Use when the user asks to list files, folders, or directory contents.",
parameters: {
type: "object",
properties: { subdir: { type: "string", default: "" } },
required: [],
},
},
},
{
type: "function",
function: {
name: "get_weather",
description: "Get current weather for a city. Use when the user asks about weather, forecast, rain, or temperature.",
"x-orqen-examples": ["weather in London", "is it raining in Paris"],
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
},
];
async function callTool(name, args) {
if (name === "list_workspace_files") {
return { entries: ["README.md", "src", "package.json"] };
}
if (name === "get_weather") {
return { city: args.city, temperature_c: 13, conditions: "light rain" };
}
return { error: "Unknown tool" };
}
async function run(userMessage) {
const messages = [{ role: "user", content: userMessage }];
for (let i = 0; i < 8; i++) {
const response = await client.chat.completions.create({
model: "gpt-4o",
messages,
tools,
tool_choice: "auto",
});
const message = response.choices[0].message;
if (!message.tool_calls?.length) return message.content ?? "";
messages.push(message);
for (const call of message.tool_calls) {
const args = JSON.parse(call.function.arguments || "{}");
const result = await callTool(call.function.name, args);
messages.push({
role: "tool",
tool_call_id: call.id,
content: JSON.stringify(result),
});
}
}
return "Stopped after max tool rounds.";
}
console.log(await run("list files"));Streaming
const stream = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Explain tool pruning" }],
tools,
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}