FunkyRouter DocsBeta

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

HeaderValue
AuthorizationBearer <inference key>. Send one authorization header only.
Content-Typeapplication/json

Do not compress the request body. If you send a Content-Encoding header, the API returns HTTP 415.

Request fields

FieldTypeRules
modelstringRequired. Use cabeza-latest.
messagesarrayRequired. 1 to 64 messages.
streambooleanOptional. 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_optionsobjectOptional. {"include_usage": true} only. Send with "stream": true only.
max_completion_tokensintegerOptional. 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_tokensintegerOptional. Same as max_completion_tokens. Do not send both.
temperaturenumberOptional. 0 to 2.
top_pnumberOptional. 0 to 1.
toolsarrayOptional. See Tool calling.
tool_choicestringOptional. "auto" or "none". Send with tools only.
parallel_tool_callsbooleanOptional. 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_key or a penalty field.

The 400 response does not identify the field. Compare your request with the table.

Messages

KeyRules
rolesystem, user, assistant or tool. The developer role is not accepted.
contentA 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_callsAssistant messages only. The calls from an earlier response.
tool_call_idTool 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. A file_id also gives HTTP 400. The API does not download files.
  • Only user messages can contain images. The model answers with text.
  • The API accepts detail (auto, low or high), 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 parameters with "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:

  1. Add the assistant message with its tool_calls to messages.
  2. Run each tool.
  3. Add one tool message for each call, with the same tool_call_id.
  4. 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

FieldContent
idchatcmpl- and 32 hexadecimal characters
modelThe model ID that you sent
choices[0].messageThe assistant message, with content or tool_calls
choices[0].finish_reasonstop, length or tool_calls
usageprompt_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

On this page