Chat completions
Request fields, streaming and tool calling for POST /v1/chat/completions.
POST https://api.funkyrouter.ai/v1/chat/completions
The endpoint uses the OpenAI Chat Completions format. The API accepts only the fields on this page. For all other fields, it returns HTTP 400.
The context window is 1,048,576 tokens. The prompt and the output share it. The request body can be 8 MiB. See Token limits.
Request headers
| Header | Value |
|---|---|
Authorization | Bearer <inference key>. Send one authorization header only. |
Content-Type | application/json |
Do not compress the request body. If you send a Content-Encoding header, the API returns HTTP 415.
Request fields
| Field | Type | Rules |
|---|---|---|
model | string | Required. Use cabeza-latest. |
messages | array | Required. 1 to 64 messages. |
stream | boolean | Optional. Default false. Set to true when the prompt is more than 131,072 tokens. If not, the API returns HTTP 400. Also set it for long answers. |
stream_options | object | Optional. {"include_usage": true} only. Send with "stream": true only. |
max_completion_tokens | integer | Optional. No default. 1 to the context window, less the prompt. If you do not set it, the request can use the remainder of the window. The gateway then reserves credit for all of it. |
max_tokens | integer | Optional. Same as max_completion_tokens. Do not send both. |
temperature | number | Optional. 0 to 2. |
top_p | number | Optional. 0 to 1. |
tools | array | Optional. See Tool calling. |
tool_choice | string | Optional. "auto" or "none". Send with tools only. |
parallel_tool_calls | boolean | Optional. Send with tools only. |
The API also returns HTTP 400 when:
- A field has the value
null. - The body has the same key two times.
- The body has a field that is not in the table, for example
stop,n,seed,user,store,logprobs,response_format,reasoning_effort,prompt_cache_keyor a penalty field.
The 400 response does not identify the field. Compare your request with the table.
Messages
| Key | Rules |
|---|---|
role | system, user, assistant or tool. The developer role is not accepted. |
content | A string, or an array of parts. Use text parts in all messages. Use image_url parts in user messages only. See Images. Other part types are not accepted. |
tool_calls | Assistant messages only. The calls from an earlier response. |
tool_call_id | Tool messages only. The ID of the call that the result answers. |
Do not send other message keys, for example name.
Images
A user message can contain images and text. Send each image as an image_url part with a base64 data URL:
{
"role": "user",
"content": [
{ "type": "text", "text": "What is in this image?" },
{
"type": "image_url",
"image_url": { "url": "data:image/png;base64,iVBORw0KGgo…" }
}
]
}- The image must be a PNG, JPEG, GIF or WebP file in a base64 data URL.
- Each side of the image must be 40 to 4,096 pixels.
- A remote URL, for example
https://…, gives HTTP 400. Afile_idalso gives HTTP 400. The API does not download files. - Only
usermessages can contain images. The model answers with text. - The API accepts
detail(auto,loworhigh), but ignores it. The model reads the image at its own resolution. - Base64 makes a file approximately one third larger. The full request body must be 8 MiB or less.
An image costs one input token for each block of 40 by 40 pixels. Each side is rounded up to the next full block. For example, a 640 × 480 image costs 192 tokens. A 4,096 × 4,096 image costs 10,609 tokens. Image tokens are billed as input tokens.
import base64, os
from openai import OpenAI
client = OpenAI(
base_url="https://api.funkyrouter.ai/v1",
api_key=os.environ["FUNKYROUTER_API_KEY"],
)
with open("photo.png", "rb") as f:
image = base64.b64encode(f.read()).decode()
completion = client.chat.completions.create(
model="cabeza-latest",
max_completion_tokens=1024,
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image}"}},
],
}],
)
print(completion.choices[0].message.content)Streaming
Set "stream": true to get server-sent events. Each event is a data: line with a chat.completion.chunk object. The stream ends with data: [DONE].
To get token usage, add "stream_options": {"include_usage": true}. The last chunk before [DONE] then has an empty choices array and a usage object.
curl https://api.funkyrouter.ai/v1/chat/completions \
--header "Authorization: Bearer $FUNKYROUTER_API_KEY" \
--header "Content-Type: application/json" \
--no-buffer \
--data '{
"model": "cabeza-latest",
"stream": true,
"stream_options": { "include_usage": true },
"messages": [{ "role": "user", "content": "Count to five." }]
}'Tool calling
Tool calling uses the OpenAI tools format. Each tool must:
- Have
"type": "function". - Have a unique name with 1 to 64 characters: letters, digits,
_or-. - Have
parameterswith"type": "object". - Not set
"strict": true.
You can send up to 64 tools.
When the model calls a tool, the response has finish_reason: "tool_calls" and a tool_calls array in the assistant message. In a stream, all calls arrive together in the last chunk. They do not arrive as partial deltas.
To continue the conversation:
- Add the assistant message with its
tool_callstomessages. - Run each tool.
- Add one
toolmessage for each call, with the sametool_call_id. - Send the request again.
An assistant message can have up to 8 tool calls. Each call must have a result before the next request.
Tool calling is in beta
A call can arrive as text in content instead of in tool_calls. Make sure that your client can handle this. Always examine the arguments before you run a tool.
{
"model": "cabeza-latest",
"messages": [
{ "role": "user", "content": "What is the weather in Fort Worth?" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}Response
| Field | Content |
|---|---|
id | chatcmpl- and 32 hexadecimal characters |
model | The model ID that you sent |
choices[0].message | The assistant message, with content or tool_calls |
choices[0].finish_reason | stop, length or tool_calls |
usage | prompt_tokens, completion_tokens, total_tokens |
The API can add new response fields at any time. Make sure that your client ignores fields that it does not know.
Last updated on