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

# Tool calling

> Give Prism models functions and return their results to the conversation.

Tool calling lets a model request application-owned functions. Your code
executes each request, validates the arguments, and sends the result back to
the model.

## OpenAI tool loop

Define tools in the Chat Completions format:

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.PRISM_API_KEY,
  baseURL: "https://api.prisminference.com/v1",
});

const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
  { role: "user", content: "What is the weather in San Francisco?" },
];

const first = await client.chat.completions.create({
  model: "prism-glm53",
  messages,
  tools: [
    {
      type: "function",
      function: {
        name: "get_weather",
        description: "Get the current weather for a city",
        parameters: {
          type: "object",
          properties: {
            city: { type: "string" },
          },
          required: ["city"],
          additionalProperties: false,
        },
      },
    },
  ],
});

const assistant = first.choices[0].message;
messages.push(assistant);

for (const call of assistant.tool_calls ?? []) {
  const args = JSON.parse(call.function.arguments) as { city: string };
  const result = await getWeather(args.city);

  messages.push({
    role: "tool",
    tool_call_id: call.id,
    content: JSON.stringify(result),
  });
}

const final = await client.chat.completions.create({
  model: "prism-glm53",
  messages,
});
```

When the model requests a tool, `finish_reason` is `tool_calls`. Preserve the
assistant message and return one `tool` message for every requested call.

## Anthropic tool loop

Anthropic uses `tool_use` and `tool_result` content blocks:

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.PRISM_API_KEY,
  baseURL: "https://api.prisminference.com",
});

const first = await client.messages.create({
  model: "prism-glm53",
  max_tokens: 1024,
  messages: [
    { role: "user", content: "What is the weather in San Francisco?" },
  ],
  tools: [
    {
      name: "get_weather",
      description: "Get the current weather for a city",
      input_schema: {
        type: "object",
        properties: { city: { type: "string" } },
        required: ["city"],
      },
    },
  ],
});

const call = first.content.find((block) => block.type === "tool_use");

if (call?.type === "tool_use") {
  const result = await getWeather((call.input as { city: string }).city);

  const final = await client.messages.create({
    model: "prism-glm53",
    max_tokens: 1024,
    messages: [
      { role: "user", content: "What is the weather in San Francisco?" },
      { role: "assistant", content: first.content },
      {
        role: "user",
        content: [
          {
            type: "tool_result",
            tool_use_id: call.id,
            content: JSON.stringify(result),
          },
        ],
      },
    ],
    tools: [
      {
        name: "get_weather",
        description: "Get the current weather for a city",
        input_schema: {
          type: "object",
          properties: { city: { type: "string" } },
          required: ["city"],
        },
      },
    ],
  });
}
```

## Production rules

* Treat model-generated arguments as untrusted input.
* Validate arguments against the tool schema before execution.
* Apply authentication and authorization in the tool implementation.
* Set timeouts and output-size limits.
* Require confirmation before destructive or high-impact actions.
* Return concise, structured errors so the model can recover.
* Execute calls in parallel only when they are independent.

Tool schemas follow the selected endpoint. Use OpenAI function definitions on
`/v1/chat/completions` and Anthropic tools on `/v1/messages`.
