Skip to main content
string
required
The API key for your account. You can find this in your account settings.
Requires an API key with the ai:chat scope. The AI Gateway lets you use Square Cloud’s hosted AI model inside your own products (chatbots, assistants, automations) through an OpenAI-compatible chat completions endpoint. If your code already speaks the OpenAI API, it speaks the AI Gateway too: point the SDK at our base URL, use your account API key, and set the model to cubic.
The AI Gateway comes with the Standard, Pro and Enterprise plans: usage within your plan’s limits has no extra charge. Everything is billed in Brazilian reais, with an invoice issued in Brazil.

How to connect

  • Base URL: https://api.squarecloud.app/v2/ai
  • API key: your account API key (the same one used by the Square Cloud API and CLI, from the account page). The Authorization header is accepted with or without the Bearer prefix.
  • Model: cubic, Square Cloud’s hosted model, the same one powering the dashboard AI assistant.

Plans and limits

Every request bills its real tokens against the gateway’s own token limits: a daily limit, reset at 00:00 UTC, and a weekly limit of 4x the daily one, reset on Monday at 00:00 UTC. They are separate from the dashboard AI assistant’s limits, so gateway traffic never uses up the assistant’s allowance, and vice versa. Every plan also has a daily fair-use allowance on the gateway, sized for moderate use (a guard for unattended keys, based on what the requests cost to serve), reset at 00:00 UTC. When a token limit runs out, the account’s AI credits keep requests going until they are spent: prepaid token packs bought in the dashboard, valid for 90 days and shared with the dashboard AI assistant, used only after a limit is reached. The fair-use allowance still applies. Every Standard plan size has the same gateway limits, and so does every Pro size. Enterprise token limits grow with the plan: 10M tokens per day on Enterprise 32, 12M on Enterprise 48, 14M on Enterprise 64, 16M on Enterprise 96 and 20M on Enterprise 128 and above, with 4x that per week.
Hobby plans (and accounts without a plan) do not have AI Gateway access: upgrading to Standard or above enables it.

Pay-as-you-go

When the plan’s token limits and the AI credits are not enough, Enterprise accounts can turn on pay-as-you-go on the dashboard’s usage page, once they have bought at least one AI credit pack. You choose a spending limit from R$ 10 to R$ 1,000, counted over the last 30 days, and you can change it or turn pay-as-you-go off at any time.
  • Order: requests use the plan’s token limits first, then the AI credits, then pay-as-you-go.
  • Price: R$ 1.00 per 1 million tokens, up to your limit.
  • Limit: once the last 30 days reach it, requests are refused with postpaid_cap_reached until the oldest days leave the count or you raise the limit.
  • Billing: it is billed with the account’s other usage on the plan’s cycle, on the plan renewal invoice or on a separate usage invoice, with a minimum of R$ 10 on the usage total (see Blob Storage overage).
  • Unpaid invoice: if a usage invoice is still unpaid after 7 days, pay-as-you-go is paused until it is paid. The plan’s limits and the AI credits keep working.
Pay-as-you-go works only on the Enterprise plan: below it, it stays off, even if it was turned on before a plan change.

Usage in the dashboard

The AI Gateway page of the dashboard lists the account’s requests one by one (chat completions and decisions), with their tokens, what covered each one (the plan, AI credits or pay-as-you-go), status and duration, next to a chart of the last 7 days of usage. On Enterprise, it also shows pay-as-you-go: whether it is on and how much of your limit the last 30 days used. The request list covers the last 48 hours on the Standard plan and the last 7 days on Pro and Enterprise.

Request contract

