Serverless Quick Start
Prepare an account, API key and Model ID, then send your first serverless inference request.
About serverless inference
Serverless inference lets you send requests on demand without provisioning capacity yourself. It can suit prototypes, variable traffic, model evaluations and low-frequency tasks. Usage is billed according to the applicable model price and meters. For sustained workloads, consider reserved throughput after confirming actual service availability.
Step 1: Register
Create an individual or company account to access the console.
Register ↗Step 2: Create an API key
Create and name a key in the console. Use separate keys for projects or environments. Keep the key in an environment variable or a secret manager; do not hard-code or commit it.
Manage API keys ↗Replace YOUR_API_BASE_URL with your assigned gateway base address; the examples already include /v1, so do not add it twice.
# Linux / macOS
export REQUEST_ID="your-request-id"
export TOKENGRID_API_KEY="sk-your-api-key"
export TOKENGRID_BASE_URL="YOUR_API_BASE_URL/v1"
# Windows (PowerShell)
$env:REQUEST_ID="your-request-id"
$env:TOKENGRID_API_KEY="sk-your-api-key"
$env:TOKENGRID_BASE_URL="YOUR_API_BASE_URL/v1"Step 3: Choose a model
Review available models, capabilities, context lengths and prices in the model catalog. Use the exact Model ID in the request's model field, not the display name.
Step 4: Send a request
Use your assigned gateway URL and API key. The following examples use the OpenAI Chat Completions format; supported capabilities depend on the model and enabled service. Select Python, JavaScript or cURL:
Set REQUEST_ID to a unique identifier for each request to help trace it.
Replace YOUR_API_BASE_URL with your assigned gateway base address; the examples already include /v1, so do not add it twice. Setting TOKENGRID_BASE_URL does not change examples with hard-coded addresses; replace the placeholder in those examples separately.
# pip install openai
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["TOKENGRID_API_KEY"],
base_url="YOUR_API_BASE_URL/v1"
)
response = client.chat.completions.create(
model="YOUR_MODEL_ID",
messages=[
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "Hello, please introduce yourself briefly."}
],
temperature=0.7,
max_tokens=512
)
print(response.choices[0].message.content)
print(f"Input tokens:{response.usage.prompt_tokens}")
print(f"Output tokens:{response.usage.completion_tokens}")
// npm install openai
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.TOKENGRID_API_KEY,
baseURL: "YOUR_API_BASE_URL/v1",
});
const response = await client.chat.completions.create({
model: "YOUR_MODEL_ID",
messages: [
{ role: "system", content: "You are a helpful AI assistant." },
{ role: "user", content: "Hello, please introduce yourself briefly." }
],
temperature: 0.7,
max_tokens: 512,
});
console.log(response.choices[0].message.content);
console.log("Input tokens:", response.usage.prompt_tokens);
console.log("Output tokens:", response.usage.completion_tokens);
curl "YOUR_API_BASE_URL/v1/chat/completions" \
-H "request-id: $REQUEST_ID" \
-H "Authorization: Bearer $TOKENGRID_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL_ID",
"messages": [
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "Hello, please introduce yourself briefly."}
],
"temperature": 0.7,
"max_tokens": 512
}'
Step 5: Read the response
{
"id": "chatcmpl-xxxxxxxx",
"object": "chat.completion",
"model": "YOUR_MODEL_ID",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I am an AI assistant on TokenGrid……"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 42,
"completion_tokens": 128,
"total_tokens": 170
}
}choices[0].message.contentcontains generated text.finish_reasonmay indicate a normal stop, a length limit, or a tool call.usagereports token usage when provided by the interface.
Step 6: Review usage
Review available token usage in the console. Data availability and update timing depend on the service.
View usage ↗ · Read pricing →Status Code Summary
| Status Code | Meaning | Typical Scenarios |
|---|---|---|
| 200 | Success | The request succeeded; if the asynchronous task itself fails, this may also be expressed as status: "failed" in the response. |
| 400 | Client request error | Invalid parameters, missing required fields, failure to parse the request body, or the task does not exist. |
| 401 | Authentication failed | Token is missing, invalid, or disabled; API Key does not exist, is disabled, expired, or unavailable. |
| 402 | Payment or quota unavailable | Overdue payment, insufficient balance, or plan or free quota exhausted. |
| 403 | Insufficient permissions | User is banned, lacks access to a group or model, or does not pass IP restrictions. |
| 404 | Resource not found | Model does not exist, has been deleted, or is not visible to the caller. |
| 405 | Request method not supported | Wrong endpoint called, for example, sending GET when POST should be used. |
| 408 | Upstream timed out waiting for the request | Request transmission is too slow, the connection is interrupted, or the complete request is not received in time. |
| 409 | Current operation conflicts with resource state | A resource with the same name is created again, or the task state does not allow the current operation. |
| 413 | Request body too large | The request body exceeds the size limit. |
| 415 | Content type not supported | Incorrect Content-Type, or unsupported file or media format. |
| 422 | Content cannot be processed | Invalid field types, message structure, or business parameter semantics. |
| 429 | Request rate limited | Model request rate limiting, global API rate limiting, or upstream load saturation. |
| 500 | Internal server error | Request conversion, upstream call, response parsing, or serialization failed. |
| 501 | Not implemented | The interface or conversion has not yet been implemented. |
| 502 | Gateway error | Upstream dependency failure, invalid response format, or failure to fetch the upstream URL in the video content proxy. |
| 503 | Service unavailable | No available channel, overloaded system resources, insufficient capacity, or service not ready. |
| 504 | Upstream timeout | The channel response time exceeded the limit. |
Next steps
- Text models: conversations, streaming and parameters.
- Vision models: image and document inputs.
- Tool calling: structured tool requests.
- Reserved throughput: capacity planning.