# Tool use

> Let Antarya ask your code to run a function and use the result.

This is not available on this model yet. The model object's `capabilities.tools` turns true when it is.

## What it is for

Antarya cannot look up your calendar or open your database. With tools, you describe a function; when she needs it, the reply contains a request to call it. You run the function and send the result back, and she finishes the answer.

## The loop

1. Send the question with `tools`, a list of functions you describe.
2. If the reply has a `tool_call` part, run that function with its `input`.
3. Send the conversation again, with the reply added and a `tool` message holding the result.
4. Repeat until `stop_reason` is `end_turn`.

```bash
curl https://antarya.ai/v1/chat \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "Draft a reply that declines the meeting."}
    ],
    "tools": [
      {
        "name": "get_free_slots",
        "description": "Return the free calendar slots for a given week.",
        "input_schema": {
          "type": "object",
          "properties": {"week": {"type": "string"}},
          "required": ["week"]
        }
      }
    ],
    "tool_choice": {"type": "auto"}
  }'
```

```python
from antarya import Antarya

client = Antarya()

tools = [{
    "name": "get_free_slots",
    "description": "Return the free calendar slots for a given week.",
    "input_schema": {
        "type": "object",
        "properties": {"week": {"type": "string"}},
        "required": ["week"],
    },
}]

messages = [{"role": "user", "content": "Draft a reply that declines the meeting."}]
reply = client.chat.create(messages=messages, tools=tools, tool_choice={"type": "auto"})

for part in reply.content:
    if part.type == "tool_call":
        slots = "Tuesday 10:00, Wednesday 14:00"  # run your own function here
        messages.append({"role": "assistant", "content": reply.content})
        messages.append({
            "role": "tool",
            "content": [{"type": "tool_result", "tool_call_id": part.id, "content": slots}],
        })
        reply = client.chat.create(messages=messages, tools=tools)

print(reply.content[0].text)
```

```typescript
import Antarya from "antarya";

const client = new Antarya();

const tools = [{
  name: "get_free_slots",
  description: "Return the free calendar slots for a given week.",
  input_schema: {
    type: "object",
    properties: { week: { type: "string" } },
    required: ["week"],
  },
}];

const messages: any[] = [{ role: "user", content: "Draft a reply that declines the meeting." }];
let reply = await client.chat.create({ messages, tools, tool_choice: { type: "auto" } });

for (const part of reply.content) {
  if (part.type === "tool_call") {
    const slots = "Tuesday 10:00, Wednesday 14:00"; // run your own function here
    messages.push({ role: "assistant", content: reply.content });
    messages.push({
      role: "tool",
      content: [{ type: "tool_result", tool_call_id: part.id, content: slots }],
    });
    reply = await client.chat.create({ messages, tools });
  }
}

console.log(reply.content[0].text);
```

Output:

```
{
  "id": "req_06",
  "content": [
    {"type": "tool_call", "id": "call_01", "name": "get_free_slots", "input": {"week": "next"}}
  ],
  "stop_reason": "tool_call",
  "usage": {"input_tokens": 88, "output_tokens": 21}
}
```

The output shows the first reply, the one that asks for the function. After you send the result, the next reply has `stop_reason` of `end_turn` and holds the answer as text.

## Parameters

- `tools` (array, optional): The functions she may ask you to call.
  - `tools.name` (text, required): A short name for the function.
  - `tools.description` (text, required): What the function does and when to use it. She reads this to decide.
  - `tools.input_schema` (object, required): A JSON schema for the function's input.
- `tool_choice` (object, optional): How freely she may use tools. `auto` lets her decide and is the default, `none` stops her using tools, `any` makes her use one, and {"type": "tool", "name": "..."} makes her use that tool.

## Next steps

- [Structured output](https://antarya.ai/docs/guides/structured-output.md): Get a reply in a fixed JSON shape.
- [Conversations](https://antarya.ai/docs/guides/conversations.md): How the history you send back is built.
- [Errors](https://antarya.ai/docs/api.md): What to do when a request fails.
