Tool calling
Let a model call your functions: define tools, run what it asks for, send the results back.
With tool calling (also called function calling) a model can ask your code to do something it cannot do itself, such as look up an order or read the weather. You describe your functions in the request. The model answers with the function to call and its arguments. You run it and send the result back, and the model writes the final answer. eWest never runs your functions: the model only asks, and your code does the work.
Which models
A chat model supports tools when supports_tools is true in GET /v1/models/{id} (the Tool use row in model comparisons). Every chat model in the catalog sets it today. Tools work on chat completions, on POST /v1/messages and on POST /v1/responses, and with streaming.
The loop
- Send your
messagesand atoolslist. Each tool is{"type": "function", "function": {"name", "description", "parameters"}}, whereparametersis a JSON Schema for the arguments. Good descriptions matter: the model chooses tools by them. - If the model wants a tool, the answer has
finish_reason: "tool_calls"andmessage.tool_calls. Each call has anid,function.nameandfunction.arguments. - Run the function.
argumentsis a JSON string: parse it, and expect that it can be invalid or miss a field, because the model writes it. - Add the assistant message to
messagesexactly as it came back, then one message per call withrole: "tool", the call'stool_call_idand your result as a string. - Send the request again, with the same
tools. The model either answers in text (finish_reason: "stop") or asks for more tools. Repeat, and put a limit on the number of rounds.
# 1. Describe the tools. The answer has finish_reason "tool_calls" and a tool_calls list.
curl https://api.ewest.ai/v1/chat/completions \
-H "Authorization: Bearer $EWEST_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "ewest/gemini-flash",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "What is the weather in Paris?"}],
"tools": [{"type": "function", "function": {
"name": "get_weather",
"description": "Current weather for a city.",
"parameters": {"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]}}}]}'
# 2. Run the function yourself, then send the conversation back: your first message, the
# assistant message as it came back (with its tool_calls), and one "tool" message per call.
# Put the id from step 1 where this example says CALL_ID.
curl https://api.ewest.ai/v1/chat/completions \
-H "Authorization: Bearer $EWEST_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "ewest/gemini-flash",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "What is the weather in Paris?"},
{"role": "assistant", "content": null, "tool_calls": [
{"id": "CALL_ID", "type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\": \"Paris\"}"}}]},
{"role": "tool", "tool_call_id": "CALL_ID", "content": "{\"temp_c\": 18, \"sky\": \"sunny\"}"}
],
"tools": [{"type": "function", "function": {
"name": "get_weather",
"description": "Current weather for a city.",
"parameters": {"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]}}}]}'Choosing when a tool is used
tool_choice controls it:
"auto"(the default): the model decides."none": the model does not call a tool. Thetoolsstill count as part of the prompt."required": the model must call at least one tool.{"type": "function", "function": {"name": "get_weather"}}: the model must call that tool. A name that is not intoolsis a400invalid_request_error.
Several calls at once
A model can ask for more than one tool in the same answer, for example the weather in two cities. Answer every call, each with its own tool_call_id, before you send the request again. You can run them at the same time. "parallel_tool_calls": false asks the model for at most one call per answer. Treat the id as an opaque string: send it back unchanged, and do not parse it or rely on its length or format.
Tool calls while streaming
With "stream": true the calls arrive as delta.tool_calls (Streaming). Each entry has an index. The first chunk for an index carries the call's id and function.name; the later ones carry pieces of function.arguments. Join the pieces for each index, and parse the result only when the stream has ended with finish_reason: "tool_calls".
Getting good results
- Give models that think before they answer room in
max_tokens. With a small limit the model can use it all on thinking and return nothing or a cut-off call. 1024 is a good start; use more if you setreasoning_effort, and if the limit is too small for it the400names the minimum. - When a function fails, send the error back as the tool's result instead of stopping. The model can usually correct the arguments.
- Other formats: on
POST /v1/messagesa tool hasname,descriptionandinput_schema, the answer hastool_useblocks andstop_reason: "tool_use", and you reply with atool_resultblock in the next user message. OnPOST /v1/responsesthe answer hasfunction_callitems that you answer withfunction_call_outputitems, as in the OpenAI Responses API. - Coding tools use all of this for you; see Developer tools. For schema-shaped answers without tools, see Structured output.