Using the FLAME inference API¶
This guide walks you through calling a large language model hosted on FLAME
from your own code. FLAME runs an inference service with an
OpenAI-compatible API, so
anything that speaks to OpenAI — the official openai Python package, LangChain,
curl — can talk to FLAME's models instead, with one difference in how you
authenticate. You'll create an API credential for your application, fetch
its secret, and then write a short Python program that uses them to send a
chat request.
This guide assumes you've written code that calls an LLM API before, but are new to FLAME's inference service. If you haven't used the OpenAI API at all, the OpenAI quickstart is a good introduction to the request and response format. Skip its instructions for creating an API key, though: that part works completely differently on FLAME, and this guide covers it.
What you'll need first
- A FLAME workspace you're a member of. If you don't have one, see Requesting access.
- SSH access to FLAME. Creating and managing API credentials is done
over SSH with the
flamectlcommand. Follow CLI access to installstep, log in, and confirmssh flamectlworks before continuing.
The model endpoint is only reachable from the Drexel network. The application that uses the credentials doesn't need to run on FLAME, but it does need to be on campus, on the Drexel VPN, or running somewhere inside FLAME (a JupyterHub server or a job, for example).
The inference service¶
The API lives at:
It implements the OpenAI chat completions endpoint (/v1/chat/completions)
and the model list (/v1/models). Models are identified by the short names in
the catalog below, which you pass as the model field of each request.
| Model name | What it is |
|---|---|
gemma-4-26b |
Gemma 4 26B, an instruction-tuned chat model from Google |
GET /v1/models always returns the current list, so you can check what's
available at any time once you have a token.
What an API credential is¶
When you use OpenAI directly, you paste a long-lived API key into your code and send it with every request. FLAME works a little differently. Instead of an API key, each application gets an API credential: a registration that belongs to your workspace and identifies the application (not you personally) to FLAME's APIs. The same credential also works for services your workspace exposes to your lab.
An API credential comes with a client ID and a client secret. These are
not sent to the model API directly. Instead, your application exchanges them
for a short-lived access token, and it's the access token that goes in the
Authorization header of each request. This is the standard OAuth 2.0
"client credentials" flow, and it looks like this:
sequenceDiagram
participant App as Your application
participant Auth as FLAME token endpoint
participant API as inference.flamecluster.io
App->>Auth: POST client_id + client_secret
Auth-->>App: access_token (valid for 5 minutes)
loop until the token expires
App->>API: chat request, Authorization: Bearer access_token
API-->>App: completion
end
App->>Auth: POST client_id + client_secret again
Auth-->>App: fresh access_token
Tokens expire after five minutes. Your code must fetch a new one whenever the current one has expired (or is about to). The client secret itself is long-lived, so once you have it, your application can keep obtaining tokens on its own without anyone logging in.
Why the extra step? Access tokens are what the inference service actually checks, and because they're short-lived, a leaked token is only useful for a few minutes. The client secret never leaves your application's environment, and if it's ever exposed you can rotate it without touching anything else. Tying the credential to a workspace rather than a person also means the application keeps working when a student graduates or a collaborator moves on, as long as someone in the workspace remains its sponsor.1
Create an API credential¶
Pick a short name for your application (letters, digits, and hyphens) and create the credential in your workspace:
Replace my-workspace with your workspace's name (ssh flamectl server config
options lists the workspaces you belong to). The command waits until the
credential is ready and prints a summary like:
created API credential my-workspace/my-app
credential: my-workspace/my-app
sponsors: you@drexel.edu
enabled: True
expiresAt: 2027-09-10T15:04:05Z
credentialGeneration: 1
phase: Ready
Note that create does not print the secret. That's the next step.
Get the secret¶
This prints the same summary, followed by four export lines:
export FLAME_CLIENT_ID=...
export FLAME_CLIENT_SECRET=...
export FLAME_TOKEN_URL=https://...
export FLAME_OPENAI_BASE_URL=https://inference.flamecluster.io/v1
Those four values are everything your application needs. Run the export
lines in the shell where you'll run your code (or put them in a .env file,
your job's environment, a secrets manager, and so on). The secret
command is the only place the secret is shown, but you can re-run it at any
time if you need it again.
Treat the client secret like a password
Don't commit it to Git, paste it into a notebook you'll share, or bake it into a container image. If you think it's been exposed, rotate it (see below) and the old secret stops working immediately.
Prefer JSON?
Add --format json to get the same four values as a JSON object instead of
shell export lines, which is handier if you're loading them
programmatically.
Call the model¶
Here's a complete, working Python example. It needs the openai and httpx
packages (pip install openai httpx) and the four environment variables from
the previous step.
import os
import threading
import time
import httpx
from openai import OpenAI
class ClientCredentialsToken:
"""Fetch an access token on demand and refresh it before it expires."""
def __init__(self):
self._token = None
self._refresh_at = 0.0
self._lock = threading.Lock()
def __call__(self):
with self._lock:
if self._token and time.monotonic() < self._refresh_at:
return self._token
response = httpx.post(
os.environ["FLAME_TOKEN_URL"],
data={
"grant_type": "client_credentials",
"client_id": os.environ["FLAME_CLIENT_ID"],
"client_secret": os.environ["FLAME_CLIENT_SECRET"],
},
timeout=30,
)
response.raise_for_status()
payload = response.json()
lifetime = int(payload["expires_in"])
self._token = payload["access_token"]
# Refresh a little early so a token never expires mid-request.
self._refresh_at = time.monotonic() + lifetime - min(30, lifetime / 10)
return self._token
client = OpenAI(
api_key=ClientCredentialsToken(),
base_url=os.environ["FLAME_OPENAI_BASE_URL"],
)
result = client.chat.completions.create(
model="gemma-4-26b",
messages=[{"role": "user", "content": "Explain transfer learning in two sentences."}],
)
print(result.choices[0].message.content)
The interesting part is ClientCredentialsToken. The openai package accepts a
callable as api_key, and calls it before each request to get the value for
the Authorization header. Our callable posts the client ID and secret to the
token endpoint the first time it's called, caches the resulting token, and
returns the cached token until it's about to expire, at which point it fetches
a new one. Everything else is exactly the code you'd write against OpenAI: same
OpenAI constructor, same chat.completions.create, same response shape. The
class is safe to share between threads, so one instance is enough for a whole
application.
This is an example, not a requirement
You don't have to structure your code this way. Any HTTP client in any
language works, as long as it does two things: sends
Authorization: Bearer <access_token> with each request to
$FLAME_OPENAI_BASE_URL, and obtains a fresh token from
$FLAME_TOKEN_URL (a POST with form fields grant_type=client_credentials,
client_id, and client_secret) whenever the current one has expired. Most
OAuth 2.0 client libraries handle this flow out of the box if you'd rather
not write it yourself; see below for one.
Using an OAuth 2.0 library instead¶
If you'd rather not hand-write the token handling, an OAuth 2.0 client library
can do it for you. Here's the same program using
Authlib (pip install
openai authlib). Authlib's session knows the client-credentials flow, tracks
the token's expiry, and fetches a new one when asked:
import os
from authlib.integrations.requests_client import OAuth2Session
from openai import OpenAI
oauth = OAuth2Session(
client_id=os.environ["FLAME_CLIENT_ID"],
client_secret=os.environ["FLAME_CLIENT_SECRET"],
token_endpoint=os.environ["FLAME_TOKEN_URL"],
grant_type="client_credentials",
)
oauth.fetch_token()
def access_token():
# Re-fetches the token from the token endpoint if it has expired.
oauth.ensure_active_token(oauth.token)
return oauth.token["access_token"]
client = OpenAI(
api_key=access_token,
base_url=os.environ["FLAME_OPENAI_BASE_URL"],
)
result = client.chat.completions.create(
model="gemma-4-26b",
messages=[{"role": "user", "content": "Explain transfer learning in two sentences."}],
)
print(result.choices[0].message.content)
The shape is the same as before: a callable passed as api_key that returns a
valid token, with Authlib doing the caching and refreshing that
ClientCredentialsToken did by hand.
To try it from the shell instead, fetch a token with curl and use it
directly. Remember it's only good for five minutes:
TOKEN=$(curl -s -X POST "$FLAME_TOKEN_URL" \
-d grant_type=client_credentials \
-d client_id="$FLAME_CLIENT_ID" \
-d client_secret="$FLAME_CLIENT_SECRET" | jq -r .access_token)
curl -s "$FLAME_OPENAI_BASE_URL/models" -H "Authorization: Bearer $TOKEN"
Rotating and renewing a credential¶
You'll rarely need these, but two things can happen to a credential over time.
The secret is exposed, or you just want a new one. Rotate it, then fetch the new credentials and update every place the old secret was configured. The old secret stops working as soon as the rotation completes.
ssh flamectl credential rotate my-app --workspace my-workspace
ssh flamectl credential secret my-app --workspace my-workspace
The credential expires. Credentials are registered for one year at a time (the
expiresAt line in the status output). Renewing extends it by another year
from today, and doesn't change the secret:
ssh flamectl credential list shows every credential in your workspaces, and ssh
flamectl credential status my-app --workspace my-workspace shows one. Neither
prints the secret.2
-
Every API credential has one or more sponsors: workspace owners or members who are responsible for it. The person who creates the credential is its first sponsor. A sponsor must remain an active owner or member of the workspace, and the last sponsor can't be removed. To share responsibility with a collaborator, run
ssh flamectl credential sponsor add <credential> --user collaborator@drexel.edu --workspace <workspace>. Workspace owners can manage any credential in the workspace without being a sponsor, but must add themselves as a sponsor before retrieving its secret. ↩ -
A credential can also be switched off and back on without changing its secret with
ssh flamectl credential disable <credential>andenable, and removed entirely withssh flamectl credential delete <credential> --confirm <credential>(you retype the name as confirmation). All of these take--workspacelike the other commands. Any active sponsor may run them. ↩