Free AI API key on your server: call it with curl or Python
Servers on plans with AI come with a free OpenAI-compatible API key. List your models, test it with curl, then use the OpenAI Python SDK with your own base URL.
If your server's plan includes AI, it comes with a free API key for AI models, with an allowance set by the plan. The API follows the OpenAI format, so the usual tools and SDKs work with it once you set the base URL.
Before you start
- Run these commands on your Starcloud server, or on one of the IP addresses you added to the key on the AI tab. Calls from anywhere else are refused.
- The first invoice of the server is paid. Servers on a free trial have no key yet.
- Open Servers, choose your server and open the AI tab. Copy the base URL and the API key.
1. Put the base URL and key in variables
export STARCLOUD_AI_BASE_URL="paste-the-base-url-from-the-ai-tab"
export STARCLOUD_AI_KEY="paste-the-key-from-the-ai-tab"
Add the two lines to ~/.profile to keep them after you sign out. Never put the key in a Git repository or in code that runs in a browser. If it leaks, press Regenerate on the AI tab.
2. List your models
curl "$STARCLOUD_AI_BASE_URL/models" -H "Authorization: Bearer $STARCLOUD_AI_KEY"
The answer lists the models of your plan. Put the id of the one you want in a variable:
export STARCLOUD_AI_MODEL="paste-a-model-id-from-the-list"
3. Test with curl
curl -i "$STARCLOUD_AI_BASE_URL/chat/completions" \
-H "Authorization: Bearer $STARCLOUD_AI_KEY" \
-H "Content-Type: application/json" \
-d "{\"model\": \"$STARCLOUD_AI_MODEL\", \"messages\": [{\"role\": \"user\", \"content\": \"Say hello\"}]}"
The answer is in choices[0].message.content. The header x-starcloud-tokens-left shows how many tokens are left in this period, and x-starcloud-reset when the pool starts again.
4. Use the OpenAI Python SDK
On Ubuntu and Debian, install the SDK in a virtual environment:
sudo apt install -y python3-venv
python3 -m venv ~/ai
. ~/ai/bin/activate
pip install openai
Then:
import os
from openai import OpenAI
client = OpenAI(
base_url=os.environ["STARCLOUD_AI_BASE_URL"],
api_key=os.environ["STARCLOUD_AI_KEY"],
)
response = client.chat.completions.create(
model=os.environ["STARCLOUD_AI_MODEL"],
messages=[{"role": "user", "content": "Write a haiku about servers."}],
)
print(response.choices[0].message.content)
The same two settings (base URL and key) work in the OpenAI SDK for JavaScript, in LangChain and in most tools that let you choose an OpenAI-compatible endpoint. Use the Chat Completions API.
Allowance and limits
Your plan gives the key one pool of tokens per period (for example a day), shared across the plan's models, plus limits per minute. The AI tab shows the pool, the period, the models, the limits and what is left. Some models can count for more than one token per token: the AI tab says when. The pool starts again at the start of each period, in UTC, and unused tokens do not carry over. What you send and what the model writes back both count.
Errors
- 401: the key is wrong or was regenerated. Copy it again from the AI tab.
- 403
ip_not_allowed: the request did not come from the server the key belongs to or from an address you added. - 403
model_not_allowed: the model is not part of your plan. List your models (step 2). - 429
quota_exceeded: the pool for this period is used up. It comes back at the time inx-starcloud-reset. - 429
rate_limit_exceeded: too many tokens or requests in one minute. Wait for theRetry-Afterseconds; the OpenAI SDKs retry on their own.