> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trylimitless.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Tool calling

> Let a model request a function that your application controls.

For a model that supports tools, send a function definition in `tools`. The model can return a request to call that function. Your application validates the arguments, runs an allowed operation, and sends the result in a subsequent completion request.

Limitless does not execute your application functions or grant the model access to your services.

```javascript theme={null}
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.LIMITLESS_API_KEY,
  baseURL: process.env.LIMITLESS_BASE_URL,
  maxRetries: 0,
});

const messages = [{ role: "user", content: "Is the blue notebook in stock?" }];
const tools = [{
  type: "function",
  function: {
    name: "check_stock",
    description: "Check a product's available quantity.",
    parameters: {
      type: "object",
      properties: { product: { type: "string" } },
      required: ["product"],
      additionalProperties: false,
    },
  },
}];

const first = await client.chat.completions.create({
  model: process.env.LIMITLESS_MODEL,
  messages,
  tools,
  tool_choice: "auto",
  max_tokens: 512,
});

const assistant = first.choices[0]?.message;
if (!assistant) throw new Error("No assistant message returned");
messages.push(assistant);

for (const call of assistant.tool_calls ?? []) {
  if (call.type !== "function" || call.function.name !== "check_stock") {
    throw new Error("Unexpected tool request");
  }
  const args = JSON.parse(call.function.arguments);
  if (typeof args.product !== "string" || args.product.length > 100) {
    throw new Error("Invalid tool arguments");
  }
  // Example fixture. Replace with your authorized inventory lookup.
  const stock = { product: args.product, quantity: 12 };
  messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(stock) });
}

if (assistant.tool_calls?.length) {
  const final = await client.chat.completions.create({
    model: process.env.LIMITLESS_MODEL,
    messages,
    max_tokens: 512,
  });
  console.log(final.choices[0]?.message.content);
} else {
  console.log(assistant.content);
}
```

Treat model-generated arguments as untrusted input. Enforce authorization in your application, allow only known functions, validate schemas, and bound the number of tool rounds. Each model call is a separate inference request.

Tool support, parallel tool calls, and schema strictness depend on the model. The gateway forwards supported request fields; it cannot make a model implement an unsupported feature.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.