Gab AI API: first request, model routing and usage
Connect your app, send a request and build on the answer with a follow-up or function call.
Before your first request
Create a key in API Settings and set GAB_API_KEY in your server or shell environment. Authenticated API use requires Plus and applicable credits. Authentication Docs explains key setup and access.
Send your first OpenAI-compatible API request
Use https://gab.ai/v1 as the client base URL. This cURL request sends supplied workshop figures to Arya through Chat Completions. Run it from your own server or shell with your key set; read the returned message and finish reason.
curl -sS https://gab.ai/v1/chat/completions \
-H "Authorization: Bearer $GAB_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "arya",
"messages": [
{
"role": "system",
"content": "Use only the supplied facts. Do not add personal information, invented facts or external sources."
},
{
"role": "user",
"content": "Write three short sentences for a workshop proposal using only these facts: 24 proposed participants at USD 20 gives USD 480 income. Base cost is USD 370, so remaining funds are USD 110 before unspecified tax and delivery. No date, venue or approval exists. State the figures and unknowns clearly; do not invent details. Maximum 60 words."
}
],
"temperature": 0,
"max_tokens": 256,
"stream": false
}'Connect with the OpenAI SDK in Python or Node.js
Use the same Gab key and base URL in your SDK client. Replace the greeting prompt with your own messages.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["GAB_API_KEY"],
base_url="https://gab.ai/v1",
)
response = client.chat.completions.create(
model="arya",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, how are you?"}
],
)
print(response.choices[0].message.content)import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.GAB_API_KEY,
baseURL: 'https://gab.ai/v1',
});
const response = await client.chat.completions.create({
model: 'arya',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello, how are you?' },
],
});
console.log(response.choices[0].message.content);Read the answer and choose a request format
The workshop request returns the proposal below. Chat Completions puts text in choices[0].message.content. Responses uses output items and Messages uses content blocks; examples of those formats follow in the disclosure.
The workshop proposal includes 24 proposed participants at USD 20 each, generating USD 480 in income. After subtracting the USD 370 base cost, the remaining funds are USD 110 before unspecified tax and delivery. Currently, no date, venue, or approval exists for the event.
Responses and Messages request examples
These separate arithmetic requests ask for the remaining amount only. Both replies are USD 110; their request and response fields differ.
{
"model": "arya",
"input": "For a workshop, income is USD 480 and base cost is USD 370. Reply with the remaining amount only, as USD 110.",
"max_output_tokens": 128,
"stream": false
}{
"status": "completed",
"output": [
{
"id": "msg_95d199ceeb3c4e3bbee76e57003216a0",
"type": "message",
"status": "completed",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "USD 110",
"annotations": []
}
]
}
]
}{
"model": "arya",
"max_tokens": 128,
"messages": [
{
"role": "user",
"content": "For a workshop, income is USD 480 and base cost is USD 370. Reply with the remaining amount only, as USD 110."
}
]
}{
"model": "google/gemini-3.5-flash-lite",
"content": [
{
"type": "text",
"text": "USD 110"
}
],
"stop_reason": "end_turn"
}Continue the conversation with a follow-up
For Chat Completions, send the original messages, the preceding assistant reply and the new user instruction. This follow-up preserves the figures and unknowns while adding an approval next step.
Keep those figures and unknowns. Add a direct next action asking the group to confirm tax and delivery before approving the proposal. Use no more than 60 words.The workshop proposes 24 participants at USD 20, yielding USD 480 income. The base cost is USD 370, leaving USD 110 before unspecified tax and delivery. No date, venue, or approval exists. Please confirm tax and delivery before approving the proposal.
The follow-up adds a fourth sentence. Check the returned answer against your app’s length, format and source requirements.
Copy the complete follow-up request
curl -sS https://gab.ai/v1/chat/completions \
-H "Authorization: Bearer $GAB_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "arya",
"messages": [
{
"role": "system",
"content": "Use only the supplied facts. Do not add personal information, invented facts or external sources."
},
{
"role": "user",
"content": "Write three short sentences for a workshop proposal using only these facts: 24 proposed participants at USD 20 gives USD 480 income. Base cost is USD 370, so remaining funds are USD 110 before unspecified tax and delivery. No date, venue or approval exists. State the figures and unknowns clearly; do not invent details. Maximum 60 words."
},
{
"role": "assistant",
"content": "The workshop proposal includes 24 proposed participants at USD 20 each, generating USD 480 in income. After subtracting the USD 370 base cost, the remaining funds are USD 110 before unspecified tax and delivery. Currently, no date, venue, or approval exists for the event."
},
{
"role": "user",
"content": "Keep those figures and unknowns. Add a direct next action asking the group to confirm tax and delivery before approving the proposal. Use no more than 60 words."
}
],
"temperature": 0,
"max_tokens": 2048,
"stream": false
}'When an answer stops early
An earlier follow-up ended with finish_reason: length. Increasing max_tokens from 256 to 2048 allowed the separate retry shown above to finish with stop. Check the completion status and the text before accepting the result.
24 proposed participants at USD 20 yields
Choose a model or use Auto routing
Use a current model ID when you want a particular model, or model="auto" to request routing. Read the selected model and routing headers in the response. For a supported coding tool loop, the kickoff selection can stay in place until a new task. Media endpoints use their own model choices.
Connect the model to a function in your app
Send a function schema to a model with function-calling support. Read the returned name and arguments, run the approved function in your own application, then return its result with the matching tool_call_id. Send the next request to get the final answer.
A function-call exchange
| Step | What your app receives or returns |
|---|---|
| Requested function | workshop_remaining |
| Supplied arguments | {"income":480,"cost":370} |
| Your function result | {"remaining_usd": 110} |
| Final assistant reply | Remaining: $110 |
Read the function-call requests
{
"model": "arya",
"messages": [
{
"role": "user",
"content": "Use workshop_remaining with income 480 and cost 370. Do not invent other figures."
}
],
"tools": [
{
"type": "function",
"function": {
"name": "workshop_remaining",
"description": "Return the remaining funds from the supplied income and cost.",
"parameters": {
"type": "object",
"properties": {
"income": {
"type": "number"
},
"cost": {
"type": "number"
}
},
"required": [
"income",
"cost"
],
"additionalProperties": false
}
}
}
],
"tool_choice": {
"type": "function",
"function": {
"name": "workshop_remaining"
}
},
"max_tokens": 1024
}{
"model": "arya",
"messages": [
{
"role": "user",
"content": "Use workshop_remaining with income 480 and cost 370. Do not invent other figures."
},
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_180604",
"type": "function",
"function": {
"name": "workshop_remaining",
"arguments": "{\"income\":480,\"cost\":370}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_180604",
"content": "{\"remaining_usd\": 110}"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "workshop_remaining",
"description": "Return the remaining funds from the supplied income and cost.",
"parameters": {
"type": "object",
"properties": {
"income": {
"type": "number"
},
"cost": {
"type": "number"
}
},
"required": [
"income",
"cost"
],
"additionalProperties": false
}
}
}
],
"max_tokens": 1024
}Troubleshoot a request or incomplete answer
Check the HTTP status and error.code before retrying. Authentication Docs covers keys, access and daily limits. For model or input errors, compare your request with the endpoint reference and current model capabilities. Check finish_reason for Chat Completions, status for Responses and stop_reason for Messages, then read the answer itself.
If an API stream times out, distinguish a connection failure from a response that stopped after some output. Inspect your SDK, application server and proxy timeouts, and retain the received text and error details. Try a smaller request to isolate the problem. A transport interruption does not prove the upstream request was cancelled: check recorded usage before retrying, and avoid automatically replaying functions that could make duplicate changes.