string
Accepted for SDK compatibility; the gateway always answers as cubic.
array
required
OpenAI-style messages with roles system, user, assistant and tool. Content must be a string: this endpoint is text only, so a multi-part array (image_url, input_audio, file) is rejected with 400 multimodal_not_supported. Up to 500 messages per request.
number
OpenAI’s current name for the answer’s token limit, the one the OpenAI SDKs send. It wins over max_tokens when both are present. Silently clamped to your plan’s output ceiling.
number
The legacy name, still accepted. Silently clamped to your plan’s output ceiling.
number
From 0 to 2.
array
Function calling in the standard OpenAI format, up to 32 tool definitions. Tool calls come back as finish_reason: "tool_calls", and you send results back as role: "tool" messages.
string | object
Standard OpenAI tool_choice values.
boolean
true sends the answer as it is written, as Server-Sent Events in the OpenAI chat.completion.chunk format (see Streaming). Default false.
object
With stream, { "include_usage": true } adds one last chunk with the request’s usage before data: [DONE].
string
Lets the model think before answering: none (default, answers at once), minimal, low, medium or high. Higher levels allow a longer reasoning, which takes more time and more tokens. The reasoning counts inside the answer’s token limit and in completion_tokens (its share is in usage.completion_tokens_details.reasoning_tokens); its text is not returned.
Unknown parameters are ignored, with two deliberate exceptions: audio and a modalities value other than ["text"] are rejected rather than ignored, so a request for spoken output never comes back as silent text. Text in, text out. Images, audio and files are not accepted on any plan, and this is a product decision rather than a temporary gap. Send text and read text back.

Streaming

With stream: true the answer arrives in pieces, in the same format as OpenAI’s streaming, so stream: true in any OpenAI SDK works unchanged. Text is sent in small batches, at most one every 50 ms, and the stream ends with data: [DONE]. When the model calls one of your tools, the call arrives whole in a single chunk, followed by finish_reason: "tool_calls". If the request is refused before any text is written (a limit, a busy moment), the answer is the usual JSON error with its status code, so the SDKs retry it as they would without streaming. An error after text has already been sent arrives inside the stream as data: {"error": ...}, which the OpenAI SDKs raise as an exception.
The model decides on its own to search the web when the conversation asks for current or external information. Searches run server-side, and only the final answer is returned, citing result URLs. If you declare your own tool named web_search, yours replaces the built-in one.

Decision model

POST https://api.squarecloud.app/v2/ai/decisions asks the cubic-decision model typed questions about a piece of content and returns one typed answer per question, with its probabilities, instead of generated text. It suits classification, routing, moderation, triage and scoring, for a fraction of a chat completion’s tokens. It uses the same API key, ai:chat scope, plans and limits as the chat completions: one token budget for both endpoints (input and output tokens of both count), the same concurrency, pacing between requests and context window, then AI credits and pay-as-you-go. Errors use the same OpenAI shape. Question types:
  • noul: yes or no. The answer’s noul is the probability of yes. criteria may describe each side: { "true": "...", "false": "..." }.
  • choice: one of 2 to 32 options, given in criteria as an object (option name and what it means). The answer brings the choice and the probabilities of every option.
  • score: a scale of 2 to 32 ordered levels, lowest first, given in criteria as an array. The answer brings the score as a number on that scale, the probabilities of every level and a legend that names each level.
instructions (a string, or a JSON object or array) tells the model what to look at. Every answer carries its confidence, and a question the model declines comes back as { "type": "refusal" }. usage reports the tokens counted against your limits. Image box questions are not supported.

Errors

Every error uses the OpenAI shape { "error": { "message", "type", "param", "code" } }, including authentication, scope, rate-limit, invalid-body, 500 and 503 errors, and the code is always lowercase. Three of these look alike but mean different things:
  • 429 daily_spend_limit_reached: the daily limit of your key, set by your plan.
  • 503 daily_capacity_reached: the AI Gateway’s daily capacity is used up; it is not a limit of your key, and it renews every day.
  • 503 server_overloaded: a momentary overload, or a request that took longer than 90 seconds in total (provider queue, retries and search rounds included). Retry in a few seconds.

Retries

Most 429 and 503 answers tell your client when to try again, through headers the OpenAI SDKs read: The OpenAI SDKs honor a Retry-After of up to 60 seconds. A longer wait falls back to the SDK’s own backoff, unless the answer also carries x-should-retry: false, which turns its automatic retries off. Without an OpenAI SDK, read Retry-After yourself and wait that many seconds before the next request.

Frequently asked questions

No, the API is stateless, exactly like OpenAI’s: send the conversation history in messages on every request.
cubic, Square Cloud’s hosted model, for chat completions, and cubic-decision for the decision model. There is no model list to choose from; the model field is accepted for SDK compatibility.
Yes, that is the main use case. It is a normal HTTPS API, so it works from anywhere.
No. The gateway has its own daily and weekly token limits, separate from the dashboard assistant’s, so heavy gateway usage never uses up the assistant’s allowance, and vice versa.