# Tool calling

Define function tools, assemble streamed tool-call arguments, and return the result to the model.


Tool calling works on chat completions, Responses and Messages. Pass your functions in `tools`. The model answers with the call it wants to make. You run it, then send the result back.

Not every model supports tools. [GET /v1/model/info](https://inference.boundless.network/docs/api-compatibility) lists the capability flags for each model.

## Example

This example forces one tool call, assembles streamed arguments, returns the local result, then streams the final answer. The last chunk carries exact token counts.

```python
import json
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["BOUNDLESS_API_KEY"],
    base_url="https://api.inference.boundless.network/v1",
)
messages = [{"role": "user", "content": "What is 20 + 22? Use the add tool."}]
tools = [{
    "type": "function",
    "function": {
        "name": "add",
        "description": "Add two integers.",
        "parameters": {
            "type": "object",
            "properties": {
                "a": {"type": "integer"},
                "b": {"type": "integer"},
            },
            "required": ["a", "b"],
            "additionalProperties": False,
        },
    },
}]

stream = client.chat.completions.create(
    model="deepseek/deepseek-v4.1-flash",
    messages=messages,
    max_tokens=1024,
    tools=tools,
    tool_choice={"type": "function", "function": {"name": "add"}},
    stream=True,
    stream_options={"include_usage": True},
)

tool_call = {"id": "", "type": "function", "function": {"name": "", "arguments": ""}}
for chunk in stream:
    if chunk.usage:
        print(f"first request: {chunk.usage}")
    if not chunk.choices:
        continue
    for delta_call in chunk.choices[0].delta.tool_calls or []:
        if delta_call.id:
            tool_call["id"] = delta_call.id
        if delta_call.function.name:
            tool_call["function"]["name"] = delta_call.function.name
        if delta_call.function.arguments:
            tool_call["function"]["arguments"] += delta_call.function.arguments

arguments = json.loads(tool_call["function"]["arguments"])
messages.extend([
    {"role": "assistant", "tool_calls": [tool_call]},
    {
        "role": "tool",
        "tool_call_id": tool_call["id"],
        "content": json.dumps({"result": arguments["a"] + arguments["b"]}),
    },
])

stream = client.chat.completions.create(
    model="deepseek/deepseek-v4.1-flash",
    messages=messages,
    max_tokens=1024,
    tools=tools,
    stream=True,
    stream_options={"include_usage": True},
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print(f"\nfinal request: {chunk.usage}")
```

## How it works

- The model sends the call in `delta.tool_calls`. The `id` and the function `name` arrive first. The `arguments` string arrives in pieces. Join the pieces, then parse the JSON once the stream ends.
- Send the result back as a message with `role` set to `tool` and the same `tool_call_id`. Keep the assistant message that made the call in the history.
- `tool_choice` forces a call to one named function. Leave it unset to let the model decide.
- A reasoning model spends `max_tokens` on thinking too. See [Reasoning](https://inference.boundless.network/docs/reasoning).

## Other wire formats

`POST /v1/responses` takes function tools and `function_call_output`. `POST /v1/messages` uses Anthropic `tool_use` blocks. See [API Compatibility](https://inference.boundless.network/docs/api-compatibility).

## Gate tool calls with a fast model

A coding agent can ask Laya before it runs a tool. [System One](https://inference.boundless.network/docs/system-one#gate-tool-calls-in-claude-code) has a Claude Code hook that asks before a destructive command runs.